1 | package CFClient::UI; |
1 | package CFClient::UI; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use Scalar::Util; |
5 | use Scalar::Util (); |
|
|
6 | use List::Util (); |
6 | |
7 | |
7 | use CFClient; |
8 | use CFClient; |
8 | |
9 | |
9 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | |
11 | |
… | |
… | |
82 | |
83 | |
83 | bless { |
84 | bless { |
84 | x => 0, |
85 | x => 0, |
85 | y => 0, |
86 | y => 0, |
86 | z => 0, |
87 | z => 0, |
|
|
88 | w => -1, |
|
|
89 | h => -1, |
87 | @_ |
90 | @_ |
88 | }, $class |
91 | }, $class |
89 | } |
92 | } |
90 | |
93 | |
91 | sub move { |
94 | sub move { |
… | |
… | |
102 | sub size_request { |
105 | sub size_request { |
103 | require Carp; |
106 | require Carp; |
104 | Carp::confess "size_request is abtract"; |
107 | Carp::confess "size_request is abtract"; |
105 | } |
108 | } |
106 | |
109 | |
107 | sub size_allocate { |
110 | sub _size_allocate { |
108 | 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; |
109 | |
117 | |
110 | $self->{w} = $w; |
118 | $self->{w} = $w; |
111 | $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); |
112 | } |
128 | } |
113 | |
129 | |
114 | # translate global koordinates to local coordinate system |
130 | # translate global koordinates to local coordinate system |
115 | sub translate { |
131 | sub translate { |
116 | my ($self, $x, $y) = @_; |
132 | my ($self, $x, $y) = @_; |
… | |
… | |
165 | glPopMatrix; |
181 | glPopMatrix; |
166 | |
182 | |
167 | if ($self == $HOVER) { |
183 | if ($self == $HOVER) { |
168 | my ($x, $y) = @$self{qw(x y)}; |
184 | my ($x, $y) = @$self{qw(x y)}; |
169 | |
185 | |
170 | glColor 1, 1, 1, 0.1; |
186 | glColor 0, 0, 1, 0.2; |
171 | glEnable GL_BLEND; |
187 | glEnable GL_BLEND; |
|
|
188 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
172 | glBegin GL_QUADS; |
189 | glBegin GL_QUADS; |
173 | glVertex $x , $y; |
190 | glVertex $x , $y; |
174 | glVertex $x + $self->{w}, $y; |
191 | glVertex $x + $self->{w}, $y; |
175 | glVertex $x + $self->{w}, $y + $self->{h}; |
192 | glVertex $x + $self->{w}, $y + $self->{h}; |
176 | glVertex $x , $y + $self->{h}; |
193 | glVertex $x , $y + $self->{h}; |
… | |
… | |
258 | my $class = shift; |
275 | my $class = shift; |
259 | |
276 | |
260 | # range [value, low, high, page] |
277 | # range [value, low, high, page] |
261 | |
278 | |
262 | $class->SUPER::new ( |
279 | $class->SUPER::new ( |
263 | bg => [0, 0, 0, 0.4], |
280 | bg => [0, 0, 0, 0.2], |
264 | active_bg => [1, 1, 1], |
281 | active_bg => [1, 1, 1], |
265 | @_ |
282 | @_ |
266 | ) |
283 | ) |
267 | } |
284 | } |
268 | |
285 | |
269 | sub _draw { |
286 | sub _draw { |
270 | my ($self) = @_; |
287 | my ($self) = @_; |
271 | |
288 | |
272 | my ($w, $h) = @$self{qw(w h)}; |
289 | my ($w, $h) = @$self{qw(w h)}; |
273 | |
290 | |
|
|
291 | glEnable GL_BLEND; |
|
|
292 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
274 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
293 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
|
|
294 | |
275 | glBegin GL_QUADS; |
295 | glBegin GL_QUADS; |
276 | glVertex 0 , 0; |
296 | glVertex 0 , 0; |
277 | glVertex 0 , $h; |
297 | glVertex 0 , $h; |
278 | glVertex $w, $h; |
298 | glVertex $w, $h; |
279 | glVertex $w, 0; |
299 | glVertex $w, 0; |
280 | glEnd; |
300 | glEnd; |
|
|
301 | |
|
|
302 | glDisable GL_BLEND; |
281 | } |
303 | } |
282 | |
304 | |
283 | ############################################################################# |
305 | ############################################################################# |
284 | |
306 | |
285 | package CFClient::UI::Empty; |
307 | package CFClient::UI::Empty; |
… | |
… | |
318 | $self->{children} = [ |
340 | $self->{children} = [ |
319 | sort { $a->{z} <=> $b->{z} } |
341 | sort { $a->{z} <=> $b->{z} } |
320 | @{$self->{children}}, $chld |
342 | @{$self->{children}}, $chld |
321 | ]; |
343 | ]; |
322 | |
344 | |
323 | $self->size_allocate ($self->{w}, $self->{h}) |
345 | $self->{w} = $self->{h} = -1; |
324 | if $self->{w}; #TODO: check for "realised state" |
346 | $self->update; |
325 | } |
347 | } |
326 | |
348 | |
327 | sub remove { |
349 | sub remove { |
328 | my ($self, $widget) = @_; |
350 | my ($self, $widget) = @_; |
329 | |
351 | |
330 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
352 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
331 | |
353 | |
332 | $self->size_allocate ($self->{w}, $self->{h}); |
354 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
333 | } |
355 | } |
334 | |
356 | |
335 | sub find_widget { |
357 | sub find_widget { |
336 | my ($self, $x, $y) = @_; |
358 | my ($self, $x, $y) = @_; |
337 | |
359 | |
… | |
… | |
390 | sub size_request { |
412 | sub size_request { |
391 | $_[0]{children}[0]->size_request |
413 | $_[0]{children}[0]->size_request |
392 | } |
414 | } |
393 | |
415 | |
394 | sub size_allocate { |
416 | sub size_allocate { |
395 | my ($self, $w, $h) = @_; |
417 | my ($self, $x, $y, $w, $h) = @_; |
396 | |
418 | |
397 | return unless $self->{w} != $w || $self->{h} != $h; |
419 | $self->_size_allocate ($x, $y, $w, $h) or return; |
398 | |
420 | |
399 | $self->SUPER::size_allocate ($w, $h); |
|
|
400 | $self->{children}[0]->size_allocate ($w, $h); |
421 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
401 | } |
422 | } |
402 | |
423 | |
403 | ############################################################################# |
424 | ############################################################################# |
404 | |
425 | |
405 | package CFClient::UI::Window; |
426 | package CFClient::UI::Window; |
… | |
… | |
430 | $self->{w}, $self->{h}, sub { $self->child->draw } |
451 | $self->{w}, $self->{h}, sub { $self->child->draw } |
431 | ); |
452 | ); |
432 | } |
453 | } |
433 | |
454 | |
434 | sub size_allocate { |
455 | sub size_allocate { |
435 | my ($self, $w, $h) = @_; |
456 | my ($self, $x, $y, $w, $h) = @_; |
436 | |
457 | |
437 | return unless $self->{w} != $w || $self->{h} != $h; |
458 | $self->_size_allocate ($x, $y, $w, $h) or return; |
438 | |
459 | |
439 | $self->{w} = $w; |
|
|
440 | $self->{h} = $h; |
|
|
441 | |
|
|
442 | $self->child->size_allocate ($w, $h); |
460 | $self->child->size_allocate (0, 0, $w, $h); |
443 | |
461 | |
444 | $self->render_chld; |
462 | $self->render_chld; |
445 | } |
463 | } |
446 | |
464 | |
447 | sub _draw { |
465 | sub _draw { |
… | |
… | |
451 | |
469 | |
452 | my $tex = $self->{texture} |
470 | my $tex = $self->{texture} |
453 | or return; |
471 | or return; |
454 | |
472 | |
455 | glEnable GL_BLEND; |
473 | glEnable GL_BLEND; |
|
|
474 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
456 | glEnable GL_TEXTURE_2D; |
475 | glEnable GL_TEXTURE_2D; |
457 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
476 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
458 | |
477 | |
459 | $tex->draw_quad (0, 0, $w, $h); |
478 | $tex->draw_quad (0, 0, $w, $h); |
460 | |
479 | |
… | |
… | |
479 | |
498 | |
480 | map { $_ + 4 } $chld->size_request; |
499 | map { $_ + 4 } $chld->size_request; |
481 | } |
500 | } |
482 | |
501 | |
483 | sub size_allocate { |
502 | sub size_allocate { |
484 | my ($self, $w, $h) = @_; |
503 | my ($self, $x, $y, $w, $h) = @_; |
485 | |
504 | |
486 | return unless $self->{w} != $w || $self->{h} != $h; |
505 | $self->_size_allocate ($x, $y, $w, $h) or return; |
487 | |
506 | |
488 | $self->{w} = $w; |
|
|
489 | $self->{h} = $h; |
|
|
490 | |
|
|
491 | $self->child->size_allocate ($w - 4, $h - 4); |
507 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
492 | $self->child->move (2, 2); |
|
|
493 | } |
508 | } |
494 | |
509 | |
495 | sub _draw { |
510 | sub _draw { |
496 | my ($self) = @_; |
511 | my ($self) = @_; |
497 | |
512 | |
… | |
… | |
534 | |
549 | |
535 | ($w, $h) |
550 | ($w, $h) |
536 | } |
551 | } |
537 | |
552 | |
538 | sub size_allocate { |
553 | sub size_allocate { |
539 | my ($self, $w, $h) = @_; |
554 | my ($self, $x, $y, $w, $h) = @_; |
540 | |
555 | |
541 | return unless $self->{w} != $w || $self->{h} != $h; |
556 | $self->_size_allocate ($x, $y, $w, $h) or return; |
542 | |
|
|
543 | $self->SUPER::size_allocate ($w, $h); |
|
|
544 | |
557 | |
545 | $h -= $tex[1]->{h}; |
558 | $h -= $tex[1]->{h}; |
546 | $h -= $tex[4]->{h}; |
559 | $h -= $tex[4]->{h}; |
547 | $w -= $tex[2]->{w}; |
560 | $w -= $tex[2]->{w}; |
548 | $w -= $tex[3]->{w}; |
561 | $w -= $tex[3]->{w}; |
… | |
… | |
550 | $h = $h < 0 ? 0 : $h; |
563 | $h = $h < 0 ? 0 : $h; |
551 | $w = $w < 0 ? 0 : $w; |
564 | $w = $w < 0 ? 0 : $w; |
552 | |
565 | |
553 | my $child = $self->child; |
566 | my $child = $self->child; |
554 | |
567 | |
555 | $child->size_allocate ($w, $h); |
|
|
556 | $child->move ($tex[3]->{w}, $tex[1]->{h}); |
568 | $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); |
557 | } |
569 | } |
558 | |
570 | |
559 | sub _draw { |
571 | sub _draw { |
560 | my ($self) = @_; |
572 | my ($self) = @_; |
561 | |
573 | |
562 | my ($w, $h) = ($self->{w}, $self->{h}); |
574 | my ($w, $h) = ($self->{w}, $self->{h}); |
563 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
575 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
564 | |
576 | |
565 | glEnable GL_BLEND; |
577 | glEnable GL_BLEND; |
|
|
578 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
566 | glEnable GL_TEXTURE_2D; |
579 | glEnable GL_TEXTURE_2D; |
567 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
568 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
580 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
569 | |
581 | |
570 | my $top = $tex[1]; |
582 | my $top = $tex[1]; |
571 | $top->draw_quad (0, 0, $w, $top->{h}); |
583 | $top->draw_quad (0, 0, $w, $top->{h}); |
572 | |
584 | |
… | |
… | |
578 | |
590 | |
579 | my $bottom = $tex[4]; |
591 | my $bottom = $tex[4]; |
580 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
592 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
581 | |
593 | |
582 | my $bg = $tex[0]; |
594 | my $bg = $tex[0]; |
|
|
595 | |
583 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
596 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
584 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
597 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
585 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
598 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
586 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
599 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
587 | |
600 | |
588 | my $rep_x = $cw / $bg->{w}; |
601 | my $rep_x = $cw / $bg->{w}; |
589 | my $rep_y = $ch / $bg->{h}; |
602 | my $rep_y = $ch / $bg->{h}; |
590 | |
603 | |
591 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
604 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
592 | |
605 | |
|
|
606 | glDisable GL_TEXTURE_2D; |
593 | glDisable GL_BLEND; |
607 | glDisable GL_BLEND; |
594 | glDisable GL_TEXTURE_2D; |
|
|
595 | |
608 | |
596 | $self->child->draw; |
609 | $self->child->draw; |
597 | |
610 | |
598 | } |
611 | } |
599 | |
612 | |
600 | ############################################################################# |
613 | ############################################################################# |
601 | |
614 | |
602 | package CFClient::UI::Table; |
615 | package CFClient::UI::Table; |
603 | |
616 | |
604 | our @ISA = CFClient::UI::Base::; |
617 | our @ISA = CFClient::UI::Base::; |
|
|
618 | |
|
|
619 | use List::Util qw(max sum); |
605 | |
620 | |
606 | use SDL::OpenGL; |
621 | use SDL::OpenGL; |
607 | |
622 | |
608 | sub add { |
623 | sub add { |
609 | my ($self, $x, $y, $chld) = @_; |
624 | my ($self, $x, $y, $chld) = @_; |
610 | my $old_chld = $self->{children}[$y][$x]; |
|
|
611 | |
625 | |
612 | $self->{children}[$y][$x] = $chld; |
626 | $self->{children}[$y][$x] = $chld; |
613 | $chld->set_parent ($self); |
627 | $chld->set_parent ($self); |
|
|
628 | |
|
|
629 | $self->{w} = $self->{h} = -1; |
614 | $self->update; |
630 | $self->update; |
615 | } |
631 | } |
616 | |
632 | |
617 | sub max_row_height { |
633 | sub get_wh { |
618 | my ($self, $row) = @_; |
634 | my ($self) = @_; |
619 | |
635 | |
620 | my $hs = 0; |
636 | my (@w, @h); |
621 | for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { |
637 | |
|
|
638 | for my $y (0 .. $#{$self->{children}}) { |
622 | my $c = $self->{children}->[$row]->[$xi]; |
639 | my $row = $self->{children}[$y] |
623 | if ($c) { |
640 | or next; |
|
|
641 | |
|
|
642 | for my $x (0 .. $#$row) { |
|
|
643 | my $widget = $row->[$x] |
|
|
644 | or next; |
624 | my ($w, $h) = $c->size_request; |
645 | my ($w, $h) = $widget->size_request; |
625 | if ($hs < $h) { $hs = $h } |
646 | |
|
|
647 | $w[$x] = max $w[$x], $w; |
|
|
648 | $h[$y] = max $h[$y], $h; |
626 | } |
649 | } |
627 | } |
650 | } |
628 | return $hs; |
|
|
629 | } |
|
|
630 | |
651 | |
631 | sub max_col_width { |
652 | (\@w, \@h) |
|
|
653 | } |
|
|
654 | |
|
|
655 | sub size_request { |
632 | my ($self, $col) = @_; |
656 | my ($self) = @_; |
633 | |
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 | |
634 | my $ws = 0; |
686 | my $x = 0; |
635 | for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { |
687 | my $row_h = $hs->[$r]; |
636 | my $c = ($self->{children}->[$yi] || [])->[$col]; |
688 | |
637 | if ($c) { |
689 | for my $c (0 .. $#$row) { |
638 | my ($w, $h) = $c->size_request; |
690 | my $widget = $row->[$c] |
639 | 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; |
640 | } |
698 | } |
|
|
699 | |
|
|
700 | $y += $row_h; |
641 | } |
701 | } |
642 | return $ws; |
702 | |
643 | } |
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::; |
644 | |
736 | |
645 | sub size_request { |
737 | sub size_request { |
646 | my ($self) = @_; |
738 | my ($self) = @_; |
647 | |
739 | |
648 | my ($hs, $ws) = (0, 0); |
|
|
649 | |
|
|
650 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
|
|
651 | $hs += $self->max_row_height ($yi); |
|
|
652 | } |
|
|
653 | |
|
|
654 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
|
|
655 | my $wm = 0; |
|
|
656 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
|
|
657 | $wm += $self->max_col_width ($xi) |
|
|
658 | } |
|
|
659 | if ($ws < $wm) { $ws = $wm } |
|
|
660 | } |
|
|
661 | |
|
|
662 | return ($ws, $hs); |
|
|
663 | } |
|
|
664 | |
|
|
665 | sub _draw { |
|
|
666 | my ($self) = @_; |
|
|
667 | |
|
|
668 | my $y = 0; |
|
|
669 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
|
|
670 | my $x = 0; |
|
|
671 | |
|
|
672 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
|
|
673 | |
|
|
674 | my $c = $self->{children}->[$yi]->[$xi]; |
|
|
675 | if ($c) { |
|
|
676 | $c->move ($x, $y, 0); #TODO: Move to size_request |
|
|
677 | $c->draw if $c; |
|
|
678 | } |
|
|
679 | |
|
|
680 | $x += $self->max_col_width ($xi); |
|
|
681 | } |
|
|
682 | |
|
|
683 | $y += $self->max_row_height ($yi); |
|
|
684 | } |
|
|
685 | } |
|
|
686 | |
|
|
687 | ############################################################################# |
|
|
688 | |
|
|
689 | package CFClient::UI::VBox; |
|
|
690 | |
|
|
691 | our @ISA = CFClient::UI::Container::; |
|
|
692 | |
|
|
693 | use SDL::OpenGL; |
|
|
694 | |
|
|
695 | sub size_request { |
|
|
696 | my ($self) = @_; |
|
|
697 | |
|
|
698 | my @alloc = map [$_->size_request], @{$self->{children}}; |
740 | my @alloc = map [$_->size_request], @{$self->{children}}; |
699 | |
741 | |
700 | ( |
742 | ( |
701 | (List::Util::max map $_->[0], @alloc), |
|
|
702 | (List::Util::sum map $_->[1], @alloc), |
743 | (List::Util::sum map $_->[0], @alloc), |
|
|
744 | (List::Util::max map $_->[1], @alloc), |
703 | ) |
745 | ) |
704 | } |
746 | } |
705 | |
747 | |
706 | sub size_allocate { |
748 | sub size_allocate { |
707 | my ($self, $w, $h) = @_; |
749 | my ($self, $x, $y, $w, $h) = @_; |
708 | |
750 | |
709 | return unless $self->{w} != $w || $self->{h} != $h; |
751 | $self->_size_allocate ($x, $y, $w, $h) or return; |
710 | |
752 | |
711 | $self->{w} = $w; |
|
|
712 | $self->{h} = $h; |
|
|
713 | |
|
|
714 | return unless $self->{h}; |
753 | return unless $self->{w}; |
|
|
754 | |
|
|
755 | ($h, $w) = ($w, $h); |
715 | |
756 | |
716 | my $children = $self->{children}; |
757 | my $children = $self->{children}; |
717 | |
758 | |
718 | my @h = map +($_->size_request)[1], @$children; |
759 | my @h = map +($_->size_request)[0], @$children; |
719 | |
760 | |
720 | my $req_h = List::Util::sum @h; |
761 | my $req_h = List::Util::sum @h; |
721 | |
762 | |
722 | if ($req_h > $h) { |
763 | if ($req_h > $h) { |
723 | # ah well, not enough space |
764 | # ah well, not enough space |
… | |
… | |
739 | |
780 | |
740 | my $y = 0; |
781 | my $y = 0; |
741 | for (0 .. $#$children) { |
782 | for (0 .. $#$children) { |
742 | my $child = $children->[$_]; |
783 | my $child = $children->[$_]; |
743 | my $h = $h[$_]; |
784 | my $h = $h[$_]; |
744 | $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[$_]; |
745 | $child->size_allocate ($w, $h); |
845 | $child->size_allocate (0, $y, $w, $h); |
746 | |
846 | |
747 | $y += $h; |
847 | $y += $h; |
748 | } |
848 | } |
749 | } |
849 | } |
750 | |
850 | |
… | |
… | |
758 | |
858 | |
759 | sub new { |
859 | sub new { |
760 | my ($class, %arg) = @_; |
860 | my ($class, %arg) = @_; |
761 | |
861 | |
762 | my $self = $class->SUPER::new ( |
862 | my $self = $class->SUPER::new ( |
763 | fg => [1, 1, 1], |
863 | fg => [1, 1, 1], |
764 | height => $::FONTSIZE, |
864 | height => $::FONTSIZE, |
765 | text => "", |
865 | text => "", |
766 | align => -1, |
866 | align => -1, |
|
|
867 | padding => 2, |
767 | layout => new CFClient::Layout, |
868 | layout => new CFClient::Layout, |
768 | %arg |
869 | %arg |
769 | ); |
870 | ); |
770 | |
871 | |
771 | $self->set_text ($self->{text}); |
872 | $self->set_text ($self->{text}); |
772 | |
873 | |
… | |
… | |
802 | sub size_request { |
903 | sub size_request { |
803 | my ($self) = @_; |
904 | my ($self) = @_; |
804 | |
905 | |
805 | $self->{layout}->set_width; |
906 | $self->{layout}->set_width; |
806 | $self->{layout}->set_height ($self->{height}); |
907 | $self->{layout}->set_height ($self->{height}); |
807 | $self->{layout}->size |
908 | my ($w, $h) = $self->{layout}->size; |
808 | # if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
909 | |
809 | # ( |
910 | ( |
810 | # $self->{texture}{w}, |
911 | $w + $self->{padding} * 2, |
811 | # $self->{texture}{h}, |
912 | $h + $self->{padding} * 2, |
812 | # ) |
913 | ) |
813 | # } else { |
|
|
814 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h}; |
|
|
815 | # |
|
|
816 | # ($w, $h) |
|
|
817 | # } |
|
|
818 | } |
914 | } |
819 | |
915 | |
820 | sub size_allocate { |
916 | sub size_allocate { |
821 | my ($self, $w, $h) = @_; |
917 | my ($self, $x, $y, $w, $h) = @_; |
822 | |
918 | |
823 | return unless $self->{w} != $w || $self->{h} != $h; |
919 | $self->_size_allocate ($x, $y, $w, $h) or return; |
824 | |
920 | |
825 | $self->SUPER::size_allocate ($w, $h); |
|
|
826 | delete $self->{texture}; |
921 | delete $self->{texture}; |
827 | } |
922 | } |
828 | |
923 | |
829 | sub update { |
924 | sub update { |
830 | my ($self) = @_; |
925 | my ($self) = @_; |
… | |
… | |
836 | sub _draw { |
931 | sub _draw { |
837 | my ($self) = @_; |
932 | my ($self) = @_; |
838 | |
933 | |
839 | my $tex = $self->{texture} ||= do { |
934 | my $tex = $self->{texture} ||= do { |
840 | $self->{layout}->set_width ($self->{w}); |
935 | $self->{layout}->set_width ($self->{w}); |
841 | new_from_layout CFClient::Texture $self->{layout}; |
936 | new_from_layout CFClient::Texture $self->{layout} |
842 | }; |
937 | }; |
843 | |
938 | |
844 | glEnable GL_BLEND; |
939 | glEnable GL_BLEND; |
|
|
940 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
845 | glEnable GL_TEXTURE_2D; |
941 | glEnable GL_TEXTURE_2D; |
846 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
847 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
942 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
848 | |
943 | |
849 | glColor @{$self->{fg}}; |
944 | glColor @{$self->{fg}}; |
850 | |
945 | |
851 | my $x = |
946 | my $x = |
852 | $self->{align} < 0 ? 0 |
947 | $self->{align} < 0 ? $self->{padding} |
853 | : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} |
948 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
854 | : ($self->{w} + $self->{texture}{w}) * 0.5; |
949 | : ($self->{w} - $tex->{w}) * 0.5; |
855 | |
950 | |
856 | $tex->draw_quad ($x, 0); |
951 | $tex->draw_quad ($x, 0); |
857 | |
952 | |
|
|
953 | glDisable GL_TEXTURE_2D; |
858 | glDisable GL_BLEND; |
954 | glDisable GL_BLEND; |
859 | glDisable GL_TEXTURE_2D; |
|
|
860 | } |
955 | } |
861 | |
956 | |
862 | ############################################################################# |
957 | ############################################################################# |
863 | |
958 | |
864 | package CFClient::UI::Entry; |
959 | package CFClient::UI::Entry; |
… | |
… | |
871 | sub new { |
966 | sub new { |
872 | my $class = shift; |
967 | my $class = shift; |
873 | |
968 | |
874 | $class->SUPER::new ( |
969 | $class->SUPER::new ( |
875 | fg => [1, 1, 1], |
970 | fg => [1, 1, 1], |
876 | bg => [0, 0, 0, 0.4], |
971 | bg => [0, 0, 0, 0.2], |
877 | active_bg => [1, 1, 1], |
972 | active_bg => [1, 1, 1], |
878 | active_fg => [0, 0, 0], |
973 | active_fg => [0, 0, 0], |
879 | @_ |
974 | @_ |
880 | ) |
975 | ) |
881 | } |
976 | } |
… | |
… | |
888 | $self->{text} = $text; |
983 | $self->{text} = $text; |
889 | $self->{layout}->set_width ($self->{w}); |
984 | $self->{layout}->set_width ($self->{w}); |
890 | |
985 | |
891 | $text =~ s/./*/g if $self->{hidden}; |
986 | $text =~ s/./*/g if $self->{hidden}; |
892 | |
987 | |
893 | |
|
|
894 | $self->{layout}->set_markup ($self->escape_text ($text)); |
988 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
895 | |
989 | |
896 | $text = substr $text, 0, $self->{cursor}; |
990 | $text = substr $text, 0, $self->{cursor}; |
897 | utf8::encode $text; |
991 | utf8::encode $text; |
898 | |
992 | |
899 | @$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); |
… | |
… | |
906 | |
1000 | |
907 | ($w + 1, $h) # add 1 for cursor |
1001 | ($w + 1, $h) # add 1 for cursor |
908 | } |
1002 | } |
909 | |
1003 | |
910 | sub size_allocate { |
1004 | sub size_allocate { |
911 | my ($self, $w, $h) = @_; |
1005 | my ($self, $x, $y, $w, $h) = @_; |
912 | |
1006 | |
913 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
914 | |
|
|
915 | $self->SUPER::size_allocate ($w, $h); |
1007 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
916 | |
1008 | |
917 | $self->_set_text ($self->{text}); |
1009 | $self->_set_text ($self->{text}); |
918 | } |
1010 | } |
919 | |
1011 | |
920 | sub set_text { |
1012 | sub set_text { |
… | |
… | |
941 | substr $text, $self->{cursor}, 1, ""; |
1033 | substr $text, $self->{cursor}, 1, ""; |
942 | } elsif ($sym == SDLK_LEFT) { |
1034 | } elsif ($sym == SDLK_LEFT) { |
943 | --$self->{cursor} if $self->{cursor}; |
1035 | --$self->{cursor} if $self->{cursor}; |
944 | } elsif ($sym == SDLK_RIGHT) { |
1036 | } elsif ($sym == SDLK_RIGHT) { |
945 | ++$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; |
946 | } elsif ($uni) { |
1042 | } elsif ($uni) { |
947 | substr $text, $self->{cursor}++, 0, chr $uni; |
1043 | substr $text, $self->{cursor}++, 0, chr $uni; |
948 | } |
1044 | } |
949 | |
1045 | |
950 | $self->_set_text ($text); |
1046 | $self->_set_text ($text); |
… | |
… | |
965 | $self->SUPER::button_down ($ev, $x, $y); |
1061 | $self->SUPER::button_down ($ev, $x, $y); |
966 | |
1062 | |
967 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1063 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
968 | |
1064 | |
969 | # byte-index to char-index |
1065 | # byte-index to char-index |
970 | my $text = $self->{layout}; |
1066 | my $text = $self->{text}; |
971 | utf8::encode $text; |
1067 | utf8::encode $text; |
972 | $self->{cursor} = length substr $text, 0, $idx; |
1068 | $self->{cursor} = length substr $text, 0, $idx; |
973 | |
1069 | |
974 | $self->_set_text ($self->{text}); |
1070 | $self->_set_text ($self->{text}); |
975 | $self->update; |
1071 | $self->update; |
… | |
… | |
990 | $self->{fg} = $self->{active_fg}; |
1086 | $self->{fg} = $self->{active_fg}; |
991 | } else { |
1087 | } else { |
992 | glColor @{$self->{bg}}; |
1088 | glColor @{$self->{bg}}; |
993 | } |
1089 | } |
994 | |
1090 | |
|
|
1091 | glEnable GL_BLEND; |
|
|
1092 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
995 | glBegin GL_QUADS; |
1093 | glBegin GL_QUADS; |
996 | glVertex 0 , 0; |
1094 | glVertex 0 , 0; |
997 | glVertex 0 , $self->{h}; |
1095 | glVertex 0 , $self->{h}; |
998 | glVertex $self->{w}, $self->{h}; |
1096 | glVertex $self->{w}, $self->{h}; |
999 | glVertex $self->{w}, 0; |
1097 | glVertex $self->{w}, 0; |
1000 | glEnd; |
1098 | glEnd; |
|
|
1099 | glDisable GL_BLEND; |
1001 | |
1100 | |
1002 | $self->SUPER::_draw; |
1101 | $self->SUPER::_draw; |
1003 | |
1102 | |
1004 | #TODO: force update every cursor change :( |
1103 | #TODO: force update every cursor change :( |
1005 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1104 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
… | |
… | |
1016 | package CFClient::UI::Slider; |
1115 | package CFClient::UI::Slider; |
1017 | |
1116 | |
1018 | use strict; |
1117 | use strict; |
1019 | |
1118 | |
1020 | use SDL::OpenGL; |
1119 | use SDL::OpenGL; |
1021 | use SDL::OpenGL::Constants; |
|
|
1022 | |
1120 | |
1023 | our @ISA = CFClient::UI::DrawBG::; |
1121 | our @ISA = CFClient::UI::DrawBG::; |
1024 | |
|
|
1025 | sub size_request { |
|
|
1026 | my ($self) = @_; |
|
|
1027 | |
|
|
1028 | my $w = 50; |
|
|
1029 | my $h = 10; |
|
|
1030 | |
|
|
1031 | $self->{vertical} ? ($h, $w) : ($w, $h) |
|
|
1032 | } |
|
|
1033 | |
1122 | |
1034 | sub new { |
1123 | sub new { |
1035 | my $class = shift; |
1124 | my $class = shift; |
1036 | |
1125 | |
1037 | # range [value, low, high, page] |
1126 | # range [value, low, high, page] |
1038 | |
1127 | |
1039 | $class->SUPER::new ( |
1128 | my $self = $class->SUPER::new ( |
1040 | fg => [1, 1, 1], |
1129 | fg => [1, 1, 1], |
1041 | active_fg => [0, 0, 0], |
1130 | active_fg => [0, 0, 0], |
1042 | range => [0, 0, 100, 10], |
1131 | range => [0, 0, 100, 10], |
|
|
1132 | req_w => 40, |
|
|
1133 | req_h => 10, |
1043 | vertical => 1, |
1134 | vertical => 0, |
1044 | @_ |
1135 | @_ |
1045 | ) |
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) |
1046 | } |
1148 | } |
1047 | |
1149 | |
1048 | sub button_down { |
1150 | sub button_down { |
1049 | my ($self, $ev, $x, $y) = @_; |
1151 | my ($self, $ev, $x, $y) = @_; |
1050 | |
1152 | |
… | |
… | |
1133 | |
1235 | |
1134 | use List::Util qw(min max); |
1236 | use List::Util qw(min max); |
1135 | |
1237 | |
1136 | use SDL; |
1238 | use SDL; |
1137 | use SDL::OpenGL; |
1239 | use SDL::OpenGL; |
1138 | use SDL::OpenGL::Constants; |
|
|
1139 | |
1240 | |
1140 | our @ISA = CFClient::UI::Base::; |
1241 | our @ISA = CFClient::UI::Base::; |
1141 | |
1242 | |
1142 | sub new { |
1243 | sub new { |
1143 | my $class = shift; |
1244 | my $class = shift; |
… | |
… | |
1165 | |
1266 | |
1166 | sub update { |
1267 | sub update { |
1167 | my ($self) = @_; |
1268 | my ($self) = @_; |
1168 | |
1269 | |
1169 | $self->{need_update} = 1; |
1270 | $self->{need_update} = 1; |
|
|
1271 | $self->SUPER::update; |
1170 | } |
1272 | } |
1171 | |
1273 | |
1172 | sub _draw { |
1274 | sub _draw { |
1173 | my ($self) = @_; |
1275 | my ($self) = @_; |
1174 | |
1276 | |
… | |
… | |
1197 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
1299 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
1198 | } |
1300 | } |
1199 | |
1301 | |
1200 | glEnable GL_TEXTURE_2D; |
1302 | glEnable GL_TEXTURE_2D; |
1201 | glEnable GL_BLEND; |
1303 | glEnable GL_BLEND; |
|
|
1304 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1202 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1305 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1203 | |
1306 | |
1204 | my $sw4 = ($sw + 3) & ~3; |
1307 | my $sw4 = ($sw + 3) & ~3; |
1205 | my $darkness = "\x00" x ($sw4 * $sh); |
1308 | my $darkness = "\x00" x ($sw4 * $sh); |
1206 | |
1309 | |
… | |
… | |
1236 | # |
1339 | # |
1237 | # $lighting = $pb->get_pixels; |
1340 | # $lighting = $pb->get_pixels; |
1238 | # $lighting =~ s/(.)../$1/gs; |
1341 | # $lighting =~ s/(.)../$1/gs; |
1239 | # } |
1342 | # } |
1240 | |
1343 | |
1241 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1242 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1344 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1243 | |
1345 | |
1244 | $darkness = new CFClient::Texture |
1346 | $darkness = new CFClient::Texture |
1245 | width => $sw4, |
1347 | w => $sw4, |
1246 | height => $sh, |
1348 | h => $sh, |
1247 | data => $darkness, |
1349 | data => $darkness, |
1248 | internalformat => GL_ALPHA, |
1350 | internalformat => GL_ALPHA, |
1249 | format => GL_ALPHA; |
1351 | format => GL_ALPHA; |
1250 | |
1352 | |
1251 | glColor 0.45, 0.45, 0.45, 1; |
1353 | glColor 0.45, 0.45, 0.45, 1; |
… | |
… | |
1362 | sub size_request { |
1464 | sub size_request { |
1363 | ($::WIDTH, $::HEIGHT) |
1465 | ($::WIDTH, $::HEIGHT) |
1364 | } |
1466 | } |
1365 | |
1467 | |
1366 | sub size_allocate { |
1468 | sub size_allocate { |
1367 | my ($self, $w, $h) = @_; |
1469 | my ($self, $x, $y, $w, $h) = @_; |
1368 | |
1470 | |
1369 | $self->SUPER::size_allocate ($w, $h); |
1471 | $self->_size_allocate ($x, $y, $w, $h); |
1370 | |
1472 | |
1371 | $_->size_allocate ($_->size_request) |
1473 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
1372 | for @{$self->{children}}; |
1474 | for @{$self->{children}}; |
1373 | } |
1475 | } |
1374 | |
1476 | |
1375 | sub translate { |
1477 | sub translate { |
1376 | my ($self, $x, $y) = @_; |
1478 | my ($self, $x, $y) = @_; |
… | |
… | |
1379 | } |
1481 | } |
1380 | |
1482 | |
1381 | sub update { |
1483 | sub update { |
1382 | my ($self) = @_; |
1484 | my ($self) = @_; |
1383 | |
1485 | |
1384 | $self->size_allocate ($self->size_request); |
1486 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
1385 | ::refresh (); |
1487 | ::refresh (); |
1386 | } |
1488 | } |
1387 | |
1489 | |
1388 | sub add { |
1490 | sub add { |
1389 | my ($self, $widget) = @_; |
1491 | my ($self, $widget) = @_; |
1390 | |
1492 | |
1391 | $self->SUPER::add ($widget); |
1493 | $self->SUPER::add ($widget); |
1392 | |
1494 | |
1393 | $widget->size_allocate ($widget->size_request); |
1495 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
1394 | } |
1496 | } |
1395 | |
1497 | |
1396 | sub draw { |
1498 | sub draw { |
1397 | my ($self) = @_; |
1499 | my ($self) = @_; |
1398 | |
1500 | |