… | |
… | |
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; |
… | |
… | |
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; |
… | |
… | |
310 | |
330 | |
311 | $self |
331 | $self |
312 | } |
332 | } |
313 | |
333 | |
314 | sub add { |
334 | sub add { |
315 | my ($self, $chld, $expand) = @_; |
335 | my ($self, $chld) = @_; |
316 | |
336 | |
317 | $chld->{expand} = $expand; |
|
|
318 | $chld->set_parent ($self); |
337 | $chld->set_parent ($self); |
319 | |
338 | |
320 | $self->{children} = [ |
339 | $self->{children} = [ |
321 | sort { $a->{z} <=> $b->{z} } |
340 | sort { $a->{z} <=> $b->{z} } |
322 | @{$self->{children}}, $chld |
341 | @{$self->{children}}, $chld |
323 | ]; |
342 | ]; |
324 | |
343 | |
325 | $self->size_allocate ($self->{w}, $self->{h}) |
344 | $self->{w} = $self->{h} = -1; |
326 | if $self->{w}; #TODO: check for "realised state" |
345 | $self->update; |
327 | } |
346 | } |
328 | |
347 | |
329 | sub remove { |
348 | sub remove { |
330 | my ($self, $widget) = @_; |
349 | my ($self, $widget) = @_; |
331 | |
350 | |
332 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
351 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
333 | |
352 | |
334 | $self->size_allocate ($self->{w}, $self->{h}); |
353 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
335 | } |
354 | } |
336 | |
355 | |
337 | sub find_widget { |
356 | sub find_widget { |
338 | my ($self, $x, $y) = @_; |
357 | my ($self, $x, $y) = @_; |
339 | |
358 | |
… | |
… | |
392 | sub size_request { |
411 | sub size_request { |
393 | $_[0]{children}[0]->size_request |
412 | $_[0]{children}[0]->size_request |
394 | } |
413 | } |
395 | |
414 | |
396 | sub size_allocate { |
415 | sub size_allocate { |
397 | my ($self, $w, $h) = @_; |
416 | my ($self, $x, $y, $w, $h) = @_; |
398 | |
417 | |
399 | return unless $self->{w} != $w || $self->{h} != $h; |
418 | $self->_size_allocate ($x, $y, $w, $h) or return; |
400 | |
419 | |
401 | $self->SUPER::size_allocate ($w, $h); |
|
|
402 | $self->{children}[0]->size_allocate ($w, $h); |
420 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
403 | } |
421 | } |
404 | |
422 | |
405 | ############################################################################# |
423 | ############################################################################# |
406 | |
424 | |
407 | package CFClient::UI::Window; |
425 | package CFClient::UI::Window; |
… | |
… | |
432 | $self->{w}, $self->{h}, sub { $self->child->draw } |
450 | $self->{w}, $self->{h}, sub { $self->child->draw } |
433 | ); |
451 | ); |
434 | } |
452 | } |
435 | |
453 | |
436 | sub size_allocate { |
454 | sub size_allocate { |
437 | my ($self, $w, $h) = @_; |
455 | my ($self, $x, $y, $w, $h) = @_; |
438 | |
456 | |
439 | return unless $self->{w} != $w || $self->{h} != $h; |
457 | $self->_size_allocate ($x, $y, $w, $h) or return; |
440 | |
458 | |
441 | $self->{w} = $w; |
|
|
442 | $self->{h} = $h; |
|
|
443 | |
|
|
444 | $self->child->size_allocate ($w, $h); |
459 | $self->child->size_allocate (0, 0, $w, $h); |
445 | |
460 | |
446 | $self->render_chld; |
461 | $self->render_chld; |
447 | } |
462 | } |
448 | |
463 | |
449 | sub _draw { |
464 | sub _draw { |
… | |
… | |
482 | |
497 | |
483 | map { $_ + 4 } $chld->size_request; |
498 | map { $_ + 4 } $chld->size_request; |
484 | } |
499 | } |
485 | |
500 | |
486 | sub size_allocate { |
501 | sub size_allocate { |
487 | my ($self, $w, $h) = @_; |
502 | my ($self, $x, $y, $w, $h) = @_; |
488 | |
503 | |
489 | return unless $self->{w} != $w || $self->{h} != $h; |
504 | $self->_size_allocate ($x, $y, $w, $h) or return; |
490 | |
505 | |
491 | $self->{w} = $w; |
|
|
492 | $self->{h} = $h; |
|
|
493 | |
|
|
494 | $self->child->size_allocate ($w - 4, $h - 4); |
506 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
495 | $self->child->move (2, 2); |
|
|
496 | } |
507 | } |
497 | |
508 | |
498 | sub _draw { |
509 | sub _draw { |
499 | my ($self) = @_; |
510 | my ($self) = @_; |
500 | |
511 | |
… | |
… | |
526 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
537 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
527 | |
538 | |
528 | sub size_request { |
539 | sub size_request { |
529 | my ($self) = @_; |
540 | my ($self) = @_; |
530 | |
541 | |
|
|
542 | my ($w, $h) = |
|
|
543 | $self->{user_w} && $self->{user_h} |
|
|
544 | ? ($self->{user_w}, $self->{user_h}) |
531 | my ($w, $h) = $self->SUPER::size_request; |
545 | : ($self->SUPER::size_request); |
532 | |
546 | |
533 | $h += $tex[1]->{h}; |
547 | $h += $tex[1]->{h}; |
534 | $h += $tex[4]->{h}; |
548 | $h += $tex[4]->{h}; |
535 | $w += $tex[2]->{w}; |
549 | $w += $tex[2]->{w}; |
536 | $w += $tex[3]->{w}; |
550 | $w += $tex[3]->{w}; |
537 | |
551 | |
538 | ($w, $h) |
552 | ($w, $h) |
539 | } |
553 | } |
540 | |
554 | |
541 | sub size_allocate { |
555 | sub size_allocate { |
542 | my ($self, $w, $h) = @_; |
556 | my ($self, $x, $y, $w, $h) = @_; |
543 | |
557 | |
544 | return unless $self->{w} != $w || $self->{h} != $h; |
558 | $self->_size_allocate ($x, $y, $w, $h) or return; |
545 | |
559 | |
546 | $self->SUPER::size_allocate ($w, $h); |
|
|
547 | |
|
|
548 | $h -= $tex[1]->{h}; |
560 | $h -= $tex[1]{h}; |
549 | $h -= $tex[4]->{h}; |
561 | $h -= $tex[4]{h}; |
550 | $w -= $tex[2]->{w}; |
562 | $w -= $tex[2]{w}; |
551 | $w -= $tex[3]->{w}; |
563 | $w -= $tex[3]{w}; |
552 | |
564 | |
553 | $h = $h < 0 ? 0 : $h; |
565 | $h = $h < 0 ? 0 : $h; |
554 | $w = $w < 0 ? 0 : $w; |
566 | $w = $w < 0 ? 0 : $w; |
555 | |
567 | |
556 | my $child = $self->child; |
568 | my $child = $self->child; |
557 | |
569 | |
558 | $child->size_allocate ($w, $h); |
|
|
559 | $child->move ($tex[3]->{w}, $tex[1]->{h}); |
570 | $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); |
|
|
571 | } |
|
|
572 | |
|
|
573 | sub button_down { |
|
|
574 | my ($self, $ev, $x, $y) = @_; |
|
|
575 | |
|
|
576 | if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w} |
|
|
577 | && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) { |
|
|
578 | |
|
|
579 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
580 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
|
|
581 | |
|
|
582 | $self->{motion} = sub { |
|
|
583 | my ($ev, $x, $y) = @_; |
|
|
584 | |
|
|
585 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
586 | |
|
|
587 | $self->{user_w} = $bw + $x - $ox; |
|
|
588 | $self->{user_h} = $bh + $y - $oy; |
|
|
589 | $self->update; |
|
|
590 | }; |
|
|
591 | |
|
|
592 | } elsif ($x >= 0 && $x < $self->{w} |
|
|
593 | && $y >= 0 && $y < $tex[1]{h}) { |
|
|
594 | |
|
|
595 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
596 | my ($bx, $by) = ($self->{x}, $self->{y}); |
|
|
597 | |
|
|
598 | $self->{motion} = sub { |
|
|
599 | my ($ev, $x, $y) = @_; |
|
|
600 | |
|
|
601 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
602 | |
|
|
603 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
|
|
604 | $self->update; |
|
|
605 | }; |
|
|
606 | } |
|
|
607 | } |
|
|
608 | |
|
|
609 | sub button_up { |
|
|
610 | my ($self, $ev, $x, $y) = @_; |
|
|
611 | |
|
|
612 | delete $self->{motion}; |
|
|
613 | } |
|
|
614 | |
|
|
615 | sub mouse_motion { |
|
|
616 | my ($self, $ev, $x, $y) = @_; |
|
|
617 | |
|
|
618 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
560 | } |
619 | } |
561 | |
620 | |
562 | sub _draw { |
621 | sub _draw { |
563 | my ($self) = @_; |
622 | my ($self) = @_; |
564 | |
623 | |
… | |
… | |
581 | |
640 | |
582 | my $bottom = $tex[4]; |
641 | my $bottom = $tex[4]; |
583 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
642 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
584 | |
643 | |
585 | my $bg = $tex[0]; |
644 | my $bg = $tex[0]; |
|
|
645 | |
586 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
646 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
587 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
647 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
588 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
648 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
589 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
649 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
590 | |
650 | |
591 | my $rep_x = $cw / $bg->{w}; |
651 | my $rep_x = $cw / $bg->{w}; |
592 | my $rep_y = $ch / $bg->{h}; |
652 | my $rep_y = $ch / $bg->{h}; |
593 | |
653 | |
594 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
654 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
595 | |
655 | |
|
|
656 | glDisable GL_TEXTURE_2D; |
596 | glDisable GL_BLEND; |
657 | glDisable GL_BLEND; |
597 | glDisable GL_TEXTURE_2D; |
|
|
598 | |
658 | |
599 | $self->child->draw; |
659 | $self->child->draw; |
600 | |
660 | |
601 | } |
661 | } |
602 | |
662 | |
603 | ############################################################################# |
663 | ############################################################################# |
604 | |
664 | |
605 | package CFClient::UI::Table; |
665 | package CFClient::UI::Table; |
606 | |
666 | |
607 | our @ISA = CFClient::UI::Base::; |
667 | our @ISA = CFClient::UI::Base::; |
|
|
668 | |
|
|
669 | use List::Util qw(max sum); |
608 | |
670 | |
609 | use SDL::OpenGL; |
671 | use SDL::OpenGL; |
610 | |
672 | |
611 | sub add { |
673 | sub add { |
612 | my ($self, $x, $y, $chld) = @_; |
674 | my ($self, $x, $y, $chld) = @_; |
613 | my $old_chld = $self->{children}[$y][$x]; |
|
|
614 | |
675 | |
615 | $self->{children}[$y][$x] = $chld; |
676 | $self->{children}[$y][$x] = $chld; |
616 | $chld->set_parent ($self); |
677 | $chld->set_parent ($self); |
|
|
678 | |
|
|
679 | $self->{w} = $self->{h} = -1; |
617 | $self->update; |
680 | $self->update; |
618 | } |
681 | } |
619 | |
682 | |
620 | sub max_row_height { |
683 | sub get_wh { |
621 | my ($self, $row) = @_; |
684 | my ($self) = @_; |
622 | |
685 | |
623 | my $hs = 0; |
686 | my (@w, @h); |
624 | for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { |
687 | |
|
|
688 | for my $y (0 .. $#{$self->{children}}) { |
625 | my $c = $self->{children}->[$row]->[$xi]; |
689 | my $row = $self->{children}[$y] |
626 | if ($c) { |
690 | or next; |
|
|
691 | |
|
|
692 | for my $x (0 .. $#$row) { |
|
|
693 | my $widget = $row->[$x] |
|
|
694 | or next; |
627 | my ($w, $h) = $c->size_request; |
695 | my ($w, $h) = $widget->size_request; |
628 | if ($hs < $h) { $hs = $h } |
696 | |
|
|
697 | $w[$x] = max $w[$x], $w; |
|
|
698 | $h[$y] = max $h[$y], $h; |
629 | } |
699 | } |
630 | } |
700 | } |
631 | return $hs; |
|
|
632 | } |
|
|
633 | |
701 | |
634 | sub max_col_width { |
702 | (\@w, \@h) |
|
|
703 | } |
|
|
704 | |
|
|
705 | sub size_request { |
635 | my ($self, $col) = @_; |
706 | my ($self) = @_; |
636 | |
707 | |
|
|
708 | my ($ws, $hs) = $self->get_wh; |
|
|
709 | |
|
|
710 | ( |
|
|
711 | (List::Util::sum @$ws), |
|
|
712 | (List::Util::sum @$hs), |
|
|
713 | ) |
|
|
714 | } |
|
|
715 | |
|
|
716 | sub size_allocate { |
|
|
717 | my ($self, $x, $y, $w, $h) = @_; |
|
|
718 | |
|
|
719 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
720 | |
|
|
721 | my ($ws, $hs) = $self->get_wh; |
|
|
722 | |
|
|
723 | my $req_w = List::Util::sum @$ws; |
|
|
724 | my $req_h = List::Util::sum @$hs; |
|
|
725 | |
|
|
726 | # linearly scale sizes |
|
|
727 | $_ *= $w / $req_w for @$ws; |
|
|
728 | $_ *= $h / $req_h for @$hs; |
|
|
729 | |
|
|
730 | my $y; |
|
|
731 | |
|
|
732 | for my $r (0 .. $#{$self->{children}}) { |
|
|
733 | my $row = $self->{children}[$r] |
|
|
734 | or next; |
|
|
735 | |
637 | my $ws = 0; |
736 | my $x = 0; |
638 | for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { |
737 | my $row_h = $hs->[$r]; |
639 | my $c = ($self->{children}->[$yi] || [])->[$col]; |
738 | |
640 | if ($c) { |
739 | for my $c (0 .. $#$row) { |
641 | my ($w, $h) = $c->size_request; |
740 | my $widget = $row->[$c] |
642 | if ($ws < $w) { $ws = $w } |
741 | or next; |
|
|
742 | |
|
|
743 | my $col_w = $ws->[$c]; |
|
|
744 | |
|
|
745 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
|
|
746 | |
|
|
747 | $x += $col_w; |
643 | } |
748 | } |
|
|
749 | |
|
|
750 | $y += $row_h; |
644 | } |
751 | } |
645 | return $ws; |
752 | |
646 | } |
753 | } |
|
|
754 | |
|
|
755 | sub find_widget { |
|
|
756 | my ($self, $x, $y) = @_; |
|
|
757 | |
|
|
758 | $x -= $self->{x}; |
|
|
759 | $y -= $self->{y}; |
|
|
760 | |
|
|
761 | my $res; |
|
|
762 | |
|
|
763 | for (grep $_, map @$_, grep $_, @{ $self->{children} }) { |
|
|
764 | $res = $_->find_widget ($x, $y) |
|
|
765 | and return $res; |
|
|
766 | } |
|
|
767 | |
|
|
768 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
|
|
769 | } |
|
|
770 | |
|
|
771 | sub _draw { |
|
|
772 | my ($self) = @_; |
|
|
773 | |
|
|
774 | for (grep $_, @{$self->{children}}) { |
|
|
775 | $_->draw for grep $_, @$_; |
|
|
776 | } |
|
|
777 | } |
|
|
778 | |
|
|
779 | ############################################################################# |
|
|
780 | |
|
|
781 | package CFClient::UI::HBox; |
|
|
782 | |
|
|
783 | # TODO: wrap into common Box base class |
|
|
784 | |
|
|
785 | our @ISA = CFClient::UI::Container::; |
647 | |
786 | |
648 | sub size_request { |
787 | sub size_request { |
649 | my ($self) = @_; |
788 | my ($self) = @_; |
650 | |
789 | |
|
|
790 | my @alloc = map [$_->size_request], @{$self->{children}}; |
|
|
791 | |
|
|
792 | ( |
|
|
793 | (List::Util::sum map $_->[0], @alloc), |
|
|
794 | (List::Util::max map $_->[1], @alloc), |
|
|
795 | ) |
|
|
796 | } |
|
|
797 | |
|
|
798 | sub size_allocate { |
|
|
799 | my ($self, $x, $y, $w, $h) = @_; |
|
|
800 | |
|
|
801 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
802 | |
|
|
803 | return unless $self->{w}; |
|
|
804 | |
651 | my ($hs, $ws) = (0, 0); |
805 | ($h, $w) = ($w, $h); |
652 | |
806 | |
653 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
807 | my $children = $self->{children}; |
654 | $hs += $self->max_row_height ($yi); |
|
|
655 | } |
|
|
656 | |
808 | |
657 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
809 | my @h = map +($_->size_request)[0], @$children; |
658 | my $wm = 0; |
810 | |
659 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
811 | my $req_h = List::Util::sum @h; |
660 | $wm += $self->max_col_width ($xi) |
812 | |
|
|
813 | if ($req_h > $h) { |
|
|
814 | # ah well, not enough space |
|
|
815 | $_ = $h[$_] * $h / $req_h for @h; |
|
|
816 | } else { |
|
|
817 | my $exp = List::Util::sum map $_->{expand}, @$children; |
|
|
818 | $exp ||= 1; |
|
|
819 | |
|
|
820 | for (0 .. $#$children) { |
|
|
821 | my $child = $children->[$_]; |
|
|
822 | |
|
|
823 | my $alloc_h = $h[$_]; |
|
|
824 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
|
|
825 | $h[$_] = $alloc_h; |
661 | } |
826 | } |
662 | if ($ws < $wm) { $ws = $wm } |
|
|
663 | } |
827 | } |
664 | |
|
|
665 | return ($ws, $hs); |
|
|
666 | } |
|
|
667 | |
|
|
668 | sub _draw { |
|
|
669 | my ($self) = @_; |
|
|
670 | |
828 | |
671 | my $y = 0; |
829 | my $y = 0; |
672 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
830 | for (0 .. $#$children) { |
|
|
831 | my $child = $children->[$_]; |
673 | my $x = 0; |
832 | my $h = $h[$_]; |
|
|
833 | $child->size_allocate ($y, 0, $h, $w); |
674 | |
834 | |
675 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
835 | $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 | } |
836 | } |
688 | } |
837 | } |
689 | |
838 | |
690 | ############################################################################# |
839 | ############################################################################# |
691 | |
840 | |
692 | package CFClient::UI::VBox; |
841 | package CFClient::UI::VBox; |
693 | |
842 | |
|
|
843 | # TODO: wrap into common Box base class |
|
|
844 | |
694 | our @ISA = CFClient::UI::Container::; |
845 | our @ISA = CFClient::UI::Container::; |
695 | |
|
|
696 | use SDL::OpenGL; |
|
|
697 | |
846 | |
698 | sub size_request { |
847 | sub size_request { |
699 | my ($self) = @_; |
848 | my ($self) = @_; |
700 | |
849 | |
701 | my @alloc = map [$_->size_request], @{$self->{children}}; |
850 | my @alloc = map [$_->size_request], @{$self->{children}}; |
… | |
… | |
705 | (List::Util::sum map $_->[1], @alloc), |
854 | (List::Util::sum map $_->[1], @alloc), |
706 | ) |
855 | ) |
707 | } |
856 | } |
708 | |
857 | |
709 | sub size_allocate { |
858 | sub size_allocate { |
710 | my ($self, $w, $h) = @_; |
859 | my ($self, $x, $y, $w, $h) = @_; |
711 | |
860 | |
712 | return unless $self->{w} != $w || $self->{h} != $h; |
861 | $self->_size_allocate ($x, $y, $w, $h) or return; |
713 | |
|
|
714 | $self->{w} = $w; |
|
|
715 | $self->{h} = $h; |
|
|
716 | |
862 | |
717 | return unless $self->{h}; |
863 | return unless $self->{h}; |
718 | |
864 | |
719 | my $children = $self->{children}; |
865 | my $children = $self->{children}; |
720 | |
866 | |
… | |
… | |
724 | |
870 | |
725 | if ($req_h > $h) { |
871 | if ($req_h > $h) { |
726 | # ah well, not enough space |
872 | # ah well, not enough space |
727 | $_ = $h[$_] * $h / $req_h for @h; |
873 | $_ = $h[$_] * $h / $req_h for @h; |
728 | } else { |
874 | } else { |
729 | my @exp = grep $_->{expand}, @$children; |
875 | my $exp = List::Util::sum map $_->{expand}, @$children; |
730 | @exp = @$children unless @exp; |
876 | $exp ||= 1; |
731 | |
877 | |
732 | my %exp = map +($_ => 1), @exp; |
|
|
733 | |
|
|
734 | for (0 .. $#$children) { |
878 | for (0 .. $#$children) { |
735 | my $child = $children->[$_]; |
879 | my $child = $children->[$_]; |
736 | |
880 | |
737 | my $alloc_h = $h[$_]; |
881 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
738 | $alloc_h += ($h - $req_h) / @exp if $exp{$child}; |
|
|
739 | $h[$_] = $alloc_h; |
|
|
740 | } |
882 | } |
741 | } |
883 | } |
742 | |
884 | |
743 | my $y = 0; |
885 | my $y = 0; |
744 | for (0 .. $#$children) { |
886 | for (0 .. $#$children) { |
745 | my $child = $children->[$_]; |
887 | my $child = $children->[$_]; |
746 | my $h = $h[$_]; |
888 | my $h = $h[$_]; |
747 | $child->move (0, $y); |
|
|
748 | $child->size_allocate ($w, $h); |
889 | $child->size_allocate (0, $y, $w, $h); |
749 | |
890 | |
750 | $y += $h; |
891 | $y += $h; |
751 | } |
892 | } |
752 | } |
893 | } |
753 | |
894 | |
… | |
… | |
761 | |
902 | |
762 | sub new { |
903 | sub new { |
763 | my ($class, %arg) = @_; |
904 | my ($class, %arg) = @_; |
764 | |
905 | |
765 | my $self = $class->SUPER::new ( |
906 | my $self = $class->SUPER::new ( |
766 | fg => [1, 1, 1], |
907 | fg => [1, 1, 1], |
767 | height => $::FONTSIZE, |
908 | height => $::FONTSIZE, |
768 | text => "", |
909 | text => "", |
769 | align => -1, |
910 | align => -1, |
|
|
911 | padding => 2, |
770 | layout => new CFClient::Layout, |
912 | layout => new CFClient::Layout, |
771 | %arg |
913 | %arg |
772 | ); |
914 | ); |
773 | |
915 | |
774 | $self->set_text ($self->{text}); |
916 | $self->set_text ($self->{text}); |
775 | |
917 | |
… | |
… | |
805 | sub size_request { |
947 | sub size_request { |
806 | my ($self) = @_; |
948 | my ($self) = @_; |
807 | |
949 | |
808 | $self->{layout}->set_width; |
950 | $self->{layout}->set_width; |
809 | $self->{layout}->set_height ($self->{height}); |
951 | $self->{layout}->set_height ($self->{height}); |
810 | $self->{layout}->size |
952 | my ($w, $h) = $self->{layout}->size; |
811 | # if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
953 | |
812 | # ( |
954 | ( |
813 | # $self->{texture}{w}, |
955 | $w + $self->{padding} * 2, |
814 | # $self->{texture}{h}, |
956 | $h + $self->{padding} * 2, |
815 | # ) |
957 | ) |
816 | # } else { |
|
|
817 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h}; |
|
|
818 | # |
|
|
819 | # ($w, $h) |
|
|
820 | # } |
|
|
821 | } |
958 | } |
822 | |
959 | |
823 | sub size_allocate { |
960 | sub size_allocate { |
824 | my ($self, $w, $h) = @_; |
961 | my ($self, $x, $y, $w, $h) = @_; |
825 | |
962 | |
826 | return unless $self->{w} != $w || $self->{h} != $h; |
963 | $self->_size_allocate ($x, $y, $w, $h) or return; |
827 | |
964 | |
828 | $self->SUPER::size_allocate ($w, $h); |
|
|
829 | delete $self->{texture}; |
965 | delete $self->{texture}; |
830 | } |
966 | } |
831 | |
967 | |
832 | sub update { |
968 | sub update { |
833 | my ($self) = @_; |
969 | my ($self) = @_; |
… | |
… | |
850 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
986 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
851 | |
987 | |
852 | glColor @{$self->{fg}}; |
988 | glColor @{$self->{fg}}; |
853 | |
989 | |
854 | my $x = |
990 | my $x = |
855 | $self->{align} < 0 ? 0 |
991 | $self->{align} < 0 ? $self->{padding} |
856 | : $self->{align} > 0 ? $self->{w} - $tex->{w} |
992 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
857 | : ($self->{w} - $tex->{w}) * 0.5; |
993 | : ($self->{w} - $tex->{w}) * 0.5; |
858 | |
994 | |
859 | $tex->draw_quad ($x, 0); |
995 | $tex->draw_quad ($x, $self->{padding}); |
860 | |
996 | |
861 | glDisable GL_TEXTURE_2D; |
997 | glDisable GL_TEXTURE_2D; |
862 | glDisable GL_BLEND; |
998 | glDisable GL_BLEND; |
863 | glGetError and die; |
|
|
864 | } |
999 | } |
865 | |
1000 | |
866 | ############################################################################# |
1001 | ############################################################################# |
867 | |
1002 | |
868 | package CFClient::UI::Entry; |
1003 | package CFClient::UI::Entry; |
… | |
… | |
875 | sub new { |
1010 | sub new { |
876 | my $class = shift; |
1011 | my $class = shift; |
877 | |
1012 | |
878 | $class->SUPER::new ( |
1013 | $class->SUPER::new ( |
879 | fg => [1, 1, 1], |
1014 | fg => [1, 1, 1], |
880 | bg => [0, 0, 0, 0.4], |
1015 | bg => [0, 0, 0, 0.2], |
881 | active_bg => [1, 1, 1], |
1016 | active_bg => [1, 1, 1], |
882 | active_fg => [0, 0, 0], |
1017 | active_fg => [0, 0, 0], |
883 | @_ |
1018 | @_ |
884 | ) |
1019 | ) |
885 | } |
1020 | } |
… | |
… | |
892 | $self->{text} = $text; |
1027 | $self->{text} = $text; |
893 | $self->{layout}->set_width ($self->{w}); |
1028 | $self->{layout}->set_width ($self->{w}); |
894 | |
1029 | |
895 | $text =~ s/./*/g if $self->{hidden}; |
1030 | $text =~ s/./*/g if $self->{hidden}; |
896 | |
1031 | |
897 | |
|
|
898 | $self->{layout}->set_markup ($self->escape_text ($text)); |
1032 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
899 | |
1033 | |
900 | $text = substr $text, 0, $self->{cursor}; |
1034 | $text = substr $text, 0, $self->{cursor}; |
901 | utf8::encode $text; |
1035 | utf8::encode $text; |
902 | |
1036 | |
903 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
1037 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
… | |
… | |
910 | |
1044 | |
911 | ($w + 1, $h) # add 1 for cursor |
1045 | ($w + 1, $h) # add 1 for cursor |
912 | } |
1046 | } |
913 | |
1047 | |
914 | sub size_allocate { |
1048 | sub size_allocate { |
915 | my ($self, $w, $h) = @_; |
1049 | my ($self, $x, $y, $w, $h) = @_; |
916 | |
1050 | |
917 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
918 | |
|
|
919 | $self->SUPER::size_allocate ($w, $h); |
1051 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
920 | |
1052 | |
921 | $self->_set_text ($self->{text}); |
1053 | $self->_set_text ($self->{text}); |
922 | } |
1054 | } |
923 | |
1055 | |
924 | sub set_text { |
1056 | sub set_text { |
… | |
… | |
945 | substr $text, $self->{cursor}, 1, ""; |
1077 | substr $text, $self->{cursor}, 1, ""; |
946 | } elsif ($sym == SDLK_LEFT) { |
1078 | } elsif ($sym == SDLK_LEFT) { |
947 | --$self->{cursor} if $self->{cursor}; |
1079 | --$self->{cursor} if $self->{cursor}; |
948 | } elsif ($sym == SDLK_RIGHT) { |
1080 | } elsif ($sym == SDLK_RIGHT) { |
949 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1081 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
|
|
1082 | } elsif ($sym == SDLK_HOME) { |
|
|
1083 | $self->{cursor} = 0; |
|
|
1084 | } elsif ($sym == SDLK_END) { |
|
|
1085 | $self->{cursor} = length $text; |
950 | } elsif ($uni) { |
1086 | } elsif ($uni) { |
951 | substr $text, $self->{cursor}++, 0, chr $uni; |
1087 | substr $text, $self->{cursor}++, 0, chr $uni; |
952 | } |
1088 | } |
953 | |
1089 | |
954 | $self->_set_text ($text); |
1090 | $self->_set_text ($text); |
… | |
… | |
969 | $self->SUPER::button_down ($ev, $x, $y); |
1105 | $self->SUPER::button_down ($ev, $x, $y); |
970 | |
1106 | |
971 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1107 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
972 | |
1108 | |
973 | # byte-index to char-index |
1109 | # byte-index to char-index |
974 | my $text = $self->{layout}; |
1110 | my $text = $self->{text}; |
975 | utf8::encode $text; |
1111 | utf8::encode $text; |
976 | $self->{cursor} = length substr $text, 0, $idx; |
1112 | $self->{cursor} = length substr $text, 0, $idx; |
977 | |
1113 | |
978 | $self->_set_text ($self->{text}); |
1114 | $self->_set_text ($self->{text}); |
979 | $self->update; |
1115 | $self->update; |
… | |
… | |
994 | $self->{fg} = $self->{active_fg}; |
1130 | $self->{fg} = $self->{active_fg}; |
995 | } else { |
1131 | } else { |
996 | glColor @{$self->{bg}}; |
1132 | glColor @{$self->{bg}}; |
997 | } |
1133 | } |
998 | |
1134 | |
|
|
1135 | glEnable GL_BLEND; |
|
|
1136 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
999 | glBegin GL_QUADS; |
1137 | glBegin GL_QUADS; |
1000 | glVertex 0 , 0; |
1138 | glVertex 0 , 0; |
1001 | glVertex 0 , $self->{h}; |
1139 | glVertex 0 , $self->{h}; |
1002 | glVertex $self->{w}, $self->{h}; |
1140 | glVertex $self->{w}, $self->{h}; |
1003 | glVertex $self->{w}, 0; |
1141 | glVertex $self->{w}, 0; |
1004 | glEnd; |
1142 | glEnd; |
|
|
1143 | glDisable GL_BLEND; |
1005 | |
1144 | |
1006 | $self->SUPER::_draw; |
1145 | $self->SUPER::_draw; |
1007 | |
1146 | |
1008 | #TODO: force update every cursor change :( |
1147 | #TODO: force update every cursor change :( |
1009 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1148 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
… | |
… | |
1023 | |
1162 | |
1024 | use SDL::OpenGL; |
1163 | use SDL::OpenGL; |
1025 | |
1164 | |
1026 | our @ISA = CFClient::UI::DrawBG::; |
1165 | our @ISA = CFClient::UI::DrawBG::; |
1027 | |
1166 | |
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 { |
1167 | sub new { |
1038 | my $class = shift; |
1168 | my $class = shift; |
1039 | |
1169 | |
1040 | # range [value, low, high, page] |
1170 | # range [value, low, high, page] |
1041 | |
1171 | |
1042 | $class->SUPER::new ( |
1172 | my $self = $class->SUPER::new ( |
1043 | fg => [1, 1, 1], |
1173 | fg => [1, 1, 1], |
1044 | active_fg => [0, 0, 0], |
1174 | active_fg => [0, 0, 0], |
1045 | range => [0, 0, 100, 10], |
1175 | range => [0, 0, 100, 10], |
|
|
1176 | req_w => 40, |
|
|
1177 | req_h => 10, |
1046 | vertical => 1, |
1178 | vertical => 0, |
1047 | @_ |
1179 | @_ |
1048 | ) |
1180 | ); |
|
|
1181 | |
|
|
1182 | $self |
|
|
1183 | } |
|
|
1184 | |
|
|
1185 | sub size_request { |
|
|
1186 | my ($self) = @_; |
|
|
1187 | |
|
|
1188 | my $w = $self->{req_w}; |
|
|
1189 | my $h = $self->{req_h}; |
|
|
1190 | |
|
|
1191 | $self->{vertical} ? ($h, $w) : ($w, $h) |
1049 | } |
1192 | } |
1050 | |
1193 | |
1051 | sub button_down { |
1194 | sub button_down { |
1052 | my ($self, $ev, $x, $y) = @_; |
1195 | my ($self, $ev, $x, $y) = @_; |
1053 | |
1196 | |
… | |
… | |
1158 | sub key_up { |
1301 | sub key_up { |
1159 | } |
1302 | } |
1160 | |
1303 | |
1161 | sub size_request { |
1304 | sub size_request { |
1162 | ( |
1305 | ( |
1163 | 1 + int $::WIDTH / 32, |
1306 | 1 + 32 * int $::WIDTH / 32, |
1164 | 1 + int $::HEIGHT / 32, |
1307 | 1 + 32 * int $::HEIGHT / 32, |
1165 | ) |
1308 | ) |
1166 | } |
1309 | } |
1167 | |
1310 | |
1168 | sub update { |
1311 | sub update { |
1169 | my ($self) = @_; |
1312 | my ($self) = @_; |
1170 | |
1313 | |
1171 | $self->{need_update} = 1; |
1314 | $self->{need_update} = 1; |
1172 | $self->SUPER::update; |
1315 | $self->SUPER::update; |
1173 | } |
1316 | } |
1174 | |
1317 | |
1175 | sub _draw { |
1318 | sub draw { |
1176 | my ($self) = @_; |
1319 | my ($self) = @_; |
1177 | |
1320 | |
1178 | if (delete $self->{need_update}) { |
1321 | if (delete $self->{need_update}) { |
1179 | glNewList $self->{list}, GL_COMPILE; |
1322 | glNewList $self->{list}, GL_COMPILE; |
1180 | |
1323 | |
… | |
… | |
1365 | sub size_request { |
1508 | sub size_request { |
1366 | ($::WIDTH, $::HEIGHT) |
1509 | ($::WIDTH, $::HEIGHT) |
1367 | } |
1510 | } |
1368 | |
1511 | |
1369 | sub size_allocate { |
1512 | sub size_allocate { |
1370 | my ($self, $w, $h) = @_; |
1513 | my ($self, $x, $y, $w, $h) = @_; |
1371 | |
1514 | |
1372 | $self->SUPER::size_allocate ($w, $h); |
1515 | $self->_size_allocate ($x, $y, $w, $h); |
1373 | |
1516 | |
1374 | $_->size_allocate ($_->size_request) |
1517 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
1375 | for @{$self->{children}}; |
1518 | for @{$self->{children}}; |
1376 | } |
1519 | } |
1377 | |
1520 | |
1378 | sub translate { |
1521 | sub translate { |
1379 | my ($self, $x, $y) = @_; |
1522 | my ($self, $x, $y) = @_; |
… | |
… | |
1382 | } |
1525 | } |
1383 | |
1526 | |
1384 | sub update { |
1527 | sub update { |
1385 | my ($self) = @_; |
1528 | my ($self) = @_; |
1386 | |
1529 | |
1387 | $self->size_allocate ($self->size_request); |
1530 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
1388 | ::refresh (); |
1531 | ::refresh (); |
1389 | } |
1532 | } |
1390 | |
1533 | |
1391 | sub add { |
1534 | sub add { |
1392 | my ($self, $widget) = @_; |
1535 | my ($self, $widget) = @_; |
1393 | |
1536 | |
1394 | $self->SUPER::add ($widget); |
1537 | $self->SUPER::add ($widget); |
1395 | |
1538 | |
1396 | $widget->size_allocate ($widget->size_request); |
1539 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
1397 | } |
1540 | } |
1398 | |
1541 | |
1399 | sub draw { |
1542 | sub draw { |
1400 | my ($self) = @_; |
1543 | my ($self) = @_; |
1401 | |
1544 | |