… | |
… | |
61 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
61 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
62 | |
62 | |
63 | if ($widget != $HOVER) { |
63 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
64 | my $hover = $HOVER; $HOVER = $widget; |
65 | |
65 | |
66 | $hover->update if $hover; |
66 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
68 | } |
68 | } |
69 | |
69 | |
70 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
70 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
|
|
71 | } |
|
|
72 | |
|
|
73 | # convert position array to integers |
|
|
74 | sub harmonize { |
|
|
75 | my ($vals) = @_; |
|
|
76 | |
|
|
77 | my $rem = 0; |
|
|
78 | |
|
|
79 | for ($vals) { |
|
|
80 | my $i = int $_ + $rem; |
|
|
81 | $rem += $_ - $i; |
|
|
82 | $_ = $i; |
|
|
83 | } |
71 | } |
84 | } |
72 | |
85 | |
73 | ############################################################################# |
86 | ############################################################################# |
74 | |
87 | |
75 | package CFClient::UI::Base; |
88 | package CFClient::UI::Base; |
… | |
… | |
439 | } |
452 | } |
440 | |
453 | |
441 | sub render_chld { |
454 | sub render_chld { |
442 | my ($self) = @_; |
455 | my ($self) = @_; |
443 | |
456 | |
444 | $self->{texture} = |
457 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
445 | CFClient::Texture->new_from_opengl ( |
458 | glClearColor 0, 0, 0, 1; |
446 | $self->{w}, $self->{h}, sub { $self->child->draw } |
459 | glClear GL_COLOR_BUFFER_BIT; |
447 | ); |
460 | $self->child->draw; |
|
|
461 | }; |
448 | } |
462 | } |
449 | |
463 | |
450 | sub size_allocate { |
464 | sub size_allocate { |
451 | my ($self, $x, $y, $w, $h) = @_; |
465 | my ($self, $x, $y, $w, $h) = @_; |
452 | |
466 | |
… | |
… | |
639 | # TODO: repeat texture not scale |
653 | # TODO: repeat texture not scale |
640 | my $rep_x = $cw / $bg->{w}; |
654 | my $rep_x = $cw / $bg->{w}; |
641 | my $rep_y = $ch / $bg->{h}; |
655 | my $rep_y = $ch / $bg->{h}; |
642 | |
656 | |
643 | glColor @{ $self->{bg} }; |
657 | glColor @{ $self->{bg} }; |
|
|
658 | |
|
|
659 | $bg->{s} = $rep_x; |
|
|
660 | $bg->{t} = $rep_y; |
|
|
661 | $bg->{wrap_mode} = 1; |
644 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
662 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
645 | |
663 | |
646 | glDisable GL_TEXTURE_2D; |
664 | glDisable GL_TEXTURE_2D; |
647 | glDisable GL_BLEND; |
665 | glDisable GL_BLEND; |
648 | |
666 | |
… | |
… | |
727 | my $col_expand = (sum @col_expand) || 1; |
745 | my $col_expand = (sum @col_expand) || 1; |
728 | |
746 | |
729 | # linearly scale sizes |
747 | # linearly scale sizes |
730 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
748 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
731 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
749 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
|
|
750 | |
|
|
751 | CFClient::UI::harmonize $ws; |
|
|
752 | CFClient::UI::harmonize $hs; |
732 | |
753 | |
733 | my $y; |
754 | my $y; |
734 | |
755 | |
735 | for my $r (0 .. $#{$self->{children}}) { |
756 | for my $r (0 .. $#{$self->{children}}) { |
736 | my $row = $self->{children}[$r] |
757 | my $row = $self->{children}[$r] |
… | |
… | |
824 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
845 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
825 | $h[$_] = $alloc_h; |
846 | $h[$_] = $alloc_h; |
826 | } |
847 | } |
827 | } |
848 | } |
828 | |
849 | |
|
|
850 | CFClient::UI::harmonize \@h; |
|
|
851 | |
829 | my $y = 0; |
852 | my $y = 0; |
830 | for (0 .. $#$children) { |
853 | for (0 .. $#$children) { |
831 | my $child = $children->[$_]; |
854 | my $child = $children->[$_]; |
832 | my $h = $h[$_]; |
855 | my $h = $h[$_]; |
833 | $child->size_allocate ($y, 0, $h, $w); |
856 | $child->size_allocate ($y, 0, $h, $w); |
… | |
… | |
878 | |
901 | |
879 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
902 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
880 | } |
903 | } |
881 | } |
904 | } |
882 | |
905 | |
|
|
906 | CFClient::UI::harmonize \@h; |
|
|
907 | |
883 | my $y = 0; |
908 | my $y = 0; |
884 | for (0 .. $#$children) { |
909 | for (0 .. $#$children) { |
885 | my $child = $children->[$_]; |
910 | my $child = $children->[$_]; |
886 | my $h = $h[$_]; |
911 | my $h = $h[$_]; |
887 | $child->size_allocate (0, $y, $w, $h); |
912 | $child->size_allocate (0, $y, $w, $h); |
… | |
… | |
900 | |
925 | |
901 | sub new { |
926 | sub new { |
902 | my ($class, %arg) = @_; |
927 | my ($class, %arg) = @_; |
903 | |
928 | |
904 | my $self = $class->SUPER::new ( |
929 | my $self = $class->SUPER::new ( |
905 | fg => [1, 1, 1], |
930 | fg => [1, 1, 1], |
906 | height => $::FONTSIZE, |
931 | fontsize => $::FONTSIZE, |
907 | text => "", |
932 | text => "", |
908 | align => -1, |
933 | align => -1, |
909 | padding => 2, |
934 | padding => 2, |
910 | layout => new CFClient::Layout, |
935 | layout => new CFClient::Layout, |
911 | %arg |
936 | %arg |
912 | ); |
937 | ); |
913 | |
938 | |
914 | $self->set_text ($self->{text}); |
939 | $self->set_text ($self->{text}); |
915 | |
940 | |
… | |
… | |
945 | |
970 | |
946 | sub size_request { |
971 | sub size_request { |
947 | my ($self) = @_; |
972 | my ($self) = @_; |
948 | |
973 | |
949 | $self->{layout}->set_width; |
974 | $self->{layout}->set_width; |
950 | $self->{layout}->set_height ($self->{height}); |
975 | $self->{layout}->set_height ($self->{fontsize}); |
951 | my ($w, $h) = $self->{layout}->size; |
976 | my ($w, $h) = $self->{layout}->size; |
952 | |
977 | |
953 | ( |
978 | ( |
954 | $w + $self->{padding} * 2, |
979 | $w + $self->{padding} * 2, |
955 | $h + $self->{padding} * 2, |
980 | $h + $self->{padding} * 2, |
… | |
… | |
974 | sub _draw { |
999 | sub _draw { |
975 | my ($self) = @_; |
1000 | my ($self) = @_; |
976 | |
1001 | |
977 | my $tex = $self->{texture} ||= do { |
1002 | my $tex = $self->{texture} ||= do { |
978 | $self->{layout}->set_width ($self->{w}); |
1003 | $self->{layout}->set_width ($self->{w}); |
979 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
1004 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
980 | new_from_layout CFClient::Texture $self->{layout} |
1005 | new_from_layout CFClient::Texture $self->{layout} |
981 | }; |
1006 | }; |
982 | |
1007 | |
983 | glEnable GL_BLEND; |
1008 | glEnable GL_BLEND; |
984 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1009 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
985 | glEnable GL_TEXTURE_2D; |
1010 | glEnable GL_TEXTURE_2D; |
986 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1011 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
987 | |
1012 | |
988 | glColor @{$self->{fg}}; |
1013 | glColor @{$self->{fg}}; |
989 | |
1014 | |
990 | my $x = |
1015 | my $x = |
991 | $self->{align} < 0 ? $self->{padding} |
1016 | $self->{align} < 0 ? $self->{padding} |
992 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1017 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
993 | : ($self->{w} - $tex->{w}) * 0.5; |
1018 | : ($self->{w} - $tex->{w}) * 0.5; |
994 | |
1019 | |
995 | glTranslate int $x, int +($self->{h} - $tex->{h}) * 0.5, 0; |
1020 | $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); |
996 | $tex->draw_quad (0, 0); |
|
|
997 | |
1021 | |
998 | glDisable GL_TEXTURE_2D; |
1022 | glDisable GL_TEXTURE_2D; |
999 | glDisable GL_BLEND; |
1023 | glDisable GL_BLEND; |
1000 | } |
1024 | } |
1001 | |
1025 | |
… | |
… | |
1023 | } |
1047 | } |
1024 | |
1048 | |
1025 | sub _set_text { |
1049 | sub _set_text { |
1026 | my ($self, $text) = @_; |
1050 | my ($self, $text) = @_; |
1027 | |
1051 | |
|
|
1052 | my $old_text = $self->{text}; |
|
|
1053 | |
1028 | $self->{last_activity} = $::NOW; |
1054 | $self->{last_activity} = $::NOW; |
1029 | |
1055 | |
1030 | $self->{text} = $text; |
1056 | $self->{text} = $text; |
1031 | $self->{layout}->set_width ($self->{w}); |
1057 | $self->{layout}->set_width ($self->{w}); |
1032 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
1058 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
1033 | |
1059 | |
1034 | $text =~ s/./*/g if $self->{hidden}; |
1060 | $text =~ s/./*/g if $self->{hidden}; |
1035 | |
1061 | |
1036 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1062 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1037 | |
1063 | |
1038 | $text = substr $text, 0, $self->{cursor}; |
1064 | $text = substr $text, 0, $self->{cursor}; |
1039 | utf8::encode $text; |
1065 | utf8::encode $text; |
1040 | |
1066 | |
1041 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
1067 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
1068 | |
|
|
1069 | $self->emit (changed => $self->{text}) # XXX: is this the right place to do this? |
|
|
1070 | if $old_text ne $self->{text}; |
1042 | } |
1071 | } |
1043 | |
1072 | |
1044 | sub size_request { |
1073 | sub size_request { |
1045 | my ($self) = @_; |
1074 | my ($self) = @_; |
1046 | |
1075 | |
… | |
… | |
1085 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1114 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1086 | } elsif ($sym == SDLK_HOME) { |
1115 | } elsif ($sym == SDLK_HOME) { |
1087 | $self->{cursor} = 0; |
1116 | $self->{cursor} = 0; |
1088 | } elsif ($sym == SDLK_END) { |
1117 | } elsif ($sym == SDLK_END) { |
1089 | $self->{cursor} = length $text; |
1118 | $self->{cursor} = length $text; |
|
|
1119 | } elsif ($sym == SDLK_ESCAPE) { |
|
|
1120 | $self->emit ('escape'); |
1090 | } elsif ($uni) { |
1121 | } elsif ($uni) { |
1091 | substr $text, $self->{cursor}++, 0, chr $uni; |
1122 | substr $text, $self->{cursor}++, 0, chr $uni; |
1092 | } |
1123 | } |
1093 | |
1124 | |
1094 | $self->_set_text ($text); |
1125 | $self->_set_text ($text); |
… | |
… | |
1156 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1187 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1157 | glEnd; |
1188 | glEnd; |
1158 | } |
1189 | } |
1159 | } |
1190 | } |
1160 | |
1191 | |
|
|
1192 | package CFClient::UI::LineEntry; |
|
|
1193 | |
|
|
1194 | our @ISA = CFClient::UI::Entry::; |
|
|
1195 | |
|
|
1196 | use SDL; |
|
|
1197 | use SDL::OpenGL; |
|
|
1198 | |
|
|
1199 | sub key_down { |
|
|
1200 | my ($self, $ev) = @_; |
|
|
1201 | |
|
|
1202 | my $sym = $ev->key_sym; |
|
|
1203 | |
|
|
1204 | if ($sym == SDLK_RETURN) { |
|
|
1205 | $self->emit (activate => $self->get_text); |
|
|
1206 | $self->update; |
|
|
1207 | |
|
|
1208 | } else { |
|
|
1209 | $self->SUPER::key_down ($ev); |
|
|
1210 | } |
|
|
1211 | |
|
|
1212 | } |
|
|
1213 | |
1161 | ############################################################################# |
1214 | ############################################################################# |
1162 | |
1215 | |
1163 | package CFClient::UI::Button; |
1216 | package CFClient::UI::Button; |
1164 | |
1217 | |
1165 | our @ISA = CFClient::UI::Label::; |
1218 | our @ISA = CFClient::UI::Label::; |
… | |
… | |
1222 | |
1275 | |
1223 | package CFClient::UI::CheckBox; |
1276 | package CFClient::UI::CheckBox; |
1224 | |
1277 | |
1225 | our @ISA = CFClient::UI::DrawBG::; |
1278 | our @ISA = CFClient::UI::DrawBG::; |
1226 | |
1279 | |
|
|
1280 | my @tex = |
|
|
1281 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1282 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
|
|
1283 | |
1227 | use SDL; |
1284 | use SDL; |
1228 | use SDL::OpenGL; |
1285 | use SDL::OpenGL; |
1229 | |
1286 | |
1230 | sub new { |
1287 | sub new { |
1231 | my $class = shift; |
1288 | my $class = shift; |
… | |
… | |
1268 | $self->SUPER::_draw; |
1325 | $self->SUPER::_draw; |
1269 | |
1326 | |
1270 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
1327 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
1271 | |
1328 | |
1272 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1329 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1273 | |
1330 | |
1274 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1331 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1275 | |
1332 | |
1276 | glBegin GL_LINE_LOOP; |
1333 | glEnable GL_BLEND; |
1277 | glVertex 0 , 0; |
1334 | glEnable GL_TEXTURE_2D; |
1278 | glVertex 0 , $s; |
1335 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1279 | glVertex $s, $s; |
|
|
1280 | glVertex $s, 0; |
|
|
1281 | glEnd; |
|
|
1282 | |
1336 | |
1283 | if ($self->{state}) { |
1337 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
1284 | glBegin GL_LINES; |
1338 | |
1285 | glVertex 0 , 0; |
1339 | $tex->draw_quad (0, 0, $s, $s); |
1286 | glVertex $s, $s; |
1340 | |
1287 | glVertex $s, 0; |
1341 | glDisable GL_TEXTURE_2D; |
1288 | glVertex 0 , $s; |
1342 | glDisable GL_BLEND; |
1289 | glEnd; |
|
|
1290 | } |
|
|
1291 | } |
1343 | } |
1292 | |
1344 | |
1293 | ############################################################################# |
1345 | ############################################################################# |
1294 | |
1346 | |
1295 | package CFClient::UI::Slider; |
1347 | package CFClient::UI::Slider; |
… | |
… | |
1297 | use strict; |
1349 | use strict; |
1298 | |
1350 | |
1299 | use SDL::OpenGL; |
1351 | use SDL::OpenGL; |
1300 | |
1352 | |
1301 | our @ISA = CFClient::UI::DrawBG::; |
1353 | our @ISA = CFClient::UI::DrawBG::; |
|
|
1354 | |
|
|
1355 | my @tex = |
|
|
1356 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1357 | qw(s1_slider.png s1_slider_bg.png); |
1302 | |
1358 | |
1303 | sub new { |
1359 | sub new { |
1304 | my $class = shift; |
1360 | my $class = shift; |
1305 | |
1361 | |
1306 | # range [value, low, high, page] |
1362 | # range [value, low, high, page] |
… | |
… | |
1314 | range => [0, 0, 100, 10], |
1370 | range => [0, 0, 100, 10], |
1315 | req_w => 40, |
1371 | req_w => 40, |
1316 | req_h => 13, |
1372 | req_h => 13, |
1317 | vertical => 0, |
1373 | vertical => 0, |
1318 | can_hover => 1, |
1374 | can_hover => 1, |
|
|
1375 | inner_pad => 5, |
1319 | @_ |
1376 | @_ |
1320 | ); |
1377 | ); |
1321 | |
1378 | |
1322 | $self |
1379 | $self |
1323 | } |
1380 | } |
… | |
… | |
1344 | if ($GRAB == $self) { |
1401 | if ($GRAB == $self) { |
1345 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1402 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1346 | |
1403 | |
1347 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
1404 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
1348 | |
1405 | |
|
|
1406 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
1407 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1408 | |
|
|
1409 | $x -= $inner_pad_px; # substract the padding |
1349 | $x = $x * ($hi - $lo) / $w + $lo; |
1410 | $x = $x * ($hi - $lo) / $inner_w + $lo; |
1350 | $x = $lo if $x < $lo; |
1411 | $x = $lo if $x < $lo; |
1351 | $x = $hi - $page if $x > $hi - $page; |
1412 | $x = $hi - $page if $x > $hi - $page; |
1352 | $self->{range}[0] = $x; |
1413 | $self->{range}[0] = $x; |
1353 | |
1414 | |
1354 | $self->emit (changed => $x); |
1415 | $self->emit (changed => $x); |
1355 | $self->update; |
1416 | $self->update; |
1356 | } |
1417 | } |
|
|
1418 | } |
|
|
1419 | |
|
|
1420 | # the inner_* stuff is for generating a padding for the slider handle, |
|
|
1421 | # so that the handle doesn't leave the texture. This calculation isn't 100% |
|
|
1422 | # correct propably, but it does the job for now |
|
|
1423 | sub _calc_inner_pad_px { |
|
|
1424 | my ($self, $w) = @_; |
|
|
1425 | ($w / 100) * $self->{inner_pad} # % to pixels |
1357 | } |
1426 | } |
1358 | |
1427 | |
1359 | sub _draw { |
1428 | sub _draw { |
1360 | my ($self) = @_; |
1429 | my ($self) = @_; |
1361 | |
1430 | |
… | |
… | |
1375 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1444 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1376 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1445 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1377 | |
1446 | |
1378 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1447 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1379 | |
1448 | |
|
|
1449 | $hi = $value + 1 if $lo == $hi; |
|
|
1450 | |
|
|
1451 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
1452 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1453 | |
1380 | $page = int $page * $w / ($hi - $lo); |
1454 | $page = int $page * $inner_w / ($hi - $lo); |
1381 | $value = int +($value - $lo) * $w / ($hi - $lo); |
1455 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
1382 | |
1456 | |
1383 | $w -= $page; |
1457 | $w -= $page; |
1384 | $page &= ~1; |
1458 | $page &= ~1; |
1385 | glTranslate $page * 0.5, 0, 0; |
1459 | glTranslate $page * 0.5, 0, 0; |
1386 | $page ||= 2; |
1460 | $page ||= 2; |
1387 | |
1461 | |
1388 | glColor @$fg; |
|
|
1389 | glBegin GL_LINES; |
|
|
1390 | glVertex 0, 0; glVertex 0, $h; |
|
|
1391 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
|
|
1392 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
|
|
1393 | glEnd; |
|
|
1394 | |
|
|
1395 | my $knob_a = $value - $page * 0.5; |
1462 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
1396 | my $knob_b = $value + $page * 0.5; |
1463 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
1397 | |
1464 | |
1398 | glBegin GL_QUADS; |
1465 | glEnable GL_BLEND; |
1399 | glColor @$fg; |
1466 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1400 | glVertex $knob_a, 0; |
1467 | glEnable GL_TEXTURE_2D; |
1401 | glVertex $knob_a, $h; |
1468 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1402 | glVertex $knob_b, $h; |
|
|
1403 | glVertex $knob_b, 0; |
|
|
1404 | |
1469 | |
1405 | if ($knob_a < $knob_b - 2) { |
1470 | # draw background |
1406 | glColor @$bg; |
1471 | $tex[1]->draw_quad (0, 0, $w, $h); |
1407 | glVertex $knob_a + 1, 1; |
1472 | |
1408 | glVertex $knob_a + 1, $h - 1; |
1473 | # draw handle |
1409 | glVertex $knob_b - 1, $h - 1; |
1474 | $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); |
1410 | glVertex $knob_b - 1, 1; |
1475 | |
1411 | } |
1476 | glDisable GL_BLEND; |
1412 | glEnd; |
1477 | glDisable GL_TEXTURE_2D; |
1413 | } |
1478 | } |
1414 | |
1479 | |
1415 | ############################################################################# |
1480 | ############################################################################# |
1416 | |
1481 | |
1417 | package CFClient::UI::TextView; |
1482 | package CFClient::UI::TextView; |
… | |
… | |
1422 | |
1487 | |
1423 | sub new { |
1488 | sub new { |
1424 | my $class = shift; |
1489 | my $class = shift; |
1425 | |
1490 | |
1426 | my $self = $class->SUPER::new ( |
1491 | my $self = $class->SUPER::new ( |
|
|
1492 | req_w => $::WIDTH / 6, |
|
|
1493 | req_h => $::HEIGHT / 6, |
|
|
1494 | fontsize => $::FONTSIZE, |
|
|
1495 | @_, |
|
|
1496 | |
|
|
1497 | layout => (new CFClient::Layout), |
1427 | par => [], |
1498 | par => [], |
1428 | @_, |
1499 | height => 0, |
1429 | children => [ |
1500 | children => [ |
1430 | (new CFClient::UI::Empty expand => 1), |
1501 | (new CFClient::UI::Empty expand => 1), |
1431 | (new CFClient::UI::Slider vertical => 1), |
1502 | (new CFClient::UI::Slider vertical => 1), |
1432 | ], |
1503 | ], |
1433 | ); |
1504 | ); |
1434 | |
1505 | |
|
|
1506 | $self->{children}[1]->connect (changed => sub { |
|
|
1507 | $self->update; |
|
|
1508 | }); |
|
|
1509 | |
1435 | $self |
1510 | $self |
|
|
1511 | } |
|
|
1512 | |
|
|
1513 | sub set_fontsize { |
|
|
1514 | my ($self, $fontsize) = @_; |
|
|
1515 | |
|
|
1516 | $self->{fontsize} = $fontsize; |
|
|
1517 | $self->reflow; |
|
|
1518 | } |
|
|
1519 | |
|
|
1520 | sub text_height { |
|
|
1521 | my ($self, $text) = @_; |
|
|
1522 | |
|
|
1523 | my $layout = $self->{layout}; |
|
|
1524 | |
|
|
1525 | $layout->set_height ($self->{fontsize}); |
|
|
1526 | $layout->set_width ($self->{w}); |
|
|
1527 | $layout->set_text ($text); |
|
|
1528 | |
|
|
1529 | ($layout->size)[1] |
|
|
1530 | } |
|
|
1531 | |
|
|
1532 | sub reflow { |
|
|
1533 | my ($self) = @_; |
|
|
1534 | |
|
|
1535 | $self->{need_reflow}++; |
|
|
1536 | $self->update; |
|
|
1537 | } |
|
|
1538 | |
|
|
1539 | sub size_request { |
|
|
1540 | my ($self) = @_; |
|
|
1541 | |
|
|
1542 | ($self->{req_w}, $self->{req_h}) |
|
|
1543 | } |
|
|
1544 | |
|
|
1545 | sub size_allocate { |
|
|
1546 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1547 | |
|
|
1548 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
|
|
1549 | |
|
|
1550 | $self->{layout}->set_height ($self->{fontsize}); |
|
|
1551 | $self->{layout}->set_width ($self->{w}); |
|
|
1552 | |
|
|
1553 | $self->reflow; |
1436 | } |
1554 | } |
1437 | |
1555 | |
1438 | sub add_paragraph { |
1556 | sub add_paragraph { |
1439 | my ($self, $color, $text) = @_; |
1557 | my ($self, $color, $text) = @_; |
1440 | |
1558 | |
|
|
1559 | #TODO: intelligently "reformat" paragraph |
|
|
1560 | |
|
|
1561 | my $height = $self->text_height ($text); |
|
|
1562 | |
|
|
1563 | $self->{height} += $height; |
|
|
1564 | |
1441 | push @{$self->{par}}, [$color, $text]; |
1565 | push @{$self->{par}}, [$height, $color, $text]; |
1442 | my $count = scalar @{$self->{par}}; |
1566 | |
1443 | $self->{children}[1]{range} = [$count - 1, 0, $count, 1]; |
1567 | $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; |
1444 | $self->{children}[1]->update; |
1568 | $self->{children}[1]->update; |
1445 | } |
1569 | } |
1446 | |
1570 | |
|
|
1571 | sub update { |
|
|
1572 | my ($self) = @_; |
|
|
1573 | |
|
|
1574 | $self->SUPER::update; |
|
|
1575 | |
|
|
1576 | return unless $self->{h} > 0; |
|
|
1577 | |
|
|
1578 | delete $self->{texture}; |
|
|
1579 | |
|
|
1580 | $TOPLEVEL->on_refresh ($self, sub { |
|
|
1581 | if (delete $self->{need_reflow}) { |
|
|
1582 | my $height = 0; |
|
|
1583 | |
|
|
1584 | $height += $_->[0] = $self->text_height ($_->[2]) |
|
|
1585 | for @{$self->{par}}; |
|
|
1586 | |
|
|
1587 | $self->{height} = $height; |
|
|
1588 | |
|
|
1589 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
|
|
1590 | |
|
|
1591 | delete $self->{texture}; |
|
|
1592 | } |
|
|
1593 | |
|
|
1594 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
|
|
1595 | glClearColor 0, 0, 0, 1; |
|
|
1596 | glClear GL_COLOR_BUFFER_BIT; |
|
|
1597 | |
|
|
1598 | glEnable GL_BLEND; |
|
|
1599 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1600 | glEnable GL_TEXTURE_2D; |
|
|
1601 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1602 | |
|
|
1603 | my $top = int $self->{children}[1]{range}[0]; |
|
|
1604 | |
|
|
1605 | my $y0 = $top; |
|
|
1606 | my $y1 = $top + $self->{h}; |
|
|
1607 | |
|
|
1608 | my $y = 0; |
|
|
1609 | |
|
|
1610 | my $layout = $self->{layout}; |
|
|
1611 | |
|
|
1612 | for my $par (@{$self->{par}}) { |
|
|
1613 | my $h = $par->[0]; |
|
|
1614 | |
|
|
1615 | if ($y0 < $y + $h && $y < $y1) { |
|
|
1616 | $layout->set_text ($par->[2]); |
|
|
1617 | |
|
|
1618 | glColor @{ $par->[1] }; |
|
|
1619 | my ($W, $H) = $layout->size; |
|
|
1620 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
|
|
1621 | } |
|
|
1622 | |
|
|
1623 | $y += $h; |
|
|
1624 | } |
|
|
1625 | |
|
|
1626 | glDisable GL_TEXTURE_2D; |
|
|
1627 | glDisable GL_BLEND; |
|
|
1628 | }; |
|
|
1629 | }); |
|
|
1630 | } |
|
|
1631 | |
1447 | sub _draw { |
1632 | sub _draw { |
1448 | my ($self) = @_; |
1633 | my ($self) = @_; |
1449 | |
1634 | |
1450 | my $par = $self->{par}; |
1635 | if ($self->{texture}) { |
1451 | |
|
|
1452 | my $bottom = (scalar @$par) - 1; |
|
|
1453 | |
|
|
1454 | my $w = $self->{children}[0]{w}; |
|
|
1455 | my $y = $self->{h}; |
|
|
1456 | |
|
|
1457 | glEnable GL_BLEND; |
|
|
1458 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1459 | glEnable GL_TEXTURE_2D; |
1636 | glEnable GL_TEXTURE_2D; |
1460 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1637 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1461 | |
1638 | $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); |
1462 | # TODO: everything! |
|
|
1463 | while ($y > 0 && $bottom >= 0) { |
|
|
1464 | my $par = $par->[$bottom--]; |
|
|
1465 | |
|
|
1466 | my $layout = new CFClient::Layout; |
|
|
1467 | |
|
|
1468 | $layout->set_height ($::FONTSIZE); |
|
|
1469 | $layout->set_width ($w); |
|
|
1470 | $layout->set_text ($par->[1]); |
|
|
1471 | my $tex = new_from_layout CFClient::Texture $layout; |
|
|
1472 | $y -= $tex->{h}; |
|
|
1473 | |
|
|
1474 | glColor @{ $par->[0] }; |
|
|
1475 | $tex->draw_quad (0, $y); |
|
|
1476 | } |
|
|
1477 | |
|
|
1478 | glDisable GL_TEXTURE_2D; |
1639 | glDisable GL_TEXTURE_2D; |
1479 | glDisable GL_BLEND; |
1640 | } |
|
|
1641 | |
|
|
1642 | $self->{children}[1]->draw; |
|
|
1643 | |
1480 | } |
1644 | } |
1481 | |
1645 | |
1482 | ############################################################################# |
1646 | ############################################################################# |
1483 | |
1647 | |
1484 | package CFClient::UI::MapWidget; |
1648 | package CFClient::UI::MapWidget; |
… | |
… | |
1508 | } |
1672 | } |
1509 | |
1673 | |
1510 | sub key_up { |
1674 | sub key_up { |
1511 | } |
1675 | } |
1512 | |
1676 | |
|
|
1677 | sub button_down { |
|
|
1678 | my ($self, $ev, $x, $y) = @_; |
|
|
1679 | |
|
|
1680 | $self->focus_in; |
|
|
1681 | |
|
|
1682 | if ($ev->button == 2) { |
|
|
1683 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
1684 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
|
|
1685 | |
|
|
1686 | $self->{motion} = sub { |
|
|
1687 | my ($ev, $x, $y) = @_; |
|
|
1688 | |
|
|
1689 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
1690 | |
|
|
1691 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
|
|
1692 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
|
|
1693 | |
|
|
1694 | $self->update; |
|
|
1695 | }; |
|
|
1696 | } |
|
|
1697 | } |
|
|
1698 | |
|
|
1699 | sub button_up { |
|
|
1700 | my ($self, $ev, $x, $y) = @_; |
|
|
1701 | |
|
|
1702 | delete $self->{motion}; |
|
|
1703 | } |
|
|
1704 | |
|
|
1705 | sub mouse_motion { |
|
|
1706 | my ($self, $ev, $x, $y) = @_; |
|
|
1707 | |
|
|
1708 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1709 | } |
|
|
1710 | |
1513 | sub size_request { |
1711 | sub size_request { |
1514 | ( |
1712 | ( |
1515 | 1 + 32 * int $::WIDTH / 32, |
1713 | 1 + 32 * int $::WIDTH / 32, |
1516 | 1 + 32 * int $::HEIGHT / 32, |
1714 | 1 + 32 * int $::HEIGHT / 32, |
1517 | ) |
1715 | ) |
… | |
… | |
1525 | } |
1723 | } |
1526 | |
1724 | |
1527 | sub draw { |
1725 | sub draw { |
1528 | my ($self) = @_; |
1726 | my ($self) = @_; |
1529 | |
1727 | |
1530 | $self->{need_update}++;#d# |
|
|
1531 | if (delete $self->{need_update}) { |
1728 | if (delete $self->{need_update}) { |
1532 | glNewList $self->{list}, GL_COMPILE; |
1729 | glNewList $self->{list}, GL_COMPILE; |
1533 | |
1730 | |
1534 | my $sw = int $::WIDTH / 32; |
|
|
1535 | my $sh = int $::HEIGHT / 32; |
|
|
1536 | |
|
|
1537 | if ($::MAP) { |
1731 | if ($::MAP) { |
|
|
1732 | my $sw = int $::WIDTH / 32; |
|
|
1733 | my $sh = int $::HEIGHT / 32; |
|
|
1734 | |
|
|
1735 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
|
|
1736 | my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
1737 | |
|
|
1738 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
|
|
1739 | |
1538 | my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh); |
1740 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
1539 | |
1741 | |
1540 | if ($::CFG->{fow_enable}) { |
1742 | if ($::CFG->{fow_enable}) { |
1541 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
1743 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
1542 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
1744 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
1543 | glConvolutionFilter2D |
1745 | glConvolutionFilter2D |
… | |
… | |
1561 | format => GL_ALPHA; |
1763 | format => GL_ALPHA; |
1562 | |
1764 | |
1563 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
1765 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
1564 | |
1766 | |
1565 | glEnable GL_BLEND; |
1767 | glEnable GL_BLEND; |
|
|
1768 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1566 | glEnable GL_TEXTURE_2D; |
1769 | glEnable GL_TEXTURE_2D; |
1567 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1770 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1568 | |
1771 | |
1569 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
1772 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
1570 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
1773 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
1571 | |
1774 | |
1572 | glDisable GL_TEXTURE_2D; |
1775 | glDisable GL_TEXTURE_2D; |
1573 | glDisable GL_BLEND; |
1776 | glDisable GL_BLEND; |
1574 | } |
1777 | } |
|
|
1778 | |
|
|
1779 | # HACK BEGIN |
|
|
1780 | { |
|
|
1781 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
1782 | my ($w, $h) = (250, 250); |
|
|
1783 | |
|
|
1784 | glEnable GL_BLEND; |
|
|
1785 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1786 | glEnable GL_TEXTURE_2D; |
|
|
1787 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1788 | |
|
|
1789 | CFClient::Texture->new ( |
|
|
1790 | w => $w, |
|
|
1791 | h => $h, |
|
|
1792 | data => $::MAP->mapmap ($w, $h), |
|
|
1793 | type => GL_UNSIGNED_INT_8_8_8_8_REV |
|
|
1794 | )->draw_quad (100, 100); |
|
|
1795 | |
|
|
1796 | glDisable GL_TEXTURE_2D; |
|
|
1797 | glDisable GL_BLEND; |
|
|
1798 | } |
|
|
1799 | # HACK END |
1575 | } |
1800 | } |
1576 | |
1801 | |
1577 | glEndList; |
1802 | glEndList; |
1578 | } |
1803 | } |
1579 | |
1804 | |
|
|
1805 | glPushMatrix; |
1580 | glCallList $self->{list}; |
1806 | glCallList $self->{list}; |
|
|
1807 | glPopMatrix; |
1581 | |
1808 | |
1582 | if ($FOCUS != $self) { |
1809 | if ($FOCUS != $self) { |
1583 | glColor 64/255, 64/255, 64/255; |
1810 | glColor 64/255, 64/255, 64/255; |
1584 | glLogicOp GL_AND; |
1811 | glLogicOp GL_AND; |
1585 | glEnable GL_COLOR_LOGIC_OP; |
1812 | glEnable GL_COLOR_LOGIC_OP; |
… | |
… | |
1617 | |
1844 | |
1618 | if ($sym == SDLK_KP5) { |
1845 | if ($sym == SDLK_KP5) { |
1619 | $::CONN->user_send ("command stay fire"); |
1846 | $::CONN->user_send ("command stay fire"); |
1620 | } elsif ($sym == SDLK_a) { |
1847 | } elsif ($sym == SDLK_a) { |
1621 | $::CONN->user_send ("command apply"); |
1848 | $::CONN->user_send ("command apply"); |
|
|
1849 | } elsif ($sym == SDLK_QUOTE) { |
|
|
1850 | $self->emit ('activate_console'); |
|
|
1851 | } elsif ($sym == SDLK_SLASH) { |
|
|
1852 | $self->emit ('activate_console' => '/'); |
1622 | } elsif (exists $DIR{$sym}) { |
1853 | } elsif (exists $DIR{$sym}) { |
1623 | if ($mod & KMOD_SHIFT) { |
1854 | if ($mod & KMOD_SHIFT) { |
1624 | $self->{shft}++; |
1855 | $self->{shft}++; |
1625 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
1856 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
1626 | } elsif ($mod & KMOD_CTRL) { |
1857 | } elsif ($mod & KMOD_CTRL) { |
… | |
… | |
1730 | |
1961 | |
1731 | package CFClient::UI::Toplevel; |
1962 | package CFClient::UI::Toplevel; |
1732 | |
1963 | |
1733 | our @ISA = CFClient::UI::Container::; |
1964 | our @ISA = CFClient::UI::Container::; |
1734 | |
1965 | |
|
|
1966 | use SDL::OpenGL; |
|
|
1967 | |
1735 | sub size_request { |
1968 | sub size_request { |
1736 | ($::WIDTH, $::HEIGHT) |
1969 | ($::WIDTH, $::HEIGHT) |
1737 | } |
1970 | } |
1738 | |
1971 | |
1739 | sub size_allocate { |
1972 | sub size_allocate { |
… | |
… | |
1764 | $self->SUPER::add ($widget); |
1997 | $self->SUPER::add ($widget); |
1765 | |
1998 | |
1766 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
1999 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
1767 | } |
2000 | } |
1768 | |
2001 | |
|
|
2002 | sub on_refresh { |
|
|
2003 | my ($self, $id, $cb) = @_; |
|
|
2004 | |
|
|
2005 | $self->{refresh_hook}{$id} = $cb; |
|
|
2006 | } |
|
|
2007 | |
1769 | sub draw { |
2008 | sub draw { |
1770 | my ($self) = @_; |
2009 | my ($self) = @_; |
1771 | |
2010 | |
|
|
2011 | while (my $rcb = delete $self->{refresh_hook}) { |
|
|
2012 | $_->() for values %$rcb; |
|
|
2013 | } |
|
|
2014 | |
|
|
2015 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
|
|
2016 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
2017 | glClear GL_COLOR_BUFFER_BIT; |
|
|
2018 | |
|
|
2019 | glMatrixMode GL_PROJECTION; |
|
|
2020 | glLoadIdentity; |
|
|
2021 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
|
|
2022 | glMatrixMode GL_MODELVIEW; |
|
|
2023 | glLoadIdentity; |
|
|
2024 | |
1772 | $self->_draw; |
2025 | $self->_draw; |
1773 | } |
2026 | } |
1774 | |
2027 | |
1775 | ############################################################################# |
2028 | ############################################################################# |
1776 | |
2029 | |