… | |
… | |
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; |
… | |
… | |
79 | use SDL::OpenGL; |
79 | use SDL::OpenGL; |
80 | |
80 | |
81 | sub new { |
81 | sub new { |
82 | my $class = shift; |
82 | my $class = shift; |
83 | |
83 | |
84 | bless { |
84 | my $self = bless { |
85 | x => 0, |
85 | x => 0, |
86 | y => 0, |
86 | y => 0, |
87 | z => 0, |
87 | z => 0, |
88 | w => -1, |
88 | w => -1, |
89 | h => -1, |
89 | h => -1, |
90 | @_ |
90 | @_ |
91 | }, $class |
91 | }, $class; |
|
|
92 | |
|
|
93 | for (keys %$self) { |
|
|
94 | if (/^connect_(.*)$/) { |
|
|
95 | $self->connect ($1 => delete $self->{$_}); |
|
|
96 | } |
|
|
97 | } |
|
|
98 | |
|
|
99 | $self |
92 | } |
100 | } |
93 | |
101 | |
94 | sub move { |
102 | sub move { |
95 | my ($self, $x, $y, $z) = @_; |
103 | my ($self, $x, $y, $z) = @_; |
96 | $self->{x} = $x; |
104 | $self->{x} = $x; |
… | |
… | |
125 | my ($self, $x, $y, $w, $h) = @_; |
133 | my ($self, $x, $y, $w, $h) = @_; |
126 | |
134 | |
127 | $self->_size_allocate ($x, $y, $w, $h); |
135 | $self->_size_allocate ($x, $y, $w, $h); |
128 | } |
136 | } |
129 | |
137 | |
130 | # translate global koordinates to local coordinate system |
138 | # translate global coordinates to local coordinate system |
131 | sub translate { |
139 | sub translate { |
132 | my ($self, $x, $y) = @_; |
140 | my ($self, $x, $y) = @_; |
133 | |
141 | |
134 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
142 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
135 | } |
143 | } |
136 | |
144 | |
137 | sub focus_in { |
145 | sub focus_in { |
138 | my ($self) = @_; |
146 | my ($self) = @_; |
139 | |
147 | |
140 | return if $FOCUS == $self; |
148 | return if $FOCUS == $self; |
|
|
149 | return unless $self->{can_focus}; |
141 | |
150 | |
142 | my $focus = $FOCUS; $FOCUS = $self; |
151 | my $focus = $FOCUS; $FOCUS = $self; |
143 | $focus->update if $focus; |
152 | $focus->update if $focus; |
144 | $FOCUS->update; |
153 | $FOCUS->update; |
145 | } |
154 | } |
… | |
… | |
178 | glPushMatrix; |
187 | glPushMatrix; |
179 | glTranslate $self->{x}, $self->{y}, 0; |
188 | glTranslate $self->{x}, $self->{y}, 0; |
180 | $self->_draw; |
189 | $self->_draw; |
181 | glPopMatrix; |
190 | glPopMatrix; |
182 | |
191 | |
183 | if ($self == $HOVER) { |
192 | if ($self == $HOVER && $self->{can_hover}) { |
184 | my ($x, $y) = @$self{qw(x y)}; |
193 | my ($x, $y) = @$self{qw(x y)}; |
185 | |
194 | |
186 | glColor 1, 1, 1, 0.1; |
195 | glColor 0, 0, 1, 0.2; |
187 | glEnable GL_BLEND; |
196 | glEnable GL_BLEND; |
188 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
197 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
189 | glBegin GL_QUADS; |
198 | glBegin GL_QUADS; |
190 | glVertex $x , $y; |
199 | glVertex $x , $y; |
191 | glVertex $x + $self->{w}, $y; |
200 | glVertex $x + $self->{w}, $y; |
… | |
… | |
200 | my ($self) = @_; |
209 | my ($self) = @_; |
201 | |
210 | |
202 | warn "no draw defined for $self\n"; |
211 | warn "no draw defined for $self\n"; |
203 | } |
212 | } |
204 | |
213 | |
205 | sub bbox { |
|
|
206 | my ($self) = @_; |
|
|
207 | my ($w, $h) = $self->size_request; |
|
|
208 | ( |
|
|
209 | $self->{x}, |
|
|
210 | $self->{y}, |
|
|
211 | $self->{x} = $w, |
|
|
212 | $self->{y} = $h |
|
|
213 | ) |
|
|
214 | } |
|
|
215 | |
|
|
216 | sub find_widget { |
214 | sub find_widget { |
217 | my ($self, $x, $y) = @_; |
215 | my ($self, $x, $y) = @_; |
218 | |
216 | |
219 | return $self |
217 | return $self |
220 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
218 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
221 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
219 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
222 | |
220 | |
223 | () |
221 | () |
224 | } |
222 | } |
225 | |
223 | |
226 | sub del_parent { $_[0]->{parent} = undef } |
|
|
227 | |
|
|
228 | sub set_parent { |
224 | sub set_parent { |
229 | my ($self, $par) = @_; |
225 | my ($self, $par) = @_; |
230 | |
226 | |
231 | $self->{parent} = $par; |
227 | $self->{parent} = $par; |
232 | Scalar::Util::weaken $self->{parent}; |
228 | Scalar::Util::weaken $self->{parent}; |
… | |
… | |
275 | my $class = shift; |
271 | my $class = shift; |
276 | |
272 | |
277 | # range [value, low, high, page] |
273 | # range [value, low, high, page] |
278 | |
274 | |
279 | $class->SUPER::new ( |
275 | $class->SUPER::new ( |
280 | bg => [0, 0, 0, 0.4], |
276 | bg => [0, 0, 0, 0.2], |
281 | active_bg => [1, 1, 1], |
277 | active_bg => [1, 1, 1, 0.5], |
282 | @_ |
278 | @_ |
283 | ) |
279 | ) |
284 | } |
280 | } |
285 | |
281 | |
286 | sub _draw { |
282 | sub _draw { |
287 | my ($self) = @_; |
283 | my ($self) = @_; |
288 | |
284 | |
289 | my ($w, $h) = @$self{qw(w h)}; |
285 | my ($w, $h) = @$self{qw(w h)}; |
290 | |
286 | |
|
|
287 | glEnable GL_BLEND; |
|
|
288 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
291 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
289 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
|
|
290 | |
292 | glBegin GL_QUADS; |
291 | glBegin GL_QUADS; |
293 | glVertex 0 , 0; |
292 | glVertex 0 , 0; |
294 | glVertex 0 , $h; |
293 | glVertex 0 , $h; |
295 | glVertex $w, $h; |
294 | glVertex $w, $h; |
296 | glVertex $w, 0; |
295 | glVertex $w, 0; |
297 | glEnd; |
296 | glEnd; |
|
|
297 | |
|
|
298 | glDisable GL_BLEND; |
298 | } |
299 | } |
299 | |
300 | |
300 | ############################################################################# |
301 | ############################################################################# |
301 | |
302 | |
302 | package CFClient::UI::Empty; |
303 | package CFClient::UI::Empty; |
… | |
… | |
325 | |
326 | |
326 | $self |
327 | $self |
327 | } |
328 | } |
328 | |
329 | |
329 | sub add { |
330 | sub add { |
330 | my ($self, $chld, $expand) = @_; |
331 | my ($self, $chld) = @_; |
331 | |
332 | |
332 | $chld->{expand} = $expand; |
|
|
333 | $chld->set_parent ($self); |
333 | $chld->set_parent ($self); |
334 | |
334 | |
335 | $self->{children} = [ |
335 | $self->{children} = [ |
336 | sort { $a->{z} <=> $b->{z} } |
336 | sort { $a->{z} <=> $b->{z} } |
337 | @{$self->{children}}, $chld |
337 | @{$self->{children}}, $chld |
… | |
… | |
530 | |
530 | |
531 | my @tex = |
531 | my @tex = |
532 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
532 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
533 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
533 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
534 | |
534 | |
|
|
535 | sub new { |
|
|
536 | my $class = shift; |
|
|
537 | |
|
|
538 | # TODO: user_x, user_y, overwrite moveto? |
|
|
539 | |
|
|
540 | $class->SUPER::new ( |
|
|
541 | bg => [1, 1, 1, 1], |
|
|
542 | border_bg => [1, 1, 1, 1], |
|
|
543 | border => $::FONTSIZE * 0.8, |
|
|
544 | @_ |
|
|
545 | ) |
|
|
546 | } |
|
|
547 | |
535 | sub size_request { |
548 | sub size_request { |
536 | my ($self) = @_; |
549 | my ($self) = @_; |
537 | |
550 | |
|
|
551 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
552 | |
538 | my ($w, $h) = $self->SUPER::size_request; |
553 | my ($w, $h) = $self->SUPER::size_request; |
539 | |
554 | |
540 | $h += $tex[1]->{h}; |
555 | ( |
541 | $h += $tex[4]->{h}; |
556 | $w + $self->{border} * 2, |
542 | $w += $tex[2]->{w}; |
557 | $h + $self->{border} * 2, |
543 | $w += $tex[3]->{w}; |
558 | ) |
544 | |
|
|
545 | ($w, $h) |
|
|
546 | } |
559 | } |
547 | |
560 | |
548 | sub size_allocate { |
561 | sub size_allocate { |
549 | my ($self, $x, $y, $w, $h) = @_; |
562 | my ($self, $x, $y, $w, $h) = @_; |
550 | |
563 | |
551 | $self->_size_allocate ($x, $y, $w, $h) or return; |
564 | $self->_size_allocate ($x, $y, $w, $h) or return; |
552 | |
565 | |
553 | $h -= $tex[1]->{h}; |
566 | $h -= List::Util::max 0, $self->{border} * 2; |
554 | $h -= $tex[4]->{h}; |
567 | $w -= List::Util::max 0, $self->{border} * 2; |
555 | $w -= $tex[2]->{w}; |
|
|
556 | $w -= $tex[3]->{w}; |
|
|
557 | |
568 | |
558 | $h = $h < 0 ? 0 : $h; |
569 | $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); |
559 | $w = $w < 0 ? 0 : $w; |
570 | } |
560 | |
571 | |
561 | my $child = $self->child; |
572 | sub button_down { |
|
|
573 | my ($self, $ev, $x, $y) = @_; |
562 | |
574 | |
563 | $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); |
575 | if ($x < $self->{w} && $x >= $self->{w} - $self->{border} |
|
|
576 | && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { |
|
|
577 | |
|
|
578 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
579 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
|
|
580 | |
|
|
581 | $self->{motion} = sub { |
|
|
582 | my ($ev, $x, $y) = @_; |
|
|
583 | |
|
|
584 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
585 | |
|
|
586 | $self->{user_w} = $bw + $x - $ox; |
|
|
587 | $self->{user_h} = $bh + $y - $oy; |
|
|
588 | $self->update; |
|
|
589 | }; |
|
|
590 | |
|
|
591 | } elsif ($x >= 0 && $x < $self->{w} |
|
|
592 | && $y >= 0 && $y < $self->{border}) { |
|
|
593 | |
|
|
594 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
595 | my ($bx, $by) = ($self->{x}, $self->{y}); |
|
|
596 | |
|
|
597 | $self->{motion} = sub { |
|
|
598 | my ($ev, $x, $y) = @_; |
|
|
599 | |
|
|
600 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
601 | |
|
|
602 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
|
|
603 | $self->update; |
|
|
604 | }; |
|
|
605 | } |
|
|
606 | } |
|
|
607 | |
|
|
608 | sub button_up { |
|
|
609 | my ($self, $ev, $x, $y) = @_; |
|
|
610 | |
|
|
611 | delete $self->{motion}; |
|
|
612 | } |
|
|
613 | |
|
|
614 | sub mouse_motion { |
|
|
615 | my ($self, $ev, $x, $y) = @_; |
|
|
616 | |
|
|
617 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
564 | } |
618 | } |
565 | |
619 | |
566 | sub _draw { |
620 | sub _draw { |
567 | my ($self) = @_; |
621 | my ($self) = @_; |
568 | |
622 | |
569 | my ($w, $h) = ($self->{w}, $self->{h}); |
623 | my ($w, $h ) = ($self->{w}, $self->{h}); |
570 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
624 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
571 | |
625 | |
572 | glEnable GL_BLEND; |
626 | glEnable GL_BLEND; |
573 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
627 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
574 | glEnable GL_TEXTURE_2D; |
628 | glEnable GL_TEXTURE_2D; |
575 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
629 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
576 | |
630 | |
577 | my $top = $tex[1]; |
631 | glColor @{ $self->{border_bg} }; |
578 | $top->draw_quad (0, 0, $w, $top->{h}); |
632 | $tex[1]->draw_quad (0, 0, $w, $self->{border}); |
579 | |
633 | $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); |
580 | my $left = $tex[3]; |
634 | $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); |
581 | $left->draw_quad (0, $top->{h}, $left->{w}, $ch); |
635 | $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); |
582 | |
|
|
583 | my $right = $tex[2]; |
|
|
584 | $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch); |
|
|
585 | |
|
|
586 | my $bottom = $tex[4]; |
|
|
587 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
|
|
588 | |
636 | |
589 | my $bg = $tex[0]; |
637 | my $bg = $tex[0]; |
590 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
|
|
591 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
592 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
|
|
593 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
|
|
594 | |
638 | |
|
|
639 | # TODO: repeat texture not scale |
595 | my $rep_x = $cw / $bg->{w}; |
640 | my $rep_x = $cw / $bg->{w}; |
596 | my $rep_y = $ch / $bg->{h}; |
641 | my $rep_y = $ch / $bg->{h}; |
597 | |
642 | |
|
|
643 | glColor @{ $self->{bg} }; |
|
|
644 | |
|
|
645 | $bg->{s} = $rep_x; |
|
|
646 | $bg->{t} = $rep_y; |
|
|
647 | $bg->{wrap_mode} = 1; |
598 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
648 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
599 | |
649 | |
|
|
650 | glDisable GL_TEXTURE_2D; |
600 | glDisable GL_BLEND; |
651 | glDisable GL_BLEND; |
601 | glDisable GL_TEXTURE_2D; |
|
|
602 | |
652 | |
603 | $self->child->draw; |
653 | $self->child->draw; |
604 | |
|
|
605 | } |
654 | } |
606 | |
655 | |
607 | ############################################################################# |
656 | ############################################################################# |
608 | |
657 | |
609 | package CFClient::UI::Table; |
658 | package CFClient::UI::Table; |
… | |
… | |
611 | our @ISA = CFClient::UI::Base::; |
660 | our @ISA = CFClient::UI::Base::; |
612 | |
661 | |
613 | use List::Util qw(max sum); |
662 | use List::Util qw(max sum); |
614 | |
663 | |
615 | use SDL::OpenGL; |
664 | use SDL::OpenGL; |
|
|
665 | |
|
|
666 | sub new { |
|
|
667 | my $class = shift; |
|
|
668 | |
|
|
669 | $class->SUPER::new ( |
|
|
670 | col_expand => [], |
|
|
671 | @_ |
|
|
672 | ) |
|
|
673 | } |
616 | |
674 | |
617 | sub add { |
675 | sub add { |
618 | my ($self, $x, $y, $chld) = @_; |
676 | my ($self, $x, $y, $chld) = @_; |
619 | |
677 | |
620 | $self->{children}[$y][$x] = $chld; |
678 | $self->{children}[$y][$x] = $chld; |
… | |
… | |
650 | my ($self) = @_; |
708 | my ($self) = @_; |
651 | |
709 | |
652 | my ($ws, $hs) = $self->get_wh; |
710 | my ($ws, $hs) = $self->get_wh; |
653 | |
711 | |
654 | ( |
712 | ( |
655 | (List::Util::sum @$ws), |
713 | (sum @$ws), |
656 | (List::Util::sum @$hs), |
714 | (sum @$hs), |
657 | ) |
715 | ) |
658 | } |
716 | } |
659 | |
717 | |
660 | sub size_allocate { |
718 | sub size_allocate { |
661 | my ($self, $x, $y, $w, $h) = @_; |
719 | my ($self, $x, $y, $w, $h) = @_; |
662 | |
720 | |
663 | $self->_size_allocate ($x, $y, $w, $h) or return; |
721 | $self->_size_allocate ($x, $y, $w, $h) or return; |
664 | |
722 | |
665 | my ($ws, $hs) = $self->get_wh; |
723 | my ($ws, $hs) = $self->get_wh; |
666 | |
724 | |
667 | my $req_w = List::Util::sum @$ws; |
725 | my $req_w = sum @$ws; |
668 | my $req_h = List::Util::sum @$hs; |
726 | my $req_h = sum @$hs; |
|
|
727 | |
|
|
728 | # TODO: nicer code && do row_expand |
|
|
729 | my @col_expand = @{$self->{col_expand}}; |
|
|
730 | @col_expand = (1) x @$ws unless @col_expand; |
|
|
731 | my $col_expand = (sum @col_expand) || 1; |
669 | |
732 | |
670 | # linearly scale sizes |
733 | # linearly scale sizes |
671 | $_ *= $req_w / $w for @$ws; |
734 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
672 | $_ *= $req_h / $h for @$hs; |
735 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
673 | |
736 | |
674 | my $y; |
737 | my $y; |
675 | |
738 | |
676 | for my $r (0 .. $#{$self->{children}}) { |
739 | for my $r (0 .. $#{$self->{children}}) { |
677 | my $row = $self->{children}[$r] |
740 | my $row = $self->{children}[$r] |
… | |
… | |
679 | |
742 | |
680 | my $x = 0; |
743 | my $x = 0; |
681 | my $row_h = $hs->[$r]; |
744 | my $row_h = $hs->[$r]; |
682 | |
745 | |
683 | for my $c (0 .. $#$row) { |
746 | for my $c (0 .. $#$row) { |
684 | my $widget = $row->[$c] |
|
|
685 | or next; |
|
|
686 | |
|
|
687 | my $col_w = $ws->[$c]; |
747 | my $col_w = $ws->[$c]; |
688 | |
748 | |
|
|
749 | if (my $widget = $row->[$c]) { |
689 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
750 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
|
|
751 | } |
690 | |
752 | |
691 | $x += $col_w; |
753 | $x += $col_w; |
692 | } |
754 | } |
693 | |
755 | |
694 | $y += $row_h; |
756 | $y += $row_h; |
695 | } |
757 | } |
696 | |
758 | |
697 | } |
759 | } |
698 | |
760 | |
|
|
761 | sub find_widget { |
|
|
762 | my ($self, $x, $y) = @_; |
|
|
763 | |
|
|
764 | $x -= $self->{x}; |
|
|
765 | $y -= $self->{y}; |
|
|
766 | |
|
|
767 | my $res; |
|
|
768 | |
|
|
769 | for (grep $_, map @$_, grep $_, @{ $self->{children} }) { |
|
|
770 | $res = $_->find_widget ($x, $y) |
|
|
771 | and return $res; |
|
|
772 | } |
|
|
773 | |
|
|
774 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
|
|
775 | } |
|
|
776 | |
699 | sub _draw { |
777 | sub _draw { |
700 | my ($self) = @_; |
778 | my ($self) = @_; |
701 | |
779 | |
702 | for (grep $_, @{$self->{children}}) { |
780 | for (grep $_, @{$self->{children}}) { |
703 | $_->draw for grep $_, @$_; |
781 | $_->draw for grep $_, @$_; |
704 | } |
782 | } |
705 | } |
783 | } |
706 | |
784 | |
707 | ############################################################################# |
785 | ############################################################################# |
708 | |
786 | |
|
|
787 | package CFClient::UI::HBox; |
|
|
788 | |
|
|
789 | # TODO: wrap into common Box base class |
|
|
790 | |
|
|
791 | our @ISA = CFClient::UI::Container::; |
|
|
792 | |
|
|
793 | sub size_request { |
|
|
794 | my ($self) = @_; |
|
|
795 | |
|
|
796 | my @alloc = map [$_->size_request], @{$self->{children}}; |
|
|
797 | |
|
|
798 | ( |
|
|
799 | (List::Util::sum map $_->[0], @alloc), |
|
|
800 | (List::Util::max map $_->[1], @alloc), |
|
|
801 | ) |
|
|
802 | } |
|
|
803 | |
|
|
804 | sub size_allocate { |
|
|
805 | my ($self, $x, $y, $w, $h) = @_; |
|
|
806 | |
|
|
807 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
808 | |
|
|
809 | ($h, $w) = ($w, $h); |
|
|
810 | |
|
|
811 | my $children = $self->{children}; |
|
|
812 | |
|
|
813 | my @h = map +($_->size_request)[0], @$children; |
|
|
814 | |
|
|
815 | my $req_h = List::Util::sum @h; |
|
|
816 | |
|
|
817 | if ($req_h > $h) { |
|
|
818 | # ah well, not enough space |
|
|
819 | $_ *= $h / $req_h for @h; |
|
|
820 | } else { |
|
|
821 | my $exp = List::Util::sum map $_->{expand}, @$children; |
|
|
822 | $exp ||= 1; |
|
|
823 | |
|
|
824 | for (0 .. $#$children) { |
|
|
825 | my $child = $children->[$_]; |
|
|
826 | |
|
|
827 | my $alloc_h = $h[$_]; |
|
|
828 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
|
|
829 | $h[$_] = $alloc_h; |
|
|
830 | } |
|
|
831 | } |
|
|
832 | |
|
|
833 | my $y = 0; |
|
|
834 | for (0 .. $#$children) { |
|
|
835 | my $child = $children->[$_]; |
|
|
836 | my $h = $h[$_]; |
|
|
837 | $child->size_allocate ($y, 0, $h, $w); |
|
|
838 | |
|
|
839 | $y += $h; |
|
|
840 | } |
|
|
841 | } |
|
|
842 | |
|
|
843 | ############################################################################# |
|
|
844 | |
709 | package CFClient::UI::VBox; |
845 | package CFClient::UI::VBox; |
710 | |
846 | |
|
|
847 | # TODO: wrap into common Box base class |
|
|
848 | |
711 | our @ISA = CFClient::UI::Container::; |
849 | our @ISA = CFClient::UI::Container::; |
712 | |
|
|
713 | use SDL::OpenGL; |
|
|
714 | |
850 | |
715 | sub size_request { |
851 | sub size_request { |
716 | my ($self) = @_; |
852 | my ($self) = @_; |
717 | |
853 | |
718 | my @alloc = map [$_->size_request], @{$self->{children}}; |
854 | my @alloc = map [$_->size_request], @{$self->{children}}; |
… | |
… | |
724 | } |
860 | } |
725 | |
861 | |
726 | sub size_allocate { |
862 | sub size_allocate { |
727 | my ($self, $x, $y, $w, $h) = @_; |
863 | my ($self, $x, $y, $w, $h) = @_; |
728 | |
864 | |
729 | $self->_size_allocate ($x, $y, $w, $h) or return; |
865 | $self->_size_allocate ($x, $y, $w, $h); |
730 | |
|
|
731 | return unless $self->{h}; |
|
|
732 | |
866 | |
733 | my $children = $self->{children}; |
867 | my $children = $self->{children}; |
734 | |
868 | |
735 | my @h = map +($_->size_request)[1], @$children; |
869 | my @h = map +($_->size_request)[1], @$children; |
736 | |
870 | |
737 | my $req_h = List::Util::sum @h; |
871 | my $req_h = List::Util::sum @h; |
738 | |
872 | |
739 | if ($req_h > $h) { |
873 | if ($req_h > $h) { |
740 | # ah well, not enough space |
874 | # ah well, not enough space |
741 | $_ = $h[$_] * $h / $req_h for @h; |
875 | $_ *= $h / $req_h for @h; |
742 | } else { |
876 | } else { |
743 | my @exp = grep $_->{expand}, @$children; |
877 | my $exp = List::Util::sum map $_->{expand}, @$children; |
744 | @exp = @$children unless @exp; |
878 | $exp ||= 1; |
745 | |
879 | |
746 | my %exp = map +($_ => 1), @exp; |
|
|
747 | |
|
|
748 | for (0 .. $#$children) { |
880 | for (0 .. $#$children) { |
749 | my $child = $children->[$_]; |
881 | my $child = $children->[$_]; |
750 | |
882 | |
751 | my $alloc_h = $h[$_]; |
883 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
752 | $alloc_h += ($h - $req_h) / @exp if $exp{$child}; |
|
|
753 | $h[$_] = $alloc_h; |
|
|
754 | } |
884 | } |
755 | } |
885 | } |
756 | |
886 | |
757 | my $y = 0; |
887 | my $y = 0; |
758 | for (0 .. $#$children) { |
888 | for (0 .. $#$children) { |
… | |
… | |
774 | |
904 | |
775 | sub new { |
905 | sub new { |
776 | my ($class, %arg) = @_; |
906 | my ($class, %arg) = @_; |
777 | |
907 | |
778 | my $self = $class->SUPER::new ( |
908 | my $self = $class->SUPER::new ( |
779 | fg => [1, 1, 1], |
909 | fg => [1, 1, 1], |
780 | height => $::FONTSIZE, |
910 | height => $::FONTSIZE, |
781 | text => "", |
911 | text => "", |
782 | align => -1, |
912 | align => -1, |
|
|
913 | padding => 2, |
783 | layout => new CFClient::Layout, |
914 | layout => new CFClient::Layout, |
784 | %arg |
915 | %arg |
785 | ); |
916 | ); |
786 | |
917 | |
787 | $self->set_text ($self->{text}); |
918 | $self->set_text ($self->{text}); |
788 | |
919 | |
… | |
… | |
804 | |
935 | |
805 | $self->{text} = $text; |
936 | $self->{text} = $text; |
806 | $self->{layout}->set_markup ($text); |
937 | $self->{layout}->set_markup ($text); |
807 | |
938 | |
808 | delete $self->{texture}; |
939 | delete $self->{texture}; |
|
|
940 | # $self->{w} = $self->{h} = -1; |
809 | $self->update; |
941 | $self->update; |
810 | } |
942 | } |
811 | |
943 | |
812 | sub get_text { |
944 | sub get_text { |
813 | my ($self, $text) = @_; |
945 | my ($self, $text) = @_; |
… | |
… | |
818 | sub size_request { |
950 | sub size_request { |
819 | my ($self) = @_; |
951 | my ($self) = @_; |
820 | |
952 | |
821 | $self->{layout}->set_width; |
953 | $self->{layout}->set_width; |
822 | $self->{layout}->set_height ($self->{height}); |
954 | $self->{layout}->set_height ($self->{height}); |
823 | $self->{layout}->size |
955 | my ($w, $h) = $self->{layout}->size; |
824 | # if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
956 | |
825 | # ( |
957 | ( |
826 | # $self->{texture}{w}, |
958 | $w + $self->{padding} * 2, |
827 | # $self->{texture}{h}, |
959 | $h + $self->{padding} * 2, |
828 | # ) |
960 | ) |
829 | # } else { |
|
|
830 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h}; |
|
|
831 | # |
|
|
832 | # ($w, $h) |
|
|
833 | # } |
|
|
834 | } |
961 | } |
835 | |
962 | |
836 | sub size_allocate { |
963 | sub size_allocate { |
837 | my ($self, $x, $y, $w, $h) = @_; |
964 | my ($self, $x, $y, $w, $h) = @_; |
838 | |
965 | |
… | |
… | |
851 | sub _draw { |
978 | sub _draw { |
852 | my ($self) = @_; |
979 | my ($self) = @_; |
853 | |
980 | |
854 | my $tex = $self->{texture} ||= do { |
981 | my $tex = $self->{texture} ||= do { |
855 | $self->{layout}->set_width ($self->{w}); |
982 | $self->{layout}->set_width ($self->{w}); |
|
|
983 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
856 | new_from_layout CFClient::Texture $self->{layout} |
984 | new_from_layout CFClient::Texture $self->{layout} |
857 | }; |
985 | }; |
858 | |
986 | |
859 | glEnable GL_BLEND; |
987 | glEnable GL_BLEND; |
860 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
988 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
… | |
… | |
862 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
990 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
863 | |
991 | |
864 | glColor @{$self->{fg}}; |
992 | glColor @{$self->{fg}}; |
865 | |
993 | |
866 | my $x = |
994 | my $x = |
867 | $self->{align} < 0 ? 0 |
995 | $self->{align} < 0 ? $self->{padding} |
868 | : $self->{align} > 0 ? $self->{w} - $tex->{w} |
996 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
869 | : ($self->{w} - $tex->{w}) * 0.5; |
997 | : ($self->{w} - $tex->{w}) * 0.5; |
870 | |
998 | |
|
|
999 | glTranslate int $x, int +($self->{h} - $tex->{h}) * 0.5, 0; |
871 | $tex->draw_quad ($x, 0); |
1000 | $tex->draw_quad (0, 0); |
872 | |
1001 | |
873 | glDisable GL_TEXTURE_2D; |
1002 | glDisable GL_TEXTURE_2D; |
874 | glDisable GL_BLEND; |
1003 | glDisable GL_BLEND; |
875 | } |
1004 | } |
876 | |
1005 | |
… | |
… | |
886 | sub new { |
1015 | sub new { |
887 | my $class = shift; |
1016 | my $class = shift; |
888 | |
1017 | |
889 | $class->SUPER::new ( |
1018 | $class->SUPER::new ( |
890 | fg => [1, 1, 1], |
1019 | fg => [1, 1, 1], |
891 | bg => [0, 0, 0, 0.4], |
1020 | bg => [0, 0, 0, 0.2], |
892 | active_bg => [1, 1, 1], |
1021 | active_bg => [1, 1, 1, 0.5], |
893 | active_fg => [0, 0, 0], |
1022 | active_fg => [0, 0, 0], |
|
|
1023 | can_hover => 1, |
|
|
1024 | can_focus => 1, |
894 | @_ |
1025 | @_ |
895 | ) |
1026 | ) |
896 | } |
1027 | } |
897 | |
1028 | |
898 | sub _set_text { |
1029 | sub _set_text { |
899 | my ($self, $text) = @_; |
1030 | my ($self, $text) = @_; |
900 | |
1031 | |
|
|
1032 | my $old_text = $self->{text}; |
|
|
1033 | |
901 | $self->{last_activity} = $::NOW; |
1034 | $self->{last_activity} = $::NOW; |
902 | |
1035 | |
903 | $self->{text} = $text; |
1036 | $self->{text} = $text; |
904 | $self->{layout}->set_width ($self->{w}); |
1037 | $self->{layout}->set_width ($self->{w}); |
|
|
1038 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
905 | |
1039 | |
906 | $text =~ s/./*/g if $self->{hidden}; |
1040 | $text =~ s/./*/g if $self->{hidden}; |
907 | |
1041 | |
908 | |
|
|
909 | $self->{layout}->set_markup ($self->escape_text ($text)); |
1042 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
910 | |
1043 | |
911 | $text = substr $text, 0, $self->{cursor}; |
1044 | $text = substr $text, 0, $self->{cursor}; |
912 | utf8::encode $text; |
1045 | utf8::encode $text; |
913 | |
1046 | |
914 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
1047 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
1048 | |
|
|
1049 | $self->emit (changed => $self->{text}) # XXX: is this the right place to do this? |
|
|
1050 | if $old_text ne $self->{text}; |
915 | } |
1051 | } |
916 | |
1052 | |
917 | sub size_request { |
1053 | sub size_request { |
918 | my ($self) = @_; |
1054 | my ($self) = @_; |
919 | |
1055 | |
… | |
… | |
954 | substr $text, $self->{cursor}, 1, ""; |
1090 | substr $text, $self->{cursor}, 1, ""; |
955 | } elsif ($sym == SDLK_LEFT) { |
1091 | } elsif ($sym == SDLK_LEFT) { |
956 | --$self->{cursor} if $self->{cursor}; |
1092 | --$self->{cursor} if $self->{cursor}; |
957 | } elsif ($sym == SDLK_RIGHT) { |
1093 | } elsif ($sym == SDLK_RIGHT) { |
958 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1094 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
|
|
1095 | } elsif ($sym == SDLK_HOME) { |
|
|
1096 | $self->{cursor} = 0; |
|
|
1097 | } elsif ($sym == SDLK_END) { |
|
|
1098 | $self->{cursor} = length $text; |
959 | } elsif ($uni) { |
1099 | } elsif ($uni) { |
960 | substr $text, $self->{cursor}++, 0, chr $uni; |
1100 | substr $text, $self->{cursor}++, 0, chr $uni; |
961 | } |
1101 | } |
962 | |
1102 | |
963 | $self->_set_text ($text); |
1103 | $self->_set_text ($text); |
… | |
… | |
978 | $self->SUPER::button_down ($ev, $x, $y); |
1118 | $self->SUPER::button_down ($ev, $x, $y); |
979 | |
1119 | |
980 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1120 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
981 | |
1121 | |
982 | # byte-index to char-index |
1122 | # byte-index to char-index |
983 | my $text = $self->{layout}; |
1123 | my $text = $self->{text}; |
984 | utf8::encode $text; |
1124 | utf8::encode $text; |
985 | $self->{cursor} = length substr $text, 0, $idx; |
1125 | $self->{cursor} = length substr $text, 0, $idx; |
986 | |
1126 | |
987 | $self->_set_text ($self->{text}); |
1127 | $self->_set_text ($self->{text}); |
988 | $self->update; |
1128 | $self->update; |
… | |
… | |
1003 | $self->{fg} = $self->{active_fg}; |
1143 | $self->{fg} = $self->{active_fg}; |
1004 | } else { |
1144 | } else { |
1005 | glColor @{$self->{bg}}; |
1145 | glColor @{$self->{bg}}; |
1006 | } |
1146 | } |
1007 | |
1147 | |
|
|
1148 | glEnable GL_BLEND; |
|
|
1149 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1008 | glBegin GL_QUADS; |
1150 | glBegin GL_QUADS; |
1009 | glVertex 0 , 0; |
1151 | glVertex 0 , 0; |
1010 | glVertex 0 , $self->{h}; |
1152 | glVertex 0 , $self->{h}; |
1011 | glVertex $self->{w}, $self->{h}; |
1153 | glVertex $self->{w}, $self->{h}; |
1012 | glVertex $self->{w}, 0; |
1154 | glVertex $self->{w}, 0; |
1013 | glEnd; |
1155 | glEnd; |
|
|
1156 | glDisable GL_BLEND; |
1014 | |
1157 | |
1015 | $self->SUPER::_draw; |
1158 | $self->SUPER::_draw; |
1016 | |
1159 | |
1017 | #TODO: force update every cursor change :( |
1160 | #TODO: force update every cursor change :( |
1018 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1161 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
… | |
… | |
1022 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1165 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1023 | glEnd; |
1166 | glEnd; |
1024 | } |
1167 | } |
1025 | } |
1168 | } |
1026 | |
1169 | |
1027 | ############################################################################# |
|
|
1028 | |
|
|
1029 | package CFClient::UI::Slider; |
1170 | package CFClient::UI::LineEntry; |
1030 | |
1171 | |
1031 | use strict; |
1172 | our @ISA = CFClient::UI::Entry::; |
1032 | |
1173 | |
|
|
1174 | use SDL; |
1033 | use SDL::OpenGL; |
1175 | use SDL::OpenGL; |
1034 | |
1176 | |
1035 | our @ISA = CFClient::UI::DrawBG::; |
1177 | sub key_down { |
1036 | |
|
|
1037 | sub size_request { |
|
|
1038 | my ($self) = @_; |
1178 | my ($self, $ev) = @_; |
1039 | |
1179 | |
1040 | my $w = 50; |
1180 | my $sym = $ev->key_sym; |
1041 | my $h = 10; |
|
|
1042 | |
1181 | |
1043 | $self->{vertical} ? ($h, $w) : ($w, $h) |
1182 | if ($sym == SDLK_RETURN) { |
|
|
1183 | $self->emit (activate => $self->get_text); |
|
|
1184 | $self->update; |
|
|
1185 | |
|
|
1186 | } else { |
|
|
1187 | $self->SUPER::key_down ($ev); |
|
|
1188 | } |
|
|
1189 | |
1044 | } |
1190 | } |
|
|
1191 | |
|
|
1192 | ############################################################################# |
|
|
1193 | |
|
|
1194 | package CFClient::UI::Button; |
|
|
1195 | |
|
|
1196 | our @ISA = CFClient::UI::Label::; |
|
|
1197 | |
|
|
1198 | use SDL; |
|
|
1199 | use SDL::OpenGL; |
|
|
1200 | |
|
|
1201 | my @tex = |
|
|
1202 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1203 | qw(b1_button_active.png); |
1045 | |
1204 | |
1046 | sub new { |
1205 | sub new { |
1047 | my $class = shift; |
1206 | my $class = shift; |
1048 | |
1207 | |
|
|
1208 | $class->SUPER::new ( |
|
|
1209 | padding => 4, |
|
|
1210 | fg => [1, 1, 1], |
|
|
1211 | bg => [1, 1, 1, 0.2], |
|
|
1212 | active_fg => [1, 1, 0], |
|
|
1213 | can_hover => 1, |
|
|
1214 | @_ |
|
|
1215 | ) |
|
|
1216 | } |
|
|
1217 | |
|
|
1218 | sub button_up { |
|
|
1219 | my ($self, $ev, $x, $y) = @_; |
|
|
1220 | |
|
|
1221 | if ($x >= 0 && $x < $self->{w} |
|
|
1222 | && $y >= 0 && $y < $self->{h}) { |
|
|
1223 | $self->emit ("activate"); |
|
|
1224 | } |
|
|
1225 | } |
|
|
1226 | |
|
|
1227 | sub _draw { |
|
|
1228 | my ($self) = @_; |
|
|
1229 | |
|
|
1230 | local $self->{fg} = $self->{fg}; |
|
|
1231 | my $tex = $tex[0]; |
|
|
1232 | |
|
|
1233 | glEnable GL_BLEND; |
|
|
1234 | glEnable GL_TEXTURE_2D; |
|
|
1235 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1236 | |
|
|
1237 | if ($GRAB == $self) { |
|
|
1238 | $self->{fg} = $self->{active_fg}; |
|
|
1239 | } |
|
|
1240 | |
|
|
1241 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
1242 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1243 | |
|
|
1244 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
|
|
1245 | |
|
|
1246 | glDisable GL_TEXTURE_2D; |
|
|
1247 | glDisable GL_BLEND; |
|
|
1248 | |
|
|
1249 | $self->SUPER::_draw; |
|
|
1250 | } |
|
|
1251 | |
|
|
1252 | ############################################################################# |
|
|
1253 | |
|
|
1254 | package CFClient::UI::CheckBox; |
|
|
1255 | |
|
|
1256 | our @ISA = CFClient::UI::DrawBG::; |
|
|
1257 | |
|
|
1258 | use SDL; |
|
|
1259 | use SDL::OpenGL; |
|
|
1260 | |
|
|
1261 | sub new { |
|
|
1262 | my $class = shift; |
|
|
1263 | |
|
|
1264 | $class->SUPER::new ( |
|
|
1265 | padding => 2, |
|
|
1266 | fg => [1, 1, 1], |
|
|
1267 | active_fg => [1, 1, 0], |
|
|
1268 | state => 0, |
|
|
1269 | can_hover => 1, |
|
|
1270 | @_ |
|
|
1271 | ) |
|
|
1272 | } |
|
|
1273 | |
|
|
1274 | sub size_request { |
|
|
1275 | my ($self) = @_; |
|
|
1276 | |
|
|
1277 | ($self->{padding} * 2 + 6) x 2 |
|
|
1278 | } |
|
|
1279 | |
|
|
1280 | sub size_allocate { |
|
|
1281 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1282 | |
|
|
1283 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1284 | } |
|
|
1285 | |
|
|
1286 | sub button_down { |
|
|
1287 | my ($self, $ev, $x, $y) = @_; |
|
|
1288 | |
|
|
1289 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
|
|
1290 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
|
|
1291 | $self->{state} = !$self->{state}; |
|
|
1292 | $self->emit (changed => $self->{state}); |
|
|
1293 | } |
|
|
1294 | } |
|
|
1295 | |
|
|
1296 | sub _draw { |
|
|
1297 | my ($self) = @_; |
|
|
1298 | |
|
|
1299 | $self->SUPER::_draw; |
|
|
1300 | |
|
|
1301 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
|
|
1302 | |
|
|
1303 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
|
|
1304 | |
|
|
1305 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
|
|
1306 | |
|
|
1307 | glBegin GL_LINE_LOOP; |
|
|
1308 | glVertex 0 , 0; |
|
|
1309 | glVertex 0 , $s; |
|
|
1310 | glVertex $s, $s; |
|
|
1311 | glVertex $s, 0; |
|
|
1312 | glEnd; |
|
|
1313 | |
|
|
1314 | if ($self->{state}) { |
|
|
1315 | glBegin GL_LINES; |
|
|
1316 | glVertex 0 , 0; |
|
|
1317 | glVertex $s, $s; |
|
|
1318 | glVertex $s, 0; |
|
|
1319 | glVertex 0 , $s; |
|
|
1320 | glEnd; |
|
|
1321 | } |
|
|
1322 | } |
|
|
1323 | |
|
|
1324 | ############################################################################# |
|
|
1325 | |
|
|
1326 | package CFClient::UI::Slider; |
|
|
1327 | |
|
|
1328 | use strict; |
|
|
1329 | |
|
|
1330 | use SDL::OpenGL; |
|
|
1331 | |
|
|
1332 | our @ISA = CFClient::UI::DrawBG::; |
|
|
1333 | |
|
|
1334 | my @tex = |
|
|
1335 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1336 | qw(s1_slider.png s1_slider_bg.png); |
|
|
1337 | |
|
|
1338 | sub new { |
|
|
1339 | my $class = shift; |
|
|
1340 | |
1049 | # range [value, low, high, page] |
1341 | # range [value, low, high, page] |
1050 | |
1342 | |
|
|
1343 | # TODO: 0-width page |
|
|
1344 | # TODO: req_w/h are wrong with vertical |
|
|
1345 | # TODO: calculations are off |
1051 | $class->SUPER::new ( |
1346 | my $self = $class->SUPER::new ( |
1052 | fg => [1, 1, 1], |
1347 | fg => [1, 1, 1], |
1053 | active_fg => [0, 0, 0], |
1348 | active_fg => [0, 0, 0], |
1054 | range => [0, 0, 100, 10], |
1349 | range => [0, 0, 100, 10], |
|
|
1350 | req_w => 40, |
|
|
1351 | req_h => 13, |
1055 | vertical => 1, |
1352 | vertical => 0, |
|
|
1353 | can_hover => 1, |
1056 | @_ |
1354 | @_ |
1057 | ) |
1355 | ); |
|
|
1356 | |
|
|
1357 | $self |
|
|
1358 | } |
|
|
1359 | |
|
|
1360 | sub size_request { |
|
|
1361 | my ($self) = @_; |
|
|
1362 | |
|
|
1363 | my $w = $self->{req_w}; |
|
|
1364 | my $h = $self->{req_h}; |
|
|
1365 | |
|
|
1366 | $self->{vertical} ? ($h, $w) : ($w, $h) |
1058 | } |
1367 | } |
1059 | |
1368 | |
1060 | sub button_down { |
1369 | sub button_down { |
1061 | my ($self, $ev, $x, $y) = @_; |
1370 | my ($self, $ev, $x, $y) = @_; |
1062 | |
1371 | |
… | |
… | |
1101 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1410 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1102 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1411 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1103 | |
1412 | |
1104 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1413 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1105 | |
1414 | |
|
|
1415 | # the inner_* stuff is for generating a padding for the slider handle, |
|
|
1416 | # so that the handle doesn't leave the texture. This calculation isn't 100% |
|
|
1417 | # correct propably, but it does the job for now |
|
|
1418 | my $inner_pad = 5; # 5% of width for slider bg texture border |
|
|
1419 | my $inner_pad_px = ($w / 100) * $inner_pad; # % to pixels |
|
|
1420 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1421 | |
1106 | $page = int $page * $w / ($hi - $lo); |
1422 | $page = int $page * $inner_w / ($hi - $lo); |
1107 | $value = int +($value - $lo) * $w / ($hi - $lo); |
1423 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
1108 | |
1424 | |
1109 | $w -= $page; |
1425 | $w -= $page; |
1110 | $page &= ~1; |
1426 | $page &= ~1; |
1111 | glTranslate $page * 0.5, 0, 0; |
1427 | glTranslate $page * 0.5, 0, 0; |
|
|
1428 | $page ||= 2; |
1112 | |
1429 | |
1113 | glColor @$fg; |
|
|
1114 | glBegin GL_LINES; |
|
|
1115 | glVertex 0, 0; glVertex 0, $h; |
|
|
1116 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
|
|
1117 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
|
|
1118 | glEnd; |
|
|
1119 | |
|
|
1120 | my $knob_a = $value - $page * 0.5; |
1430 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
1121 | my $knob_b = $value + $page * 0.5; |
1431 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
1122 | |
1432 | |
1123 | glBegin GL_QUADS; |
1433 | glEnable GL_BLEND; |
1124 | glColor @$fg; |
1434 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1125 | glVertex $knob_a, 0; |
1435 | glEnable GL_TEXTURE_2D; |
1126 | glVertex $knob_a, $h; |
1436 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1127 | glVertex $knob_b, $h; |
|
|
1128 | glVertex $knob_b, 0; |
|
|
1129 | |
1437 | |
1130 | if ($knob_a < $knob_b - 2) { |
1438 | # draw background |
1131 | glColor @$bg; |
1439 | $tex[1]->draw_quad (0, 0, $w, $h); |
1132 | glVertex $knob_a + 1, 1; |
1440 | |
1133 | glVertex $knob_a + 1, $h - 1; |
1441 | # draw handle |
1134 | glVertex $knob_b - 1, $h - 1; |
1442 | $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); |
1135 | glVertex $knob_b - 1, 1; |
1443 | |
|
|
1444 | glDisable GL_BLEND; |
|
|
1445 | glDisable GL_TEXTURE_2D; |
|
|
1446 | } |
|
|
1447 | |
|
|
1448 | ############################################################################# |
|
|
1449 | |
|
|
1450 | package CFClient::UI::TextView; |
|
|
1451 | |
|
|
1452 | our @ISA = CFClient::UI::HBox::; |
|
|
1453 | |
|
|
1454 | use SDL::OpenGL; |
|
|
1455 | |
|
|
1456 | sub new { |
|
|
1457 | my $class = shift; |
|
|
1458 | |
|
|
1459 | my $self = $class->SUPER::new ( |
|
|
1460 | par => [], |
|
|
1461 | @_, |
|
|
1462 | children => [ |
|
|
1463 | (new CFClient::UI::Empty expand => 1), |
|
|
1464 | (new CFClient::UI::Slider vertical => 1), |
|
|
1465 | ], |
|
|
1466 | ); |
|
|
1467 | |
|
|
1468 | $self |
|
|
1469 | } |
|
|
1470 | |
|
|
1471 | sub add_paragraph { |
|
|
1472 | my ($self, $color, $text) = @_; |
|
|
1473 | |
|
|
1474 | push @{$self->{par}}, [$color, $text]; |
|
|
1475 | my $count = scalar @{$self->{par}}; |
|
|
1476 | $self->{children}[1]{range} = [$count - 1, 0, $count, 1]; |
|
|
1477 | $self->{children}[1]->update; |
|
|
1478 | } |
|
|
1479 | |
|
|
1480 | sub _draw { |
|
|
1481 | my ($self) = @_; |
|
|
1482 | |
|
|
1483 | my $par = $self->{par}; |
|
|
1484 | |
|
|
1485 | my $bottom = (scalar @$par) - 1; |
|
|
1486 | |
|
|
1487 | my $w = $self->{children}[0]{w}; |
|
|
1488 | my $y = $self->{h}; |
|
|
1489 | |
|
|
1490 | glEnable GL_BLEND; |
|
|
1491 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1492 | glEnable GL_TEXTURE_2D; |
|
|
1493 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1494 | |
|
|
1495 | # TODO: everything! |
|
|
1496 | while ($y > 0 && $bottom >= 0) { |
|
|
1497 | my $par = $par->[$bottom--]; |
|
|
1498 | |
|
|
1499 | my $layout = new CFClient::Layout; |
|
|
1500 | |
|
|
1501 | $layout->set_height ($::FONTSIZE); |
|
|
1502 | $layout->set_width ($w); |
|
|
1503 | $layout->set_text ($par->[1]); |
|
|
1504 | my $tex = new_from_layout CFClient::Texture $layout; |
|
|
1505 | $y -= $tex->{h}; |
|
|
1506 | |
|
|
1507 | glColor @{ $par->[0] }; |
|
|
1508 | $tex->draw_quad (0, $y); |
1136 | } |
1509 | } |
1137 | glEnd; |
1510 | |
|
|
1511 | glDisable GL_TEXTURE_2D; |
|
|
1512 | glDisable GL_BLEND; |
1138 | } |
1513 | } |
1139 | |
1514 | |
1140 | ############################################################################# |
1515 | ############################################################################# |
1141 | |
1516 | |
1142 | package CFClient::UI::MapWidget; |
1517 | package CFClient::UI::MapWidget; |
… | |
… | |
1152 | |
1527 | |
1153 | sub new { |
1528 | sub new { |
1154 | my $class = shift; |
1529 | my $class = shift; |
1155 | |
1530 | |
1156 | $class->SUPER::new ( |
1531 | $class->SUPER::new ( |
1157 | z => -1, |
1532 | z => -1, |
|
|
1533 | can_focus => 1, |
1158 | list => (glGenLists 1), |
1534 | list => (glGenLists 1), |
1159 | @_ |
1535 | @_ |
1160 | ) |
1536 | ) |
1161 | } |
1537 | } |
1162 | |
1538 | |
1163 | sub key_down { |
1539 | sub key_down { |
… | |
… | |
1167 | sub key_up { |
1543 | sub key_up { |
1168 | } |
1544 | } |
1169 | |
1545 | |
1170 | sub size_request { |
1546 | sub size_request { |
1171 | ( |
1547 | ( |
1172 | 1 + int $::WIDTH / 32, |
1548 | 1 + 32 * int $::WIDTH / 32, |
1173 | 1 + int $::HEIGHT / 32, |
1549 | 1 + 32 * int $::HEIGHT / 32, |
1174 | ) |
1550 | ) |
1175 | } |
1551 | } |
1176 | |
1552 | |
1177 | sub update { |
1553 | sub update { |
1178 | my ($self) = @_; |
1554 | my ($self) = @_; |
1179 | |
1555 | |
1180 | $self->{need_update} = 1; |
1556 | $self->{need_update} = 1; |
1181 | $self->SUPER::update; |
1557 | $self->SUPER::update; |
1182 | } |
1558 | } |
1183 | |
1559 | |
1184 | sub _draw { |
1560 | sub draw { |
1185 | my ($self) = @_; |
1561 | my ($self) = @_; |
1186 | |
1562 | |
|
|
1563 | $self->{need_update}++;#d# |
1187 | if (delete $self->{need_update}) { |
1564 | if (delete $self->{need_update}) { |
1188 | glNewList $self->{list}, GL_COMPILE; |
1565 | glNewList $self->{list}, GL_COMPILE; |
1189 | |
1566 | |
1190 | my $mx = $::CONN->{mapx}; |
|
|
1191 | my $my = $::CONN->{mapy}; |
|
|
1192 | |
|
|
1193 | my $map = $::CONN->{map}; |
|
|
1194 | |
|
|
1195 | my ($xofs, $yofs); |
|
|
1196 | |
|
|
1197 | my $sw = 1 + int $::WIDTH / 32; |
1567 | my $sw = int $::WIDTH / 32; |
1198 | my $sh = 1 + int $::HEIGHT / 32; |
1568 | my $sh = int $::HEIGHT / 32; |
1199 | |
1569 | |
1200 | if ($::CONN->{mapw} > $sw) { |
1570 | if ($::MAP) { |
1201 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
1571 | my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh); |
1202 | } else { |
|
|
1203 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
|
|
1204 | } |
|
|
1205 | |
1572 | |
1206 | if ($::CONN->{maph} > $sh) { |
1573 | if ($::CFG->{fow_enable}) { |
1207 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
1574 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
1208 | } else { |
1575 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
1209 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
1576 | glConvolutionFilter2D |
1210 | } |
1577 | GL_CONVOLUTION_2D, |
1211 | |
1578 | GL_ALPHA, |
1212 | glEnable GL_TEXTURE_2D; |
1579 | 3, 3, |
1213 | glEnable GL_BLEND; |
1580 | GL_ALPHA, GL_FLOAT, |
1214 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1581 | pack "f*", |
1215 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1582 | 0.1, 0.1, 0.1, |
1216 | |
1583 | 0.1, 0.2, 0.1, |
1217 | my $sw4 = ($sw + 3) & ~3; |
1584 | 0.1, 0.1, 0.1, |
1218 | my $darkness = "\x00" x ($sw4 * $sh); |
|
|
1219 | |
|
|
1220 | for my $x (0 .. $sw - 1) { |
|
|
1221 | my $row = $map->[$x + $xofs]; |
|
|
1222 | for my $y (0 .. $sh - 1) { |
|
|
1223 | |
|
|
1224 | my $cell = $row->[$y + $yofs] |
|
|
1225 | or next; |
1585 | ; |
1226 | |
1586 | glEnable GL_CONVOLUTION_2D; |
1227 | my $dark = $cell->[0]; |
|
|
1228 | if ($dark < 0) { |
|
|
1229 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
|
|
1230 | } else { |
|
|
1231 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
|
|
1232 | } |
1587 | } |
1233 | |
1588 | |
1234 | for my $num (grep $_, @$cell[1,2,3]) { |
1589 | my $tex = new CFClient::Texture |
1235 | my $tex = $::CONN->{face}[$num]{texture} || next; |
1590 | w => $w, |
1236 | |
1591 | h => $h, |
1237 | my ($w, $h) = @$tex{qw(w h)}; |
1592 | data => $data, |
|
|
1593 | internalformat => GL_ALPHA, |
|
|
1594 | format => GL_ALPHA; |
1238 | |
1595 | |
1239 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
1596 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
1240 | } |
1597 | |
|
|
1598 | glEnable GL_BLEND; |
|
|
1599 | glEnable GL_TEXTURE_2D; |
|
|
1600 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1601 | |
|
|
1602 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1603 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1604 | |
|
|
1605 | glDisable GL_TEXTURE_2D; |
|
|
1606 | glDisable GL_BLEND; |
1241 | } |
1607 | } |
1242 | } |
1608 | } |
1243 | |
1609 | |
1244 | # if (1) { # higher quality darkness |
|
|
1245 | # $lighting =~ s/(.)/$1$1$1/gs; |
|
|
1246 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
|
|
1247 | # |
|
|
1248 | # $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear"); |
|
|
1249 | # |
|
|
1250 | # $lighting = $pb->get_pixels; |
|
|
1251 | # $lighting =~ s/(.)../$1/gs; |
|
|
1252 | # } |
|
|
1253 | |
|
|
1254 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1255 | |
|
|
1256 | $darkness = new CFClient::Texture |
|
|
1257 | w => $sw4, |
|
|
1258 | h => $sh, |
|
|
1259 | data => $darkness, |
|
|
1260 | internalformat => GL_ALPHA, |
|
|
1261 | format => GL_ALPHA; |
|
|
1262 | |
|
|
1263 | glColor 0.45, 0.45, 0.45, 1; |
|
|
1264 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
|
|
1265 | |
|
|
1266 | glDisable GL_TEXTURE_2D; |
|
|
1267 | glDisable GL_BLEND; |
|
|
1268 | |
|
|
1269 | glEndList; |
1610 | glEndList; |
1270 | } |
1611 | } |
1271 | |
1612 | |
1272 | glCallList $self->{list}; |
1613 | glCallList $self->{list}; |
|
|
1614 | |
|
|
1615 | if ($FOCUS != $self) { |
|
|
1616 | glColor 64/255, 64/255, 64/255; |
|
|
1617 | glLogicOp GL_AND; |
|
|
1618 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1619 | glBegin GL_QUADS; |
|
|
1620 | glVertex 0, 0; |
|
|
1621 | glVertex 0, $::HEIGHT; |
|
|
1622 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1623 | glVertex $::WIDTH, 0; |
|
|
1624 | glEnd; |
|
|
1625 | glDisable GL_COLOR_LOGIC_OP; |
|
|
1626 | } |
1273 | } |
1627 | } |
1274 | |
1628 | |
1275 | my %DIR = ( |
1629 | my %DIR = ( |
1276 | SDLK_KP8, [1, "north"], |
1630 | SDLK_KP8, [1, "north"], |
1277 | SDLK_KP9, [2, "northeast"], |
1631 | SDLK_KP9, [2, "northeast"], |
… | |
… | |
1293 | |
1647 | |
1294 | my $mod = $ev->key_mod; |
1648 | my $mod = $ev->key_mod; |
1295 | my $sym = $ev->key_sym; |
1649 | my $sym = $ev->key_sym; |
1296 | |
1650 | |
1297 | if ($sym == SDLK_KP5) { |
1651 | if ($sym == SDLK_KP5) { |
1298 | $::CONN->send ("command stay fire"); |
1652 | $::CONN->user_send ("command stay fire"); |
|
|
1653 | } elsif ($sym == SDLK_a) { |
|
|
1654 | $::CONN->user_send ("command apply"); |
1299 | } elsif (exists $DIR{$sym}) { |
1655 | } elsif (exists $DIR{$sym}) { |
1300 | if ($mod & KMOD_SHIFT) { |
1656 | if ($mod & KMOD_SHIFT) { |
1301 | $self->{shft}++; |
1657 | $self->{shft}++; |
1302 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
1658 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
1303 | } elsif ($mod & KMOD_CTRL) { |
1659 | } elsif ($mod & KMOD_CTRL) { |
1304 | $self->{ctrl}++; |
1660 | $self->{ctrl}++; |
1305 | $::CONN->send ("command run $DIR{$sym}[0]"); |
1661 | $::CONN->user_send ("command run $DIR{$sym}[0]"); |
1306 | } else { |
1662 | } else { |
1307 | $::CONN->send ("command $DIR{$sym}[1]"); |
1663 | $::CONN->user_send ("command $DIR{$sym}[1]"); |
1308 | } |
1664 | } |
1309 | } |
1665 | } |
1310 | } |
1666 | } |
1311 | |
1667 | |
1312 | sub key_up { |
1668 | sub key_up { |
… | |
… | |
1314 | |
1670 | |
1315 | my $mod = $ev->key_mod; |
1671 | my $mod = $ev->key_mod; |
1316 | my $sym = $ev->key_sym; |
1672 | my $sym = $ev->key_sym; |
1317 | |
1673 | |
1318 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
1674 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
1319 | $::CONN->send ("command fire_stop"); |
1675 | $::CONN->user_send ("command fire_stop"); |
1320 | } |
1676 | } |
1321 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
1677 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
1322 | $::CONN->send ("command run_stop"); |
1678 | $::CONN->user_send ("command run_stop"); |
1323 | } |
1679 | } |
1324 | } |
1680 | } |
1325 | |
1681 | |
1326 | ############################################################################# |
1682 | ############################################################################# |
1327 | |
1683 | |
… | |
… | |
1365 | glPopMatrix; |
1721 | glPopMatrix; |
1366 | } |
1722 | } |
1367 | |
1723 | |
1368 | ############################################################################# |
1724 | ############################################################################# |
1369 | |
1725 | |
|
|
1726 | package CFClient::UI::Flopper; |
|
|
1727 | |
|
|
1728 | our @ISA = CFClient::UI::Button::; |
|
|
1729 | |
|
|
1730 | sub new { |
|
|
1731 | my $class = shift; |
|
|
1732 | |
|
|
1733 | my $self = $class->SUPER::new ( |
|
|
1734 | state => 0, |
|
|
1735 | connect_activate => \&toggle_flopper, |
|
|
1736 | @_ |
|
|
1737 | ); |
|
|
1738 | |
|
|
1739 | if ($self->{state}) { |
|
|
1740 | $self->{state} = 0; |
|
|
1741 | $self->toggle_flopper; |
|
|
1742 | } |
|
|
1743 | |
|
|
1744 | $self |
|
|
1745 | } |
|
|
1746 | |
|
|
1747 | sub toggle_flopper { |
|
|
1748 | my ($self) = @_; |
|
|
1749 | |
|
|
1750 | # TODO: use animation |
|
|
1751 | if ($self->{state} = !$self->{state}) { |
|
|
1752 | $CFClient::UI::TOPLEVEL->add ($self->{other}); |
|
|
1753 | $self->{other}->move ( |
|
|
1754 | ($::WIDTH - $self->{other}{w}) * 0.5, |
|
|
1755 | ($::HEIGHT - $self->{other}{h}) * 0.5, |
|
|
1756 | ); |
|
|
1757 | } else { |
|
|
1758 | $CFClient::UI::TOPLEVEL->remove ($self->{other}); |
|
|
1759 | } |
|
|
1760 | } |
|
|
1761 | |
|
|
1762 | ############################################################################# |
|
|
1763 | |
1370 | package CFClient::UI::Toplevel; |
1764 | package CFClient::UI::Toplevel; |
1371 | |
1765 | |
1372 | our @ISA = CFClient::UI::Container::; |
1766 | our @ISA = CFClient::UI::Container::; |
1373 | |
1767 | |
1374 | sub size_request { |
1768 | sub size_request { |