… | |
… | |
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 | } |
… | |
… | |
200 | my ($self) = @_; |
208 | my ($self) = @_; |
201 | |
209 | |
202 | warn "no draw defined for $self\n"; |
210 | warn "no draw defined for $self\n"; |
203 | } |
211 | } |
204 | |
212 | |
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 { |
213 | sub find_widget { |
217 | my ($self, $x, $y) = @_; |
214 | my ($self, $x, $y) = @_; |
218 | |
215 | |
219 | return $self |
216 | return $self |
220 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
217 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
221 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
218 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
222 | |
219 | |
223 | () |
220 | () |
224 | } |
221 | } |
225 | |
222 | |
226 | sub del_parent { $_[0]->{parent} = undef } |
|
|
227 | |
|
|
228 | sub set_parent { |
223 | sub set_parent { |
229 | my ($self, $par) = @_; |
224 | my ($self, $par) = @_; |
230 | |
225 | |
231 | $self->{parent} = $par; |
226 | $self->{parent} = $par; |
232 | Scalar::Util::weaken $self->{parent}; |
227 | Scalar::Util::weaken $self->{parent}; |
… | |
… | |
276 | |
271 | |
277 | # range [value, low, high, page] |
272 | # range [value, low, high, page] |
278 | |
273 | |
279 | $class->SUPER::new ( |
274 | $class->SUPER::new ( |
280 | bg => [0, 0, 0, 0.2], |
275 | bg => [0, 0, 0, 0.2], |
281 | active_bg => [1, 1, 1], |
276 | active_bg => [1, 1, 1, 0.5], |
282 | @_ |
277 | @_ |
283 | ) |
278 | ) |
284 | } |
279 | } |
285 | |
280 | |
286 | sub _draw { |
281 | sub _draw { |
… | |
… | |
537 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
532 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
538 | |
533 | |
539 | sub size_request { |
534 | sub size_request { |
540 | my ($self) = @_; |
535 | my ($self) = @_; |
541 | |
536 | |
542 | my ($w, $h) = |
537 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
543 | $self->{user_w} && $self->{user_h} |
538 | |
544 | ? ($self->{user_w}, $self->{user_h}) |
|
|
545 | : ($self->SUPER::size_request); |
539 | my ($w, $h) = $self->SUPER::size_request; |
546 | |
540 | |
547 | $h += $tex[1]->{h}; |
541 | $h += $tex[1]->{h}; |
548 | $h += $tex[4]->{h}; |
542 | $h += $tex[4]->{h}; |
549 | $w += $tex[2]->{w}; |
543 | $w += $tex[2]->{w}; |
550 | $w += $tex[3]->{w}; |
544 | $w += $tex[3]->{w}; |
… | |
… | |
643 | |
637 | |
644 | my $bg = $tex[0]; |
638 | my $bg = $tex[0]; |
645 | |
639 | |
646 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
640 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
647 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
641 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
648 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
|
|
649 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
|
|
650 | |
642 | |
651 | my $rep_x = $cw / $bg->{w}; |
643 | my $rep_x = $cw / $bg->{w}; |
652 | my $rep_y = $ch / $bg->{h}; |
644 | my $rep_y = $ch / $bg->{h}; |
653 | |
645 | |
654 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
646 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
… | |
… | |
667 | our @ISA = CFClient::UI::Base::; |
659 | our @ISA = CFClient::UI::Base::; |
668 | |
660 | |
669 | use List::Util qw(max sum); |
661 | use List::Util qw(max sum); |
670 | |
662 | |
671 | use SDL::OpenGL; |
663 | use SDL::OpenGL; |
|
|
664 | |
|
|
665 | sub new { |
|
|
666 | my $class = shift; |
|
|
667 | |
|
|
668 | $class->SUPER::new ( |
|
|
669 | col_expand => [], |
|
|
670 | @_ |
|
|
671 | ) |
|
|
672 | } |
672 | |
673 | |
673 | sub add { |
674 | sub add { |
674 | my ($self, $x, $y, $chld) = @_; |
675 | my ($self, $x, $y, $chld) = @_; |
675 | |
676 | |
676 | $self->{children}[$y][$x] = $chld; |
677 | $self->{children}[$y][$x] = $chld; |
… | |
… | |
706 | my ($self) = @_; |
707 | my ($self) = @_; |
707 | |
708 | |
708 | my ($ws, $hs) = $self->get_wh; |
709 | my ($ws, $hs) = $self->get_wh; |
709 | |
710 | |
710 | ( |
711 | ( |
711 | (List::Util::sum @$ws), |
712 | (sum @$ws), |
712 | (List::Util::sum @$hs), |
713 | (sum @$hs), |
713 | ) |
714 | ) |
714 | } |
715 | } |
715 | |
716 | |
716 | sub size_allocate { |
717 | sub size_allocate { |
717 | my ($self, $x, $y, $w, $h) = @_; |
718 | my ($self, $x, $y, $w, $h) = @_; |
718 | |
719 | |
719 | $self->_size_allocate ($x, $y, $w, $h) or return; |
720 | $self->_size_allocate ($x, $y, $w, $h) or return; |
720 | |
721 | |
721 | my ($ws, $hs) = $self->get_wh; |
722 | my ($ws, $hs) = $self->get_wh; |
722 | |
723 | |
723 | my $req_w = List::Util::sum @$ws; |
724 | my $req_w = sum @$ws; |
724 | my $req_h = List::Util::sum @$hs; |
725 | my $req_h = sum @$hs; |
|
|
726 | |
|
|
727 | # TODO: nicer code && do row_expand |
|
|
728 | my @col_expand = @{$self->{col_expand}}; |
|
|
729 | @col_expand = (1) x @$ws unless @col_expand; |
|
|
730 | my $col_expand = (sum @col_expand) || 1; |
725 | |
731 | |
726 | # linearly scale sizes |
732 | # linearly scale sizes |
727 | $_ *= $w / $req_w for @$ws; |
733 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
728 | $_ *= $h / $req_h for @$hs; |
734 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
729 | |
735 | |
730 | my $y; |
736 | my $y; |
731 | |
737 | |
732 | for my $r (0 .. $#{$self->{children}}) { |
738 | for my $r (0 .. $#{$self->{children}}) { |
733 | my $row = $self->{children}[$r] |
739 | my $row = $self->{children}[$r] |
… | |
… | |
735 | |
741 | |
736 | my $x = 0; |
742 | my $x = 0; |
737 | my $row_h = $hs->[$r]; |
743 | my $row_h = $hs->[$r]; |
738 | |
744 | |
739 | for my $c (0 .. $#$row) { |
745 | for my $c (0 .. $#$row) { |
740 | my $widget = $row->[$c] |
|
|
741 | or next; |
|
|
742 | |
|
|
743 | my $col_w = $ws->[$c]; |
746 | my $col_w = $ws->[$c]; |
744 | |
747 | |
|
|
748 | if (my $widget = $row->[$c]) { |
745 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
749 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
|
|
750 | } |
746 | |
751 | |
747 | $x += $col_w; |
752 | $x += $col_w; |
748 | } |
753 | } |
749 | |
754 | |
750 | $y += $row_h; |
755 | $y += $row_h; |
… | |
… | |
810 | |
815 | |
811 | my $req_h = List::Util::sum @h; |
816 | my $req_h = List::Util::sum @h; |
812 | |
817 | |
813 | if ($req_h > $h) { |
818 | if ($req_h > $h) { |
814 | # ah well, not enough space |
819 | # ah well, not enough space |
815 | $_ = $h[$_] * $h / $req_h for @h; |
820 | $_ *= $h / $req_h for @h; |
816 | } else { |
821 | } else { |
817 | my $exp = List::Util::sum map $_->{expand}, @$children; |
822 | my $exp = List::Util::sum map $_->{expand}, @$children; |
818 | $exp ||= 1; |
823 | $exp ||= 1; |
819 | |
824 | |
820 | for (0 .. $#$children) { |
825 | for (0 .. $#$children) { |
… | |
… | |
868 | |
873 | |
869 | my $req_h = List::Util::sum @h; |
874 | my $req_h = List::Util::sum @h; |
870 | |
875 | |
871 | if ($req_h > $h) { |
876 | if ($req_h > $h) { |
872 | # ah well, not enough space |
877 | # ah well, not enough space |
873 | $_ = $h[$_] * $h / $req_h for @h; |
878 | $_ *= $h / $req_h for @h; |
874 | } else { |
879 | } else { |
875 | my $exp = List::Util::sum map $_->{expand}, @$children; |
880 | my $exp = List::Util::sum map $_->{expand}, @$children; |
876 | $exp ||= 1; |
881 | $exp ||= 1; |
877 | |
882 | |
878 | for (0 .. $#$children) { |
883 | for (0 .. $#$children) { |
… | |
… | |
933 | |
938 | |
934 | $self->{text} = $text; |
939 | $self->{text} = $text; |
935 | $self->{layout}->set_markup ($text); |
940 | $self->{layout}->set_markup ($text); |
936 | |
941 | |
937 | delete $self->{texture}; |
942 | delete $self->{texture}; |
|
|
943 | # $self->{w} = $self->{h} = -1; |
938 | $self->update; |
944 | $self->update; |
939 | } |
945 | } |
940 | |
946 | |
941 | sub get_text { |
947 | sub get_text { |
942 | my ($self, $text) = @_; |
948 | my ($self, $text) = @_; |
… | |
… | |
975 | sub _draw { |
981 | sub _draw { |
976 | my ($self) = @_; |
982 | my ($self) = @_; |
977 | |
983 | |
978 | my $tex = $self->{texture} ||= do { |
984 | my $tex = $self->{texture} ||= do { |
979 | $self->{layout}->set_width ($self->{w}); |
985 | $self->{layout}->set_width ($self->{w}); |
|
|
986 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
980 | new_from_layout CFClient::Texture $self->{layout} |
987 | new_from_layout CFClient::Texture $self->{layout} |
981 | }; |
988 | }; |
982 | |
989 | |
983 | glEnable GL_BLEND; |
990 | glEnable GL_BLEND; |
984 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
991 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
… | |
… | |
990 | my $x = |
997 | my $x = |
991 | $self->{align} < 0 ? $self->{padding} |
998 | $self->{align} < 0 ? $self->{padding} |
992 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
999 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
993 | : ($self->{w} - $tex->{w}) * 0.5; |
1000 | : ($self->{w} - $tex->{w}) * 0.5; |
994 | |
1001 | |
995 | $tex->draw_quad ($x, $self->{padding}); |
1002 | glTranslate $x, $self->{padding}, 0; |
|
|
1003 | $tex->draw_quad (0, 0); |
996 | |
1004 | |
997 | glDisable GL_TEXTURE_2D; |
1005 | glDisable GL_TEXTURE_2D; |
998 | glDisable GL_BLEND; |
1006 | glDisable GL_BLEND; |
999 | } |
1007 | } |
1000 | |
1008 | |
… | |
… | |
1011 | my $class = shift; |
1019 | my $class = shift; |
1012 | |
1020 | |
1013 | $class->SUPER::new ( |
1021 | $class->SUPER::new ( |
1014 | fg => [1, 1, 1], |
1022 | fg => [1, 1, 1], |
1015 | bg => [0, 0, 0, 0.2], |
1023 | bg => [0, 0, 0, 0.2], |
1016 | active_bg => [1, 1, 1], |
1024 | active_bg => [1, 1, 1, 0.5], |
1017 | active_fg => [0, 0, 0], |
1025 | active_fg => [0, 0, 0], |
1018 | @_ |
1026 | @_ |
1019 | ) |
1027 | ) |
1020 | } |
1028 | } |
1021 | |
1029 | |
… | |
… | |
1024 | |
1032 | |
1025 | $self->{last_activity} = $::NOW; |
1033 | $self->{last_activity} = $::NOW; |
1026 | |
1034 | |
1027 | $self->{text} = $text; |
1035 | $self->{text} = $text; |
1028 | $self->{layout}->set_width ($self->{w}); |
1036 | $self->{layout}->set_width ($self->{w}); |
|
|
1037 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
1029 | |
1038 | |
1030 | $text =~ s/./*/g if $self->{hidden}; |
1039 | $text =~ s/./*/g if $self->{hidden}; |
1031 | |
1040 | |
1032 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1041 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1033 | |
1042 | |
… | |
… | |
1154 | } |
1163 | } |
1155 | } |
1164 | } |
1156 | |
1165 | |
1157 | ############################################################################# |
1166 | ############################################################################# |
1158 | |
1167 | |
|
|
1168 | package CFClient::UI::Button; |
|
|
1169 | |
|
|
1170 | our @ISA = CFClient::UI::Label::; |
|
|
1171 | |
|
|
1172 | use SDL; |
|
|
1173 | use SDL::OpenGL; |
|
|
1174 | |
|
|
1175 | sub new { |
|
|
1176 | my $class = shift; |
|
|
1177 | |
|
|
1178 | $class->SUPER::new ( |
|
|
1179 | padding => 4, |
|
|
1180 | fg => [1, 1, 1], |
|
|
1181 | bg => [1, 1, 1, 0.2], |
|
|
1182 | active_bg => [0, 0, 0, 0.5], |
|
|
1183 | active_fg => [1, 1, 0], |
|
|
1184 | border_fg => [1, 1, 0], |
|
|
1185 | @_ |
|
|
1186 | ) |
|
|
1187 | } |
|
|
1188 | |
|
|
1189 | sub button_up { |
|
|
1190 | my ($self, $ev, $x, $y) = @_; |
|
|
1191 | |
|
|
1192 | if ($x >= 0 && $x < $self->{w} |
|
|
1193 | && $y >= 0 && $y < $self->{h}) { |
|
|
1194 | $self->emit ("activate"); |
|
|
1195 | } |
|
|
1196 | } |
|
|
1197 | |
|
|
1198 | sub _draw { |
|
|
1199 | my ($self) = @_; |
|
|
1200 | |
|
|
1201 | local $self->{fg} = $self->{fg}; |
|
|
1202 | |
|
|
1203 | glEnable GL_BLEND; |
|
|
1204 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1205 | |
|
|
1206 | glTranslate 0.375, 0.375, 0; # make line and polyogon coordinates behave similarly |
|
|
1207 | |
|
|
1208 | glBegin GL_LINE_LOOP; |
|
|
1209 | glColor @{$self->{border_fg}}; |
|
|
1210 | glVertex 1 , 1; |
|
|
1211 | glVertex 1 , $self->{h} - 2; |
|
|
1212 | glVertex $self->{w} - 2, $self->{h} - 2; |
|
|
1213 | glVertex $self->{w} - 2, 1; |
|
|
1214 | glEnd; |
|
|
1215 | |
|
|
1216 | if ($GRAB == $self) { |
|
|
1217 | glColor @{$self->{active_bg}}; |
|
|
1218 | $self->{fg} = $self->{active_fg}; |
|
|
1219 | } else { |
|
|
1220 | glColor @{$self->{bg}}; |
|
|
1221 | } |
|
|
1222 | |
|
|
1223 | glBegin GL_QUADS; |
|
|
1224 | glVertex 2 , 2; |
|
|
1225 | glVertex 2 , $self->{h} - 2; |
|
|
1226 | glVertex $self->{w} - 2, $self->{h} - 2; |
|
|
1227 | glVertex $self->{w} - 2, 2; |
|
|
1228 | glEnd; |
|
|
1229 | glDisable GL_BLEND; |
|
|
1230 | |
|
|
1231 | $self->SUPER::_draw; |
|
|
1232 | } |
|
|
1233 | |
|
|
1234 | ############################################################################# |
|
|
1235 | |
1159 | package CFClient::UI::Slider; |
1236 | package CFClient::UI::Slider; |
1160 | |
1237 | |
1161 | use strict; |
1238 | use strict; |
1162 | |
1239 | |
1163 | use SDL::OpenGL; |
1240 | use SDL::OpenGL; |
… | |
… | |
1241 | $value = int +($value - $lo) * $w / ($hi - $lo); |
1318 | $value = int +($value - $lo) * $w / ($hi - $lo); |
1242 | |
1319 | |
1243 | $w -= $page; |
1320 | $w -= $page; |
1244 | $page &= ~1; |
1321 | $page &= ~1; |
1245 | glTranslate $page * 0.5, 0, 0; |
1322 | glTranslate $page * 0.5, 0, 0; |
|
|
1323 | $page ||= 2; |
1246 | |
1324 | |
1247 | glColor @$fg; |
1325 | glColor @$fg; |
1248 | glBegin GL_LINES; |
1326 | glBegin GL_LINES; |
1249 | glVertex 0, 0; glVertex 0, $h; |
1327 | glVertex 0, 0; glVertex 0, $h; |
1250 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
1328 | glVertex $w - 1, 0; glVertex $w - 1, $h; |