… | |
… | |
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) = @_; |
… | |
… | |
166 | glPopMatrix; |
181 | glPopMatrix; |
167 | |
182 | |
168 | if ($self == $HOVER) { |
183 | if ($self == $HOVER) { |
169 | my ($x, $y) = @$self{qw(x y)}; |
184 | my ($x, $y) = @$self{qw(x y)}; |
170 | |
185 | |
171 | glColor 1, 1, 1, 0.1; |
186 | glColor 0, 0, 1, 0.2; |
172 | glEnable GL_BLEND; |
187 | glEnable GL_BLEND; |
173 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
188 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
174 | glBegin GL_QUADS; |
189 | glBegin GL_QUADS; |
175 | glVertex $x , $y; |
190 | glVertex $x , $y; |
176 | glVertex $x + $self->{w}, $y; |
191 | glVertex $x + $self->{w}, $y; |
… | |
… | |
260 | my $class = shift; |
275 | my $class = shift; |
261 | |
276 | |
262 | # range [value, low, high, page] |
277 | # range [value, low, high, page] |
263 | |
278 | |
264 | $class->SUPER::new ( |
279 | $class->SUPER::new ( |
265 | bg => [0, 0, 0, 0.4], |
280 | bg => [0, 0, 0, 0.2], |
266 | active_bg => [1, 1, 1], |
281 | active_bg => [1, 1, 1], |
267 | @_ |
282 | @_ |
268 | ) |
283 | ) |
269 | } |
284 | } |
270 | |
285 | |
271 | sub _draw { |
286 | sub _draw { |
272 | my ($self) = @_; |
287 | my ($self) = @_; |
273 | |
288 | |
274 | my ($w, $h) = @$self{qw(w h)}; |
289 | my ($w, $h) = @$self{qw(w h)}; |
275 | |
290 | |
|
|
291 | glEnable GL_BLEND; |
|
|
292 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
276 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
293 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
|
|
294 | |
277 | glBegin GL_QUADS; |
295 | glBegin GL_QUADS; |
278 | glVertex 0 , 0; |
296 | glVertex 0 , 0; |
279 | glVertex 0 , $h; |
297 | glVertex 0 , $h; |
280 | glVertex $w, $h; |
298 | glVertex $w, $h; |
281 | glVertex $w, 0; |
299 | glVertex $w, 0; |
282 | glEnd; |
300 | glEnd; |
|
|
301 | |
|
|
302 | glDisable GL_BLEND; |
283 | } |
303 | } |
284 | |
304 | |
285 | ############################################################################# |
305 | ############################################################################# |
286 | |
306 | |
287 | package CFClient::UI::Empty; |
307 | package CFClient::UI::Empty; |
… | |
… | |
320 | $self->{children} = [ |
340 | $self->{children} = [ |
321 | sort { $a->{z} <=> $b->{z} } |
341 | sort { $a->{z} <=> $b->{z} } |
322 | @{$self->{children}}, $chld |
342 | @{$self->{children}}, $chld |
323 | ]; |
343 | ]; |
324 | |
344 | |
325 | $self->size_allocate ($self->{w}, $self->{h}) |
345 | $self->{w} = $self->{h} = -1; |
326 | if $self->{w}; #TODO: check for "realised state" |
346 | $self->update; |
327 | } |
347 | } |
328 | |
348 | |
329 | sub remove { |
349 | sub remove { |
330 | my ($self, $widget) = @_; |
350 | my ($self, $widget) = @_; |
331 | |
351 | |
332 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
352 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
333 | |
353 | |
334 | $self->size_allocate ($self->{w}, $self->{h}); |
354 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
335 | } |
355 | } |
336 | |
356 | |
337 | sub find_widget { |
357 | sub find_widget { |
338 | my ($self, $x, $y) = @_; |
358 | my ($self, $x, $y) = @_; |
339 | |
359 | |
… | |
… | |
392 | sub size_request { |
412 | sub size_request { |
393 | $_[0]{children}[0]->size_request |
413 | $_[0]{children}[0]->size_request |
394 | } |
414 | } |
395 | |
415 | |
396 | sub size_allocate { |
416 | sub size_allocate { |
397 | my ($self, $w, $h) = @_; |
417 | my ($self, $x, $y, $w, $h) = @_; |
398 | |
418 | |
399 | return unless $self->{w} != $w || $self->{h} != $h; |
419 | $self->_size_allocate ($x, $y, $w, $h) or return; |
400 | |
420 | |
401 | $self->SUPER::size_allocate ($w, $h); |
|
|
402 | $self->{children}[0]->size_allocate ($w, $h); |
421 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
403 | } |
422 | } |
404 | |
423 | |
405 | ############################################################################# |
424 | ############################################################################# |
406 | |
425 | |
407 | package CFClient::UI::Window; |
426 | package CFClient::UI::Window; |
… | |
… | |
432 | $self->{w}, $self->{h}, sub { $self->child->draw } |
451 | $self->{w}, $self->{h}, sub { $self->child->draw } |
433 | ); |
452 | ); |
434 | } |
453 | } |
435 | |
454 | |
436 | sub size_allocate { |
455 | sub size_allocate { |
437 | my ($self, $w, $h) = @_; |
456 | my ($self, $x, $y, $w, $h) = @_; |
438 | |
457 | |
439 | return unless $self->{w} != $w || $self->{h} != $h; |
458 | $self->_size_allocate ($x, $y, $w, $h) or return; |
440 | |
459 | |
441 | $self->{w} = $w; |
|
|
442 | $self->{h} = $h; |
|
|
443 | |
|
|
444 | $self->child->size_allocate ($w, $h); |
460 | $self->child->size_allocate (0, 0, $w, $h); |
445 | |
461 | |
446 | $self->render_chld; |
462 | $self->render_chld; |
447 | } |
463 | } |
448 | |
464 | |
449 | sub _draw { |
465 | sub _draw { |
… | |
… | |
482 | |
498 | |
483 | map { $_ + 4 } $chld->size_request; |
499 | map { $_ + 4 } $chld->size_request; |
484 | } |
500 | } |
485 | |
501 | |
486 | sub size_allocate { |
502 | sub size_allocate { |
487 | my ($self, $w, $h) = @_; |
503 | my ($self, $x, $y, $w, $h) = @_; |
488 | |
504 | |
489 | return unless $self->{w} != $w || $self->{h} != $h; |
505 | $self->_size_allocate ($x, $y, $w, $h) or return; |
490 | |
506 | |
491 | $self->{w} = $w; |
|
|
492 | $self->{h} = $h; |
|
|
493 | |
|
|
494 | $self->child->size_allocate ($w - 4, $h - 4); |
507 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
495 | $self->child->move (2, 2); |
|
|
496 | } |
508 | } |
497 | |
509 | |
498 | sub _draw { |
510 | sub _draw { |
499 | my ($self) = @_; |
511 | my ($self) = @_; |
500 | |
512 | |
… | |
… | |
537 | |
549 | |
538 | ($w, $h) |
550 | ($w, $h) |
539 | } |
551 | } |
540 | |
552 | |
541 | sub size_allocate { |
553 | sub size_allocate { |
542 | my ($self, $w, $h) = @_; |
554 | my ($self, $x, $y, $w, $h) = @_; |
543 | |
555 | |
544 | return unless $self->{w} != $w || $self->{h} != $h; |
556 | $self->_size_allocate ($x, $y, $w, $h) or return; |
545 | |
|
|
546 | $self->SUPER::size_allocate ($w, $h); |
|
|
547 | |
557 | |
548 | $h -= $tex[1]->{h}; |
558 | $h -= $tex[1]->{h}; |
549 | $h -= $tex[4]->{h}; |
559 | $h -= $tex[4]->{h}; |
550 | $w -= $tex[2]->{w}; |
560 | $w -= $tex[2]->{w}; |
551 | $w -= $tex[3]->{w}; |
561 | $w -= $tex[3]->{w}; |
… | |
… | |
553 | $h = $h < 0 ? 0 : $h; |
563 | $h = $h < 0 ? 0 : $h; |
554 | $w = $w < 0 ? 0 : $w; |
564 | $w = $w < 0 ? 0 : $w; |
555 | |
565 | |
556 | my $child = $self->child; |
566 | my $child = $self->child; |
557 | |
567 | |
558 | $child->size_allocate ($w, $h); |
|
|
559 | $child->move ($tex[3]->{w}, $tex[1]->{h}); |
568 | $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); |
560 | } |
569 | } |
561 | |
570 | |
562 | sub _draw { |
571 | sub _draw { |
563 | my ($self) = @_; |
572 | my ($self) = @_; |
564 | |
573 | |
… | |
… | |
581 | |
590 | |
582 | my $bottom = $tex[4]; |
591 | my $bottom = $tex[4]; |
583 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
592 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
584 | |
593 | |
585 | my $bg = $tex[0]; |
594 | my $bg = $tex[0]; |
|
|
595 | |
586 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
596 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
587 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
597 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
588 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
598 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
589 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
599 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
590 | |
600 | |
591 | my $rep_x = $cw / $bg->{w}; |
601 | my $rep_x = $cw / $bg->{w}; |
592 | my $rep_y = $ch / $bg->{h}; |
602 | my $rep_y = $ch / $bg->{h}; |
593 | |
603 | |
594 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
604 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
595 | |
605 | |
|
|
606 | glDisable GL_TEXTURE_2D; |
596 | glDisable GL_BLEND; |
607 | glDisable GL_BLEND; |
597 | glDisable GL_TEXTURE_2D; |
|
|
598 | |
608 | |
599 | $self->child->draw; |
609 | $self->child->draw; |
600 | |
610 | |
601 | } |
611 | } |
602 | |
612 | |
603 | ############################################################################# |
613 | ############################################################################# |
604 | |
614 | |
605 | package CFClient::UI::Table; |
615 | package CFClient::UI::Table; |
606 | |
616 | |
607 | our @ISA = CFClient::UI::Base::; |
617 | our @ISA = CFClient::UI::Base::; |
|
|
618 | |
|
|
619 | use List::Util qw(max sum); |
608 | |
620 | |
609 | use SDL::OpenGL; |
621 | use SDL::OpenGL; |
610 | |
622 | |
611 | sub add { |
623 | sub add { |
612 | my ($self, $x, $y, $chld) = @_; |
624 | my ($self, $x, $y, $chld) = @_; |
613 | my $old_chld = $self->{children}[$y][$x]; |
|
|
614 | |
625 | |
615 | $self->{children}[$y][$x] = $chld; |
626 | $self->{children}[$y][$x] = $chld; |
616 | $chld->set_parent ($self); |
627 | $chld->set_parent ($self); |
|
|
628 | |
|
|
629 | $self->{w} = $self->{h} = -1; |
617 | $self->update; |
630 | $self->update; |
618 | } |
631 | } |
619 | |
632 | |
620 | sub max_row_height { |
633 | sub get_wh { |
621 | my ($self, $row) = @_; |
634 | my ($self) = @_; |
622 | |
635 | |
623 | my $hs = 0; |
636 | my (@w, @h); |
624 | for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { |
637 | |
|
|
638 | for my $y (0 .. $#{$self->{children}}) { |
625 | my $c = $self->{children}->[$row]->[$xi]; |
639 | my $row = $self->{children}[$y] |
626 | if ($c) { |
640 | or next; |
|
|
641 | |
|
|
642 | for my $x (0 .. $#$row) { |
|
|
643 | my $widget = $row->[$x] |
|
|
644 | or next; |
627 | my ($w, $h) = $c->size_request; |
645 | my ($w, $h) = $widget->size_request; |
628 | if ($hs < $h) { $hs = $h } |
646 | |
|
|
647 | $w[$x] = max $w[$x], $w; |
|
|
648 | $h[$y] = max $h[$y], $h; |
629 | } |
649 | } |
630 | } |
650 | } |
631 | return $hs; |
|
|
632 | } |
|
|
633 | |
651 | |
634 | sub max_col_width { |
652 | (\@w, \@h) |
|
|
653 | } |
|
|
654 | |
|
|
655 | sub size_request { |
635 | my ($self, $col) = @_; |
656 | my ($self) = @_; |
636 | |
657 | |
|
|
658 | my ($ws, $hs) = $self->get_wh; |
|
|
659 | |
|
|
660 | ( |
|
|
661 | (List::Util::sum @$ws), |
|
|
662 | (List::Util::sum @$hs), |
|
|
663 | ) |
|
|
664 | } |
|
|
665 | |
|
|
666 | sub size_allocate { |
|
|
667 | my ($self, $x, $y, $w, $h) = @_; |
|
|
668 | |
|
|
669 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
670 | |
|
|
671 | my ($ws, $hs) = $self->get_wh; |
|
|
672 | |
|
|
673 | my $req_w = List::Util::sum @$ws; |
|
|
674 | my $req_h = List::Util::sum @$hs; |
|
|
675 | |
|
|
676 | # linearly scale sizes |
|
|
677 | $_ *= $req_w / $w for @$ws; |
|
|
678 | $_ *= $req_h / $h for @$hs; |
|
|
679 | |
|
|
680 | my $y; |
|
|
681 | |
|
|
682 | for my $r (0 .. $#{$self->{children}}) { |
|
|
683 | my $row = $self->{children}[$r] |
|
|
684 | or next; |
|
|
685 | |
637 | my $ws = 0; |
686 | my $x = 0; |
638 | for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { |
687 | my $row_h = $hs->[$r]; |
639 | my $c = ($self->{children}->[$yi] || [])->[$col]; |
688 | |
640 | if ($c) { |
689 | for my $c (0 .. $#$row) { |
641 | my ($w, $h) = $c->size_request; |
690 | my $widget = $row->[$c] |
642 | if ($ws < $w) { $ws = $w } |
691 | or next; |
|
|
692 | |
|
|
693 | my $col_w = $ws->[$c]; |
|
|
694 | |
|
|
695 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
|
|
696 | |
|
|
697 | $x += $col_w; |
643 | } |
698 | } |
|
|
699 | |
|
|
700 | $y += $row_h; |
644 | } |
701 | } |
645 | return $ws; |
702 | |
646 | } |
703 | } |
|
|
704 | |
|
|
705 | sub find_widget { |
|
|
706 | my ($self, $x, $y) = @_; |
|
|
707 | |
|
|
708 | $x -= $self->{x}; |
|
|
709 | $y -= $self->{y}; |
|
|
710 | |
|
|
711 | my $res; |
|
|
712 | |
|
|
713 | for (grep $_, map @$_, grep $_, @{ $self->{children} }) { |
|
|
714 | $res = $_->find_widget ($x, $y) |
|
|
715 | and return $res; |
|
|
716 | } |
|
|
717 | |
|
|
718 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
|
|
719 | } |
|
|
720 | |
|
|
721 | sub _draw { |
|
|
722 | my ($self) = @_; |
|
|
723 | |
|
|
724 | for (grep $_, @{$self->{children}}) { |
|
|
725 | $_->draw for grep $_, @$_; |
|
|
726 | } |
|
|
727 | } |
|
|
728 | |
|
|
729 | ############################################################################# |
|
|
730 | |
|
|
731 | package CFClient::UI::HBox; |
|
|
732 | |
|
|
733 | # TODO: wrap into common Box base class |
|
|
734 | |
|
|
735 | our @ISA = CFClient::UI::Container::; |
647 | |
736 | |
648 | sub size_request { |
737 | sub size_request { |
649 | my ($self) = @_; |
738 | my ($self) = @_; |
650 | |
739 | |
651 | my ($hs, $ws) = (0, 0); |
|
|
652 | |
|
|
653 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
|
|
654 | $hs += $self->max_row_height ($yi); |
|
|
655 | } |
|
|
656 | |
|
|
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 | } |
|
|
667 | |
|
|
668 | sub _draw { |
|
|
669 | my ($self) = @_; |
|
|
670 | |
|
|
671 | my $y = 0; |
|
|
672 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
|
|
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 | } |
|
|
688 | } |
|
|
689 | |
|
|
690 | ############################################################################# |
|
|
691 | |
|
|
692 | package CFClient::UI::VBox; |
|
|
693 | |
|
|
694 | our @ISA = CFClient::UI::Container::; |
|
|
695 | |
|
|
696 | use SDL::OpenGL; |
|
|
697 | |
|
|
698 | sub size_request { |
|
|
699 | my ($self) = @_; |
|
|
700 | |
|
|
701 | my @alloc = map [$_->size_request], @{$self->{children}}; |
740 | my @alloc = map [$_->size_request], @{$self->{children}}; |
702 | |
741 | |
703 | ( |
742 | ( |
704 | (List::Util::max map $_->[0], @alloc), |
|
|
705 | (List::Util::sum map $_->[1], @alloc), |
743 | (List::Util::sum map $_->[0], @alloc), |
|
|
744 | (List::Util::max map $_->[1], @alloc), |
706 | ) |
745 | ) |
707 | } |
746 | } |
708 | |
747 | |
709 | sub size_allocate { |
748 | sub size_allocate { |
710 | my ($self, $w, $h) = @_; |
749 | my ($self, $x, $y, $w, $h) = @_; |
711 | |
750 | |
712 | return unless $self->{w} != $w || $self->{h} != $h; |
751 | $self->_size_allocate ($x, $y, $w, $h) or return; |
713 | |
752 | |
714 | $self->{w} = $w; |
|
|
715 | $self->{h} = $h; |
|
|
716 | |
|
|
717 | return unless $self->{h}; |
753 | return unless $self->{w}; |
|
|
754 | |
|
|
755 | ($h, $w) = ($w, $h); |
718 | |
756 | |
719 | my $children = $self->{children}; |
757 | my $children = $self->{children}; |
720 | |
758 | |
721 | my @h = map +($_->size_request)[1], @$children; |
759 | my @h = map +($_->size_request)[0], @$children; |
722 | |
760 | |
723 | my $req_h = List::Util::sum @h; |
761 | my $req_h = List::Util::sum @h; |
724 | |
762 | |
725 | if ($req_h > $h) { |
763 | if ($req_h > $h) { |
726 | # ah well, not enough space |
764 | # ah well, not enough space |
… | |
… | |
742 | |
780 | |
743 | my $y = 0; |
781 | my $y = 0; |
744 | for (0 .. $#$children) { |
782 | for (0 .. $#$children) { |
745 | my $child = $children->[$_]; |
783 | my $child = $children->[$_]; |
746 | my $h = $h[$_]; |
784 | my $h = $h[$_]; |
747 | $child->move (0, $y); |
785 | $child->size_allocate ($y, 0, $h, $w); |
|
|
786 | |
|
|
787 | $y += $h; |
|
|
788 | } |
|
|
789 | } |
|
|
790 | |
|
|
791 | ############################################################################# |
|
|
792 | |
|
|
793 | package CFClient::UI::VBox; |
|
|
794 | |
|
|
795 | # TODO: wrap into common Box base class |
|
|
796 | |
|
|
797 | our @ISA = CFClient::UI::Container::; |
|
|
798 | |
|
|
799 | sub size_request { |
|
|
800 | my ($self) = @_; |
|
|
801 | |
|
|
802 | my @alloc = map [$_->size_request], @{$self->{children}}; |
|
|
803 | |
|
|
804 | ( |
|
|
805 | (List::Util::max map $_->[0], @alloc), |
|
|
806 | (List::Util::sum map $_->[1], @alloc), |
|
|
807 | ) |
|
|
808 | } |
|
|
809 | |
|
|
810 | sub size_allocate { |
|
|
811 | my ($self, $x, $y, $w, $h) = @_; |
|
|
812 | |
|
|
813 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
814 | |
|
|
815 | return unless $self->{h}; |
|
|
816 | |
|
|
817 | my $children = $self->{children}; |
|
|
818 | |
|
|
819 | my @h = map +($_->size_request)[1], @$children; |
|
|
820 | |
|
|
821 | my $req_h = List::Util::sum @h; |
|
|
822 | |
|
|
823 | if ($req_h > $h) { |
|
|
824 | # ah well, not enough space |
|
|
825 | $_ = $h[$_] * $h / $req_h for @h; |
|
|
826 | } else { |
|
|
827 | my @exp = grep $_->{expand}, @$children; |
|
|
828 | @exp = @$children unless @exp; |
|
|
829 | |
|
|
830 | my %exp = map +($_ => 1), @exp; |
|
|
831 | |
|
|
832 | for (0 .. $#$children) { |
|
|
833 | my $child = $children->[$_]; |
|
|
834 | |
|
|
835 | my $alloc_h = $h[$_]; |
|
|
836 | $alloc_h += ($h - $req_h) / @exp if $exp{$child}; |
|
|
837 | $h[$_] = $alloc_h; |
|
|
838 | } |
|
|
839 | } |
|
|
840 | |
|
|
841 | my $y = 0; |
|
|
842 | for (0 .. $#$children) { |
|
|
843 | my $child = $children->[$_]; |
|
|
844 | my $h = $h[$_]; |
748 | $child->size_allocate ($w, $h); |
845 | $child->size_allocate (0, $y, $w, $h); |
749 | |
846 | |
750 | $y += $h; |
847 | $y += $h; |
751 | } |
848 | } |
752 | } |
849 | } |
753 | |
850 | |
… | |
… | |
761 | |
858 | |
762 | sub new { |
859 | sub new { |
763 | my ($class, %arg) = @_; |
860 | my ($class, %arg) = @_; |
764 | |
861 | |
765 | my $self = $class->SUPER::new ( |
862 | my $self = $class->SUPER::new ( |
766 | fg => [1, 1, 1], |
863 | fg => [1, 1, 1], |
767 | height => $::FONTSIZE, |
864 | height => $::FONTSIZE, |
768 | text => "", |
865 | text => "", |
769 | align => -1, |
866 | align => -1, |
|
|
867 | padding => 2, |
770 | layout => new CFClient::Layout, |
868 | layout => new CFClient::Layout, |
771 | %arg |
869 | %arg |
772 | ); |
870 | ); |
773 | |
871 | |
774 | $self->set_text ($self->{text}); |
872 | $self->set_text ($self->{text}); |
775 | |
873 | |
… | |
… | |
805 | sub size_request { |
903 | sub size_request { |
806 | my ($self) = @_; |
904 | my ($self) = @_; |
807 | |
905 | |
808 | $self->{layout}->set_width; |
906 | $self->{layout}->set_width; |
809 | $self->{layout}->set_height ($self->{height}); |
907 | $self->{layout}->set_height ($self->{height}); |
810 | $self->{layout}->size |
908 | my ($w, $h) = $self->{layout}->size; |
811 | # if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
909 | |
812 | # ( |
910 | ( |
813 | # $self->{texture}{w}, |
911 | $w + $self->{padding} * 2, |
814 | # $self->{texture}{h}, |
912 | $h + $self->{padding} * 2, |
815 | # ) |
913 | ) |
816 | # } else { |
|
|
817 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h}; |
|
|
818 | # |
|
|
819 | # ($w, $h) |
|
|
820 | # } |
|
|
821 | } |
914 | } |
822 | |
915 | |
823 | sub size_allocate { |
916 | sub size_allocate { |
824 | my ($self, $w, $h) = @_; |
917 | my ($self, $x, $y, $w, $h) = @_; |
825 | |
918 | |
826 | return unless $self->{w} != $w || $self->{h} != $h; |
919 | $self->_size_allocate ($x, $y, $w, $h) or return; |
827 | |
920 | |
828 | $self->SUPER::size_allocate ($w, $h); |
|
|
829 | delete $self->{texture}; |
921 | delete $self->{texture}; |
830 | } |
922 | } |
831 | |
923 | |
832 | sub update { |
924 | sub update { |
833 | my ($self) = @_; |
925 | my ($self) = @_; |
… | |
… | |
850 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
942 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
851 | |
943 | |
852 | glColor @{$self->{fg}}; |
944 | glColor @{$self->{fg}}; |
853 | |
945 | |
854 | my $x = |
946 | my $x = |
855 | $self->{align} < 0 ? 0 |
947 | $self->{align} < 0 ? $self->{padding} |
856 | : $self->{align} > 0 ? $self->{w} - $tex->{w} |
948 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
857 | : ($self->{w} - $tex->{w}) * 0.5; |
949 | : ($self->{w} - $tex->{w}) * 0.5; |
858 | |
950 | |
859 | $tex->draw_quad ($x, 0); |
951 | $tex->draw_quad ($x, 0); |
860 | |
952 | |
861 | glDisable GL_TEXTURE_2D; |
953 | glDisable GL_TEXTURE_2D; |
862 | glDisable GL_BLEND; |
954 | glDisable GL_BLEND; |
863 | glGetError and die; |
|
|
864 | } |
955 | } |
865 | |
956 | |
866 | ############################################################################# |
957 | ############################################################################# |
867 | |
958 | |
868 | package CFClient::UI::Entry; |
959 | package CFClient::UI::Entry; |
… | |
… | |
875 | sub new { |
966 | sub new { |
876 | my $class = shift; |
967 | my $class = shift; |
877 | |
968 | |
878 | $class->SUPER::new ( |
969 | $class->SUPER::new ( |
879 | fg => [1, 1, 1], |
970 | fg => [1, 1, 1], |
880 | bg => [0, 0, 0, 0.4], |
971 | bg => [0, 0, 0, 0.2], |
881 | active_bg => [1, 1, 1], |
972 | active_bg => [1, 1, 1], |
882 | active_fg => [0, 0, 0], |
973 | active_fg => [0, 0, 0], |
883 | @_ |
974 | @_ |
884 | ) |
975 | ) |
885 | } |
976 | } |
… | |
… | |
892 | $self->{text} = $text; |
983 | $self->{text} = $text; |
893 | $self->{layout}->set_width ($self->{w}); |
984 | $self->{layout}->set_width ($self->{w}); |
894 | |
985 | |
895 | $text =~ s/./*/g if $self->{hidden}; |
986 | $text =~ s/./*/g if $self->{hidden}; |
896 | |
987 | |
897 | |
|
|
898 | $self->{layout}->set_markup ($self->escape_text ($text)); |
988 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
899 | |
989 | |
900 | $text = substr $text, 0, $self->{cursor}; |
990 | $text = substr $text, 0, $self->{cursor}; |
901 | utf8::encode $text; |
991 | utf8::encode $text; |
902 | |
992 | |
903 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
993 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
… | |
… | |
910 | |
1000 | |
911 | ($w + 1, $h) # add 1 for cursor |
1001 | ($w + 1, $h) # add 1 for cursor |
912 | } |
1002 | } |
913 | |
1003 | |
914 | sub size_allocate { |
1004 | sub size_allocate { |
915 | my ($self, $w, $h) = @_; |
1005 | my ($self, $x, $y, $w, $h) = @_; |
916 | |
1006 | |
917 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
918 | |
|
|
919 | $self->SUPER::size_allocate ($w, $h); |
1007 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
920 | |
1008 | |
921 | $self->_set_text ($self->{text}); |
1009 | $self->_set_text ($self->{text}); |
922 | } |
1010 | } |
923 | |
1011 | |
924 | sub set_text { |
1012 | sub set_text { |
… | |
… | |
945 | substr $text, $self->{cursor}, 1, ""; |
1033 | substr $text, $self->{cursor}, 1, ""; |
946 | } elsif ($sym == SDLK_LEFT) { |
1034 | } elsif ($sym == SDLK_LEFT) { |
947 | --$self->{cursor} if $self->{cursor}; |
1035 | --$self->{cursor} if $self->{cursor}; |
948 | } elsif ($sym == SDLK_RIGHT) { |
1036 | } elsif ($sym == SDLK_RIGHT) { |
949 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1037 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
|
|
1038 | } elsif ($sym == SDLK_HOME) { |
|
|
1039 | $self->{cursor} = 0; |
|
|
1040 | } elsif ($sym == SDLK_END) { |
|
|
1041 | $self->{cursor} = length $text; |
950 | } elsif ($uni) { |
1042 | } elsif ($uni) { |
951 | substr $text, $self->{cursor}++, 0, chr $uni; |
1043 | substr $text, $self->{cursor}++, 0, chr $uni; |
952 | } |
1044 | } |
953 | |
1045 | |
954 | $self->_set_text ($text); |
1046 | $self->_set_text ($text); |
… | |
… | |
969 | $self->SUPER::button_down ($ev, $x, $y); |
1061 | $self->SUPER::button_down ($ev, $x, $y); |
970 | |
1062 | |
971 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1063 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
972 | |
1064 | |
973 | # byte-index to char-index |
1065 | # byte-index to char-index |
974 | my $text = $self->{layout}; |
1066 | my $text = $self->{text}; |
975 | utf8::encode $text; |
1067 | utf8::encode $text; |
976 | $self->{cursor} = length substr $text, 0, $idx; |
1068 | $self->{cursor} = length substr $text, 0, $idx; |
977 | |
1069 | |
978 | $self->_set_text ($self->{text}); |
1070 | $self->_set_text ($self->{text}); |
979 | $self->update; |
1071 | $self->update; |
… | |
… | |
994 | $self->{fg} = $self->{active_fg}; |
1086 | $self->{fg} = $self->{active_fg}; |
995 | } else { |
1087 | } else { |
996 | glColor @{$self->{bg}}; |
1088 | glColor @{$self->{bg}}; |
997 | } |
1089 | } |
998 | |
1090 | |
|
|
1091 | glEnable GL_BLEND; |
|
|
1092 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
999 | glBegin GL_QUADS; |
1093 | glBegin GL_QUADS; |
1000 | glVertex 0 , 0; |
1094 | glVertex 0 , 0; |
1001 | glVertex 0 , $self->{h}; |
1095 | glVertex 0 , $self->{h}; |
1002 | glVertex $self->{w}, $self->{h}; |
1096 | glVertex $self->{w}, $self->{h}; |
1003 | glVertex $self->{w}, 0; |
1097 | glVertex $self->{w}, 0; |
1004 | glEnd; |
1098 | glEnd; |
|
|
1099 | glDisable GL_BLEND; |
1005 | |
1100 | |
1006 | $self->SUPER::_draw; |
1101 | $self->SUPER::_draw; |
1007 | |
1102 | |
1008 | #TODO: force update every cursor change :( |
1103 | #TODO: force update every cursor change :( |
1009 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1104 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
… | |
… | |
1023 | |
1118 | |
1024 | use SDL::OpenGL; |
1119 | use SDL::OpenGL; |
1025 | |
1120 | |
1026 | our @ISA = CFClient::UI::DrawBG::; |
1121 | our @ISA = CFClient::UI::DrawBG::; |
1027 | |
1122 | |
1028 | sub size_request { |
|
|
1029 | my ($self) = @_; |
|
|
1030 | |
|
|
1031 | my $w = 50; |
|
|
1032 | my $h = 10; |
|
|
1033 | |
|
|
1034 | $self->{vertical} ? ($h, $w) : ($w, $h) |
|
|
1035 | } |
|
|
1036 | |
|
|
1037 | sub new { |
1123 | sub new { |
1038 | my $class = shift; |
1124 | my $class = shift; |
1039 | |
1125 | |
1040 | # range [value, low, high, page] |
1126 | # range [value, low, high, page] |
1041 | |
1127 | |
1042 | $class->SUPER::new ( |
1128 | my $self = $class->SUPER::new ( |
1043 | fg => [1, 1, 1], |
1129 | fg => [1, 1, 1], |
1044 | active_fg => [0, 0, 0], |
1130 | active_fg => [0, 0, 0], |
1045 | range => [0, 0, 100, 10], |
1131 | range => [0, 0, 100, 10], |
|
|
1132 | req_w => 40, |
|
|
1133 | req_h => 10, |
1046 | vertical => 1, |
1134 | vertical => 0, |
1047 | @_ |
1135 | @_ |
1048 | ) |
1136 | ); |
|
|
1137 | |
|
|
1138 | $self |
|
|
1139 | } |
|
|
1140 | |
|
|
1141 | sub size_request { |
|
|
1142 | my ($self) = @_; |
|
|
1143 | |
|
|
1144 | my $w = $self->{req_w}; |
|
|
1145 | my $h = $self->{req_h}; |
|
|
1146 | |
|
|
1147 | $self->{vertical} ? ($h, $w) : ($w, $h) |
1049 | } |
1148 | } |
1050 | |
1149 | |
1051 | sub button_down { |
1150 | sub button_down { |
1052 | my ($self, $ev, $x, $y) = @_; |
1151 | my ($self, $ev, $x, $y) = @_; |
1053 | |
1152 | |
… | |
… | |
1365 | sub size_request { |
1464 | sub size_request { |
1366 | ($::WIDTH, $::HEIGHT) |
1465 | ($::WIDTH, $::HEIGHT) |
1367 | } |
1466 | } |
1368 | |
1467 | |
1369 | sub size_allocate { |
1468 | sub size_allocate { |
1370 | my ($self, $w, $h) = @_; |
1469 | my ($self, $x, $y, $w, $h) = @_; |
1371 | |
1470 | |
1372 | $self->SUPER::size_allocate ($w, $h); |
1471 | $self->_size_allocate ($x, $y, $w, $h); |
1373 | |
1472 | |
1374 | $_->size_allocate ($_->size_request) |
1473 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
1375 | for @{$self->{children}}; |
1474 | for @{$self->{children}}; |
1376 | } |
1475 | } |
1377 | |
1476 | |
1378 | sub translate { |
1477 | sub translate { |
1379 | my ($self, $x, $y) = @_; |
1478 | my ($self, $x, $y) = @_; |
… | |
… | |
1382 | } |
1481 | } |
1383 | |
1482 | |
1384 | sub update { |
1483 | sub update { |
1385 | my ($self) = @_; |
1484 | my ($self) = @_; |
1386 | |
1485 | |
1387 | $self->size_allocate ($self->size_request); |
1486 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
1388 | ::refresh (); |
1487 | ::refresh (); |
1389 | } |
1488 | } |
1390 | |
1489 | |
1391 | sub add { |
1490 | sub add { |
1392 | my ($self, $widget) = @_; |
1491 | my ($self, $widget) = @_; |
1393 | |
1492 | |
1394 | $self->SUPER::add ($widget); |
1493 | $self->SUPER::add ($widget); |
1395 | |
1494 | |
1396 | $widget->size_allocate ($widget->size_request); |
1495 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
1397 | } |
1496 | } |
1398 | |
1497 | |
1399 | sub draw { |
1498 | sub draw { |
1400 | my ($self) = @_; |
1499 | my ($self) = @_; |
1401 | |
1500 | |