… | |
… | |
83 | |
83 | |
84 | bless { |
84 | 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 |
90 | } |
92 | } |
91 | |
93 | |
92 | sub move { |
94 | sub move { |
… | |
… | |
103 | sub size_request { |
105 | sub size_request { |
104 | require Carp; |
106 | require Carp; |
105 | Carp::confess "size_request is abtract"; |
107 | Carp::confess "size_request is abtract"; |
106 | } |
108 | } |
107 | |
109 | |
108 | sub size_allocate { |
110 | sub _size_allocate { |
109 | my ($self, $w, $h) = @_; |
111 | my ($self, $x, $y, $w, $h) = @_; |
|
|
112 | |
|
|
113 | $self->{x} = $x; |
|
|
114 | $self->{y} = $y; |
|
|
115 | |
|
|
116 | return unless $self->{w} != $w || $self->{h} != $h; |
110 | |
117 | |
111 | $self->{w} = $w; |
118 | $self->{w} = $w; |
112 | $self->{h} = $h; |
119 | $self->{h} = $h; |
|
|
120 | |
|
|
121 | 1 |
|
|
122 | } |
|
|
123 | |
|
|
124 | sub size_allocate { |
|
|
125 | my ($self, $x, $y, $w, $h) = @_; |
|
|
126 | |
|
|
127 | $self->_size_allocate ($x, $y, $w, $h); |
113 | } |
128 | } |
114 | |
129 | |
115 | # translate global koordinates to local coordinate system |
130 | # translate global koordinates to local coordinate system |
116 | sub translate { |
131 | sub translate { |
117 | my ($self, $x, $y) = @_; |
132 | my ($self, $x, $y) = @_; |
… | |
… | |
320 | $self->{children} = [ |
335 | $self->{children} = [ |
321 | sort { $a->{z} <=> $b->{z} } |
336 | sort { $a->{z} <=> $b->{z} } |
322 | @{$self->{children}}, $chld |
337 | @{$self->{children}}, $chld |
323 | ]; |
338 | ]; |
324 | |
339 | |
325 | $self->size_allocate ($self->{w}, $self->{h}) |
340 | $self->{w} = $self->{h} = -1; |
326 | if $self->{w}; #TODO: check for "realised state" |
341 | $self->update; |
327 | } |
342 | } |
328 | |
343 | |
329 | sub remove { |
344 | sub remove { |
330 | my ($self, $widget) = @_; |
345 | my ($self, $widget) = @_; |
331 | |
346 | |
332 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
347 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
333 | |
348 | |
334 | $self->size_allocate ($self->{w}, $self->{h}); |
349 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
335 | } |
350 | } |
336 | |
351 | |
337 | sub find_widget { |
352 | sub find_widget { |
338 | my ($self, $x, $y) = @_; |
353 | my ($self, $x, $y) = @_; |
339 | |
354 | |
… | |
… | |
392 | sub size_request { |
407 | sub size_request { |
393 | $_[0]{children}[0]->size_request |
408 | $_[0]{children}[0]->size_request |
394 | } |
409 | } |
395 | |
410 | |
396 | sub size_allocate { |
411 | sub size_allocate { |
397 | my ($self, $w, $h) = @_; |
412 | my ($self, $x, $y, $w, $h) = @_; |
398 | |
413 | |
399 | return unless $self->{w} != $w || $self->{h} != $h; |
414 | $self->_size_allocate ($x, $y, $w, $h) or return; |
400 | |
415 | |
401 | $self->SUPER::size_allocate ($w, $h); |
|
|
402 | $self->{children}[0]->size_allocate ($w, $h); |
416 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
403 | } |
417 | } |
404 | |
418 | |
405 | ############################################################################# |
419 | ############################################################################# |
406 | |
420 | |
407 | package CFClient::UI::Window; |
421 | package CFClient::UI::Window; |
… | |
… | |
432 | $self->{w}, $self->{h}, sub { $self->child->draw } |
446 | $self->{w}, $self->{h}, sub { $self->child->draw } |
433 | ); |
447 | ); |
434 | } |
448 | } |
435 | |
449 | |
436 | sub size_allocate { |
450 | sub size_allocate { |
437 | my ($self, $w, $h) = @_; |
451 | my ($self, $x, $y, $w, $h) = @_; |
438 | |
452 | |
439 | return unless $self->{w} != $w || $self->{h} != $h; |
453 | $self->_size_allocate ($x, $y, $w, $h) or return; |
440 | |
454 | |
441 | $self->{w} = $w; |
|
|
442 | $self->{h} = $h; |
|
|
443 | |
|
|
444 | $self->child->size_allocate ($w, $h); |
455 | $self->child->size_allocate (0, 0, $w, $h); |
445 | |
456 | |
446 | $self->render_chld; |
457 | $self->render_chld; |
447 | } |
458 | } |
448 | |
459 | |
449 | sub _draw { |
460 | sub _draw { |
… | |
… | |
482 | |
493 | |
483 | map { $_ + 4 } $chld->size_request; |
494 | map { $_ + 4 } $chld->size_request; |
484 | } |
495 | } |
485 | |
496 | |
486 | sub size_allocate { |
497 | sub size_allocate { |
487 | my ($self, $w, $h) = @_; |
498 | my ($self, $x, $y, $w, $h) = @_; |
488 | |
499 | |
489 | return unless $self->{w} != $w || $self->{h} != $h; |
500 | $self->_size_allocate ($x, $y, $w, $h) or return; |
490 | |
501 | |
491 | $self->{w} = $w; |
|
|
492 | $self->{h} = $h; |
|
|
493 | |
|
|
494 | $self->child->size_allocate ($w - 4, $h - 4); |
502 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
495 | $self->child->move (2, 2); |
|
|
496 | } |
503 | } |
497 | |
504 | |
498 | sub _draw { |
505 | sub _draw { |
499 | my ($self) = @_; |
506 | my ($self) = @_; |
500 | |
507 | |
… | |
… | |
537 | |
544 | |
538 | ($w, $h) |
545 | ($w, $h) |
539 | } |
546 | } |
540 | |
547 | |
541 | sub size_allocate { |
548 | sub size_allocate { |
542 | my ($self, $w, $h) = @_; |
549 | my ($self, $x, $y, $w, $h) = @_; |
543 | |
550 | |
544 | return unless $self->{w} != $w || $self->{h} != $h; |
551 | $self->_size_allocate ($x, $y, $w, $h) or return; |
545 | |
|
|
546 | $self->SUPER::size_allocate ($w, $h); |
|
|
547 | |
552 | |
548 | $h -= $tex[1]->{h}; |
553 | $h -= $tex[1]->{h}; |
549 | $h -= $tex[4]->{h}; |
554 | $h -= $tex[4]->{h}; |
550 | $w -= $tex[2]->{w}; |
555 | $w -= $tex[2]->{w}; |
551 | $w -= $tex[3]->{w}; |
556 | $w -= $tex[3]->{w}; |
… | |
… | |
553 | $h = $h < 0 ? 0 : $h; |
558 | $h = $h < 0 ? 0 : $h; |
554 | $w = $w < 0 ? 0 : $w; |
559 | $w = $w < 0 ? 0 : $w; |
555 | |
560 | |
556 | my $child = $self->child; |
561 | my $child = $self->child; |
557 | |
562 | |
558 | $child->size_allocate ($w, $h); |
|
|
559 | $child->move ($tex[3]->{w}, $tex[1]->{h}); |
563 | $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); |
560 | } |
564 | } |
561 | |
565 | |
562 | sub _draw { |
566 | sub _draw { |
563 | my ($self) = @_; |
567 | my ($self) = @_; |
564 | |
568 | |
… | |
… | |
604 | |
608 | |
605 | package CFClient::UI::Table; |
609 | package CFClient::UI::Table; |
606 | |
610 | |
607 | our @ISA = CFClient::UI::Base::; |
611 | our @ISA = CFClient::UI::Base::; |
608 | |
612 | |
|
|
613 | use List::Util qw(max sum); |
|
|
614 | |
609 | use SDL::OpenGL; |
615 | use SDL::OpenGL; |
610 | |
616 | |
611 | sub add { |
617 | sub add { |
612 | my ($self, $x, $y, $chld) = @_; |
618 | my ($self, $x, $y, $chld) = @_; |
613 | my $old_chld = $self->{children}[$y][$x]; |
|
|
614 | |
619 | |
615 | $self->{children}[$y][$x] = $chld; |
620 | $self->{children}[$y][$x] = $chld; |
616 | $chld->set_parent ($self); |
621 | $chld->set_parent ($self); |
|
|
622 | |
|
|
623 | $self->{w} = $self->{h} = -1; |
617 | $self->update; |
624 | $self->update; |
618 | } |
625 | } |
619 | |
626 | |
620 | sub max_row_height { |
627 | sub get_wh { |
621 | my ($self, $row) = @_; |
628 | my ($self) = @_; |
622 | |
629 | |
623 | my $hs = 0; |
630 | my (@w, @h); |
624 | for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { |
631 | |
|
|
632 | for my $y (0 .. $#{$self->{children}}) { |
625 | my $c = $self->{children}->[$row]->[$xi]; |
633 | my $row = $self->{children}[$y] |
626 | if ($c) { |
634 | or next; |
|
|
635 | |
|
|
636 | for my $x (0 .. $#$row) { |
|
|
637 | my $widget = $row->[$x] |
|
|
638 | or next; |
627 | my ($w, $h) = $c->size_request; |
639 | my ($w, $h) = $widget->size_request; |
628 | if ($hs < $h) { $hs = $h } |
640 | |
|
|
641 | $w[$x] = max $w[$x], $w; |
|
|
642 | $h[$y] = max $h[$y], $h; |
629 | } |
643 | } |
630 | } |
644 | } |
631 | return $hs; |
|
|
632 | } |
|
|
633 | |
645 | |
634 | sub max_col_width { |
646 | (\@w, \@h) |
|
|
647 | } |
|
|
648 | |
|
|
649 | sub size_request { |
635 | my ($self, $col) = @_; |
650 | my ($self) = @_; |
636 | |
651 | |
|
|
652 | my ($ws, $hs) = $self->get_wh; |
|
|
653 | |
|
|
654 | ( |
|
|
655 | (List::Util::sum @$ws), |
|
|
656 | (List::Util::sum @$hs), |
|
|
657 | ) |
|
|
658 | } |
|
|
659 | |
|
|
660 | sub size_allocate { |
|
|
661 | my ($self, $x, $y, $w, $h) = @_; |
|
|
662 | |
|
|
663 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
664 | |
|
|
665 | my ($ws, $hs) = $self->get_wh; |
|
|
666 | |
|
|
667 | my $req_w = List::Util::sum @$ws; |
|
|
668 | my $req_h = List::Util::sum @$hs; |
|
|
669 | |
|
|
670 | # linearly scale sizes |
|
|
671 | $_ *= $req_w / $w for @$ws; |
|
|
672 | $_ *= $req_h / $h for @$hs; |
|
|
673 | |
|
|
674 | my $y; |
|
|
675 | |
|
|
676 | for my $r (0 .. $#{$self->{children}}) { |
|
|
677 | my $row = $self->{children}[$r] |
|
|
678 | or next; |
|
|
679 | |
637 | my $ws = 0; |
680 | my $x = 0; |
638 | for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { |
681 | my $row_h = $hs->[$r]; |
639 | my $c = ($self->{children}->[$yi] || [])->[$col]; |
682 | |
640 | if ($c) { |
683 | for my $c (0 .. $#$row) { |
641 | my ($w, $h) = $c->size_request; |
684 | my $widget = $row->[$c] |
642 | if ($ws < $w) { $ws = $w } |
685 | or next; |
|
|
686 | |
|
|
687 | my $col_w = $ws->[$c]; |
|
|
688 | |
|
|
689 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
|
|
690 | |
|
|
691 | $x += $col_w; |
643 | } |
692 | } |
644 | } |
|
|
645 | return $ws; |
|
|
646 | } |
|
|
647 | |
693 | |
648 | sub size_request { |
694 | $y += $row_h; |
649 | my ($self) = @_; |
|
|
650 | |
|
|
651 | my ($hs, $ws) = (0, 0); |
|
|
652 | |
|
|
653 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
|
|
654 | $hs += $self->max_row_height ($yi); |
|
|
655 | } |
695 | } |
656 | |
696 | |
657 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
|
|
658 | my $wm = 0; |
|
|
659 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
|
|
660 | $wm += $self->max_col_width ($xi) |
|
|
661 | } |
|
|
662 | if ($ws < $wm) { $ws = $wm } |
|
|
663 | } |
|
|
664 | |
|
|
665 | return ($ws, $hs); |
|
|
666 | } |
697 | } |
667 | |
698 | |
668 | sub _draw { |
699 | sub _draw { |
669 | my ($self) = @_; |
700 | my ($self) = @_; |
670 | |
701 | |
671 | my $y = 0; |
702 | for (grep $_, @{$self->{children}}) { |
672 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
703 | $_->draw for grep $_, @$_; |
673 | my $x = 0; |
|
|
674 | |
|
|
675 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
|
|
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 | } |
704 | } |
688 | } |
705 | } |
689 | |
706 | |
690 | ############################################################################# |
707 | ############################################################################# |
691 | |
708 | |
… | |
… | |
705 | (List::Util::sum map $_->[1], @alloc), |
722 | (List::Util::sum map $_->[1], @alloc), |
706 | ) |
723 | ) |
707 | } |
724 | } |
708 | |
725 | |
709 | sub size_allocate { |
726 | sub size_allocate { |
710 | my ($self, $w, $h) = @_; |
727 | my ($self, $x, $y, $w, $h) = @_; |
711 | |
728 | |
712 | return unless $self->{w} != $w || $self->{h} != $h; |
729 | $self->_size_allocate ($x, $y, $w, $h) or return; |
713 | |
|
|
714 | $self->{w} = $w; |
|
|
715 | $self->{h} = $h; |
|
|
716 | |
730 | |
717 | return unless $self->{h}; |
731 | return unless $self->{h}; |
718 | |
732 | |
719 | my $children = $self->{children}; |
733 | my $children = $self->{children}; |
720 | |
734 | |
… | |
… | |
742 | |
756 | |
743 | my $y = 0; |
757 | my $y = 0; |
744 | for (0 .. $#$children) { |
758 | for (0 .. $#$children) { |
745 | my $child = $children->[$_]; |
759 | my $child = $children->[$_]; |
746 | my $h = $h[$_]; |
760 | my $h = $h[$_]; |
747 | $child->move (0, $y); |
|
|
748 | $child->size_allocate ($w, $h); |
761 | $child->size_allocate (0, $y, $w, $h); |
749 | |
762 | |
750 | $y += $h; |
763 | $y += $h; |
751 | } |
764 | } |
752 | } |
765 | } |
753 | |
766 | |
… | |
… | |
819 | # ($w, $h) |
832 | # ($w, $h) |
820 | # } |
833 | # } |
821 | } |
834 | } |
822 | |
835 | |
823 | sub size_allocate { |
836 | sub size_allocate { |
824 | my ($self, $w, $h) = @_; |
837 | my ($self, $x, $y, $w, $h) = @_; |
825 | |
838 | |
826 | return unless $self->{w} != $w || $self->{h} != $h; |
839 | $self->_size_allocate ($x, $y, $w, $h) or return; |
827 | |
840 | |
828 | $self->SUPER::size_allocate ($w, $h); |
|
|
829 | delete $self->{texture}; |
841 | delete $self->{texture}; |
830 | } |
842 | } |
831 | |
843 | |
832 | sub update { |
844 | sub update { |
833 | my ($self) = @_; |
845 | my ($self) = @_; |
… | |
… | |
858 | |
870 | |
859 | $tex->draw_quad ($x, 0); |
871 | $tex->draw_quad ($x, 0); |
860 | |
872 | |
861 | glDisable GL_TEXTURE_2D; |
873 | glDisable GL_TEXTURE_2D; |
862 | glDisable GL_BLEND; |
874 | glDisable GL_BLEND; |
863 | glGetError and die; |
|
|
864 | } |
875 | } |
865 | |
876 | |
866 | ############################################################################# |
877 | ############################################################################# |
867 | |
878 | |
868 | package CFClient::UI::Entry; |
879 | package CFClient::UI::Entry; |
… | |
… | |
910 | |
921 | |
911 | ($w + 1, $h) # add 1 for cursor |
922 | ($w + 1, $h) # add 1 for cursor |
912 | } |
923 | } |
913 | |
924 | |
914 | sub size_allocate { |
925 | sub size_allocate { |
915 | my ($self, $w, $h) = @_; |
926 | my ($self, $x, $y, $w, $h) = @_; |
916 | |
927 | |
917 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
918 | |
|
|
919 | $self->SUPER::size_allocate ($w, $h); |
928 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
920 | |
929 | |
921 | $self->_set_text ($self->{text}); |
930 | $self->_set_text ($self->{text}); |
922 | } |
931 | } |
923 | |
932 | |
924 | sub set_text { |
933 | sub set_text { |
… | |
… | |
1365 | sub size_request { |
1374 | sub size_request { |
1366 | ($::WIDTH, $::HEIGHT) |
1375 | ($::WIDTH, $::HEIGHT) |
1367 | } |
1376 | } |
1368 | |
1377 | |
1369 | sub size_allocate { |
1378 | sub size_allocate { |
1370 | my ($self, $w, $h) = @_; |
1379 | my ($self, $x, $y, $w, $h) = @_; |
1371 | |
1380 | |
1372 | $self->SUPER::size_allocate ($w, $h); |
1381 | $self->_size_allocate ($x, $y, $w, $h); |
1373 | |
1382 | |
1374 | $_->size_allocate ($_->size_request) |
1383 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
1375 | for @{$self->{children}}; |
1384 | for @{$self->{children}}; |
1376 | } |
1385 | } |
1377 | |
1386 | |
1378 | sub translate { |
1387 | sub translate { |
1379 | my ($self, $x, $y) = @_; |
1388 | my ($self, $x, $y) = @_; |
… | |
… | |
1382 | } |
1391 | } |
1383 | |
1392 | |
1384 | sub update { |
1393 | sub update { |
1385 | my ($self) = @_; |
1394 | my ($self) = @_; |
1386 | |
1395 | |
1387 | $self->size_allocate ($self->size_request); |
1396 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
1388 | ::refresh (); |
1397 | ::refresh (); |
1389 | } |
1398 | } |
1390 | |
1399 | |
1391 | sub add { |
1400 | sub add { |
1392 | my ($self, $widget) = @_; |
1401 | my ($self, $widget) = @_; |
1393 | |
1402 | |
1394 | $self->SUPER::add ($widget); |
1403 | $self->SUPER::add ($widget); |
1395 | |
1404 | |
1396 | $widget->size_allocate ($widget->size_request); |
1405 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
1397 | } |
1406 | } |
1398 | |
1407 | |
1399 | sub draw { |
1408 | sub draw { |
1400 | my ($self) = @_; |
1409 | my ($self) = @_; |
1401 | |
1410 | |