1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 863 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
|
/**
This module provides classes for the parsing of a JSON describing
a faust interface and the realization of that interface in SVG.
It also allows for interaction between this interface and a server.
@module Faust Web UI
@main Faust Web UI
**/
/**
Extension of the JavaScript String class.
@class String
@for
@constructor
**/
/**
Formats a string in a pythonic way. See
http://docs.python.org/2/library/string.html.
@method format
@for String
@param {Array} args The arguments to be inserted into the string.
@return {String} The formatted string.
**/
String.prototype.format = function (args) {
var str = this;
return str.replace(String.prototype.format_regex, function(item) {
var intVal = parseInt(item.substring(1, item.length - 1));
var replace;
if (intVal >= 0) {
replace = args[intVal];
}
else if (intVal === -1) {
replace = "{";
}
else if (intVal === -2) {
replace = "}";
}
else {
replace = "";
}
return replace;
});
}
/**
The regular expression used to find the insertion points
in a string that needs formatting.
@property format_regex
@for String
@type RegExp
**/
String.prototype.format_regex = new RegExp("{-?[0-9] }", "g");
/**
Extension of the JavaScript Array class.
@class Array
@for
@constructor
**/
/**
Finds the maximum of a numerical array.
@method max
@for Array
@return {Number} The maximum of the array.
**/
Array.prototype.max = function() {
var max = this[0];
var len = this.length;
for (var i = 1; i < len; i ) {
if (this[i] > max) {
max = this[i];
}
}
return max;
}
/**
Finds the minimum of a numerical array.
@method min
@for Array
@return {Number} The minimum of the array.
**/
Array.prototype.min = function() {
var min = this[0];
var len = this.length;
for (var i = 1; i < len; i ) {
if (this[i] < min) {
min = this[i];
}
}
return min;
}
/**
Finds the sum of a numerical array.
@method sum
@for Array
@return {Number} The sum of the array.
**/
Array.prototype.sum = function() {
var sum = 0;
for (var i = 0; i < this.length; i ) {
sum = this[i];
}
return sum;
}
/**
Provides the base class for the creation of a _f4u$t UI.
It functions more like a namespace than an object, so it
does not have a constructer and only contains classes and
static methods.
@class _f4u$t
@static
**/
var _f4u$t = {};
/**
Estimation of text height in layout computations.
@property TEXT_HEIGHT
@for _f4u$t
@type Number
@default 20
**/
_f4u$t.TEXT_HEIGHT = 20;
/**
Padding between text and other objects.
@property TEXT_PADDING
@for _f4u$t
@type Number
@default 10
**/
_f4u$t.TEXT_PADDING = 10;
/**
Width of value boxes.
@property VALUE_BOX_W
@for _f4u$t
@type Number
@default 60
**/
_f4u$t.VALUE_BOX_W = 60;
/**
Height of value boxes.
@property VALUE_BOX_H
@for _f4u$t
@type Number
@default 20
**/
_f4u$t.VALUE_BOX_H = _f4u$t.TEXT_HEIGHT;
/**
Padding between the bottom of the space an object occupies
and its text-box.
@property TEXT_BOX_PADDING
@for _f4u$t
@type Number
@default 3
**/
_f4u$t.TEXT_BOX_PADDING = 3;
// some convenience methods for inheritance
/**
Used as a placeholder to define constructors via
_f4u$t.extend
@method surrogateCtor
@for _f4u$t
@static
**/
_f4u$t.surrogateCtor = function() {}
/**
Used to make one class inherit from another.
@method extend
@for _f4u$t
@static
@param {Object} base The base class to be inherited from.
@param {Object} sub The class doing the inheriting.
**/
_f4u$t.extend = function(base, sub) {
// Copy the prototype from the base to setup inheritance
_f4u$t.surrogateCtor.prototype = base.prototype;
sub.prototype = new _f4u$t.surrogateCtor();
// Remember the constructor property was set wrong, let's fix it
sub.prototype.constructor = sub;
}
/**
Identity function.
@method identity
@for _f4u$t
@static
@param {Object} value The value to return
@return {Object} The input value
**/
_f4u$t.identity = function(value) {
return value;
}
/**
Enum-like constant for the X axis.
@property X_AXIS
@for _f4u$t
@type Number
@default 0
**/
_f4u$t.X_AXIS = 0;
/**
Enum-like constant for the Y axis.
@property Y_AXIS
@for _f4u$t
@type Number
@default 1
**/
_f4u$t.Y_AXIS = 1;
/**
Enum-like constant for no axes left to consult,
used in iterating.
@property NO_AXES
@for _f4u$t
@type Number
@default 2
**/
_f4u$t.NO_AXES = 2;
/**
Enum-like constant for the left side.
@property LEFT
@for _f4u$t
@type Number
@default -1
**/
_f4u$t.LEFT = -1;
/**
Enum-like constant for the right side.
@property RIGHT
@for _f4u$t
@type Number
@default 1
**/
_f4u$t.RIGHT = 1;
/**
Enum-like constant for the up side.
@property UP
@for _f4u$t
@type Number
@default -1
**/
_f4u$t.UP = -1;
/**
Enum-like constant for the down side.
@property DOWN
@for _f4u$t
@type Number
@default 1
**/
_f4u$t.DOWN = 1;
_f4u$t.CENTER = 0;
/**
Representation of none, null, the abyss, the ether, etc..
@property NULL
@for _f4u$t
@default null
**/
_f4u$t.NONE = null;
/**
RGB value of the color black in an array.
@for _f4u$t
@property BLACK
@type Array
@default [0,0,0]
**/
_f4u$t.BLACK = [0,0,0];
/**
RGB value of the color white in an array.
@property WHITE
@for _f4u$t
@type Array
@default [255,255,255]
**/
_f4u$t.WHITE = [255,255,255];
/**
RGB value of the color cyan in an array.
@property CYAN
@for _f4u$t
@type Array
@default [0,255,255]
**/
_f4u$t.CYAN = [0,255,255];
/**
RGB value of the color cyan in an array.
/**
RGB value of the color grey in an array.
@property GREY
@for _f4u$t
@type Array
@default [100,100,100]
**/
_f4u$t.GREY = [100,100,100];
/**
RGB value of the color pink in an array.
@property PINK
@for _f4u$t
@type Array
@default [233,150,122]
**/
_f4u$t.PINK = [233,150,122];
/**
RGB value of the color green in an array.
@property GREEN
@for _f4u$t
@type Array
@default [173,255,47]
**/
_f4u$t.GREEN = [173,255,47];
/**
RGB value of the color kermit in an array.
@property KERMIT
@for _f4u$t
@type Array
@default [47,243,160]
**/
_f4u$t.KERMIT = [47,243,160];
/**
RGB value of the color pale green in an array.
@property PALEGREEN
@for _f4u$t
@type Array
@default [152,251,152]
**/
_f4u$t.PALEGREEN = [152,251,152];
// INTERACTION WITH THE UI
/**
Information about the current objects being
interacted with. Holds subproperties id, moved
and value.
@property _I
@for _f4u$t
@type Object
@default {}
**/
_f4u$t._I = {};
/**
Id of the current value box interacted with.
@property _N
@for _f4u$t
@type String
@default ""
**/
_f4u$t._N = "";
/*
* SERVER INTERACTION
*/
/**
Object that pairs paths of objects on the server
with DOM ids.
@property PATHS_TO_IDS
@for _f4u$t
@type Object
@default {}
**/
_f4u$t.PATHS_TO_IDS = {};
_f4u$t.path_to_id = function (path, id) {
_f4u$t.PATHS_TO_IDS[path] = id;
}
/**
Rather than using lots of global variables (clutters namespace)
or using this.attribute (dangerous depending on browser and libraries),
we use _f4u$t.IDS_TO_ATTRIBUTES to hold all information for faust UI objects.
That way, the impact on the namespace of the global session as well
as the objects is minimal.
@property IDS_TO_ATTRIBUTES
@for _f4u$t
@type Object
@default {}
**/
_f4u$t.IDS_TO_ATTRIBUTES = {};
/**
Returns a random, soft, pretty color, represented
as 0-255 RGB values in an array, to act as a background
for layout managers.
@method magic_color
@for _f4u$t
@static
@return {Array} An array of three values, 0-255 for RGB.
**/
_f4u$t.magic_color = function() {
var r = Math.floor(Math.random() * 100) 156;
var g = Math.floor(Math.random() * 100) 156;
var b = 530 - r - g;
var v = [r,g,b];
v.sort(function() {return 0.5 - Math.random()}) // shuffles
return v;
}
/**
Returns a random string.
@method rand_string
@for _f4u$t
@static
@param {Number} n (optional) the length of the string to return
@return {String} a random string
**/
_f4u$t.rand_string = function(n)
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
if (!n) {
n = 8;
}
for (var i = 0; i < n; i ) {
text = possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
/**
Returns true if needle is the first part of haystack
@method first_part_matches
@for _f4u$t
@static
@param {String} haystack the string to search
@param {String} needle the search query
@return {Boolean} true if needle is the first part of the haystack
**/
_f4u$t.first_part_matches = function(haystack, needle) {
return haystack.indexOf(needle) == 0;
}
/**
Removes needle from the head of haystack.
@method remove_from_head_of_string
@for _f4u$t
@static
@param {String} haystack the string to operate on
@param {String} needle remove
@return {String} the modified string without needle
**/
_f4u$t.remove_from_head_of_string = function(haystack, needle) {
if (_f4u$t.first_part_matches(haystack, needle)) {
return haystack.substr(needle.length);
}
}
/**
The bounds for an accelerometer orientation.
@property orientation_bounds
@for _f4u$t
@type Object
@default {}
**/
_f4u$t.orientation_bounds = {
alpha : [0, 360],
beta : [-90, 90],
gamma : [-90, 90]
};
/**
Parses a string into an orientation
@method parse_orientation
@for _f4u$t
@static
@return {Object}
**/
_f4u$t.parse_orientation = function(s) {
var split = s.toString().split(" ");
while (split.indexOf("") >= 0) {
split.splce(split.indexOf(""), 1);
}
if (split.length == 0) {
return {};
}
if (['alpha','beta','gamma'].indexOf(split[0]) < 0) {
return {};
}
var itor = {1 : _f4u$t.orientation_bounds[split[0]][0], 2 : _f4u$t.orientation_bounds[split[0]][1] };
// this loop creates the rest of the array
for (var i in itor) {
if (split.length == i) {
split.push(itor[i]);
}
else {
split[i] = parseFloat(split[i]);
if (isNaN(split[i])) {
split[i] = itor[i];
}
}
}
return {angle : split[0], low : split[1], high : split[2]};
}
/**
Taking an axis _f4u$t.X_AXIS or _f4u$t.Y_AXIS, returns
the opposite axis.
@method other_axis
@for _f4u$t
@static
@param {Number} axis An axis, either _f4u$t.X_AXIS or _f4u$t.Y_AXIS.
@return {Number} The opposite axis.
**/
_f4u$t.other_axis = function(axis) {
return (axis 1) % _f4u$t.NO_AXES;
}
/**
Remaps a value in a range to another range.
@method remap
@for _f4u$t
@static
@param {Number} v The value to remap.
@param {Number} mn0 The minimum of the old range.
@param {Number} mx0 The maximum of the old range.
@param {Number} mn1 The minimum of the new range.
@param {Number} mx1 The maximum of the new range.
@return {Number} The value v remapped to the new range.
**/
_f4u$t.remap = function(v, mn0, mx0, mn1, mx1) {
var p = 1.0 * (v - mn0) / (mx0 - mn0);
return p * (mx1 - mn1) mn1;
}
/**
Bounds a value between two numbers.
@method bound
@for _f4u$t
@static
@param {Number} v The value to bound.
@param {Number} m One side of the bound (either the min or max).
@param {Number} n The other side of the bound (either min or max).
@return {Number} Bounded value, meaning either unchanged or cropped at the min/max.
**/
_f4u$t.bound = function(v,m,n) {
var mn = Math.min(m,n);
var mx = Math.max(m,n);
if (v < mn) { return mn; }
if (v > mx) { return mx; }
return v;
}
/**
Bounds a value between two numbers, avoiding large leaps
from a previous value
@method bound
@for _f4u$t
@static
@param {Number} aval The value to bound.
@param {Number} pval The previous value
@param {Number} l The lower bound
@param {Number} h The upper bound
@return {Number} The value bounded. If the previous value was the upper
bound, we remain on this to avoid large leaps.
**/
_f4u$t.bound_and_avoid_large_leaps = function(aval, pval, l, h, epsilon) {
if (!epsilon) {
epsilon = 0;
}
if (l > aval) {
if (Math.abs(pval - h) <= epsilon) {
return l;
}
}
else if (aval > h) {
if (Math.abs(pval - l) <= epsilon) {
return h;
}
}
// if neither of the above are true, free to move by the difference
else {
return aval;
}
// corner case - we avoid large leaps
return pval;
}
/**
Quantizes a value between two numbers at a given step.
@method quantize
@for _f4u$t
@static
@param {Number} v The value to quantize.
@param {Number} m One side of the quantizing range (either the min or max).
@param {Number} n The other side of the quantizing range (either min or max).
@param {Number} s The step value to which the number must be quantized.
@return {Number} ov quantized (and bounded if necessary)
**/
_f4u$t.quantize = function(ov,m,n,s) {
var v = _f4u$t.bound(ov, m, n);
var mn = Math.min(m,n);
var mx = Math.max(m,n);
out = Math.floor((v - mn) / s 0.5) * s mn;
return _f4u$t.bound(out, m, n);
}
/**
Flips a value at the center of a range.
@method flip
@for _f4u$t
@static
@param {Number} v The value to flip.
@param {Number} m One side of the range (either the min or max).
@param {Number} n The other side of the range (either min or max).
@return {Number} v flipped over the axis at the middle of the given range.
**/
_f4u$t.flip = function(v,m,n) {
var mn = Math.min(m,n);
var mx = Math.max(m,n);
var offset = (mx - mn) / 2.0 mn;
return -1.0 * (v - offset) offset;
}
/**
Remaps a value via _f4u$t.remap and then bounds it in the new
range via _f4u$t.bound.
@method remap_and_bound
@for _f4u$t
@static
@param {Number} v The value to remap and bound.
@param {Number} mn0 The minimum of the old range.
@param {Number} mx0 The maximum of the old range.
@param {Number} mn1 The minimum and bound of the new range.
@param {Number} mx1 The maximum and bound of the new range.
@return {Number} The value v remapped and bounded to the new range.
**/
_f4u$t.remap_and_bound = function(v, mn0, mx0, mn1, mx1) {
return _f4u$t.bound(_f4u$t.remap(v, mn0, mx0, mn1, mx1), mn1, mx1);
}
/**
Remaps a value via _f4u$t.remap and then quantizes it in the new
range via _f4u$t.quantize.
@method remap_and_quantize
@for _f4u$t
@static
@param {Number} v The value to remap and quantize.
@param {Number} mn0 The minimum of the old range.
@param {Number} mx0 The maximum of the old range.
@param {Number} mn1 The minimum and bound of the new range.
@param {Number} mx1 The maximum and bound of the new range.
@param {Number} s The step value to which the number must be quantized.
@return {Number} The value v remapped and quantizeed to the new range.
**/
_f4u$t.remap_and_quantize = function(v, mn0, mx0, mn1, mx1, s) {
return _f4u$t.quantize(_f4u$t.remap(v, mn0, mx0, mn1, mx1), mn1, mx1, s);
}
/**
Remaps a value via _f4u$t.remap and then bounds it in the new
range via _f4u$t.bound and then flips it via _f4u$t.flip.
@method remap_and_bound_and_flip
@for _f4u$t
@static
@param {Number} v The value to remap and bound and flip.
@param {Number} mn0 The minimum of the old range.
@param {Number} mx0 The maximum of the old range.
@param {Number} mn1 The minimum and bound of the new range.
@param {Number} mx1 The maximum and bound of the new range.
@return {Number} The value v remapped and bounded and flipped to the new range.
**/
_f4u$t.remap_and_bound_and_flip = function(v, mn0, mx0, mn1, mx1) {
return _f4u$t.flip(_f4u$t.bound(_f4u$t.remap(v, mn0, mx0, mn1, mx1), mn1, mx1), mn1, mx1);
}
/**
Remaps a value via _f4u$t.remap and then quantizes it in the new
range via _f4u$t.quantize and then flips it via _f4u$t.flip.
@method remap_and_quantize_and_flip
@for _f4u$t
@static
@param {Number} v The value to remap and quantize and flip.
@param {Number} mn0 The minimum of the old range.
@param {Number} mx0 The maximum of the old range.
@param {Number} mn1 The minimum and bound of the new range.
@param {Number} mx1 The maximum and bound of the new range.
@return {Number} The value v remapped and quantized and flipped to the new range.
**/
_f4u$t.remap_and_quantize_and_flip = function(v, mn0, mx0, mn1, mx1, s) {
return _f4u$t.flip(_f4u$t.quantize(_f4u$t.remap(v, mn0, mx0, mn1, mx1), mn1, mx1, s), mn1, mx1);
}
/**
Remaps a value via _f4u$t.remap and then flips it via _f4u$t.flip.
@method remap_and_flip
@for _f4u$t
@static
@param {Number} v The value to remap and flip.
@param {Number} mn0 The minimum of the old range.
@param {Number} mx0 The maximum of the old range.
@param {Number} mn1 The minimum of the new range.
@param {Number} mx1 The maximum of the new range.
@return {Number} The value v remapped and flipped to the new range.
**/
_f4u$t.remap_and_flip = function(v, mn0, mx0, mn1, mx1) {
return _f4u$t.flip(_f4u$t.remap(v, mn0, mx0, mn1, mx1), mn1, mx1);
}
/**
Bounds a value via _f4u$t.bound and then flips it via _f4u$t.flip.
@method bound_and_flip
@for _f4u$t
@static
@param {Number} v The value to bound and flip.
@param {Number} mn0 The minimum of the old range.
@param {Number} mx0 The maximum of the old range.
@param {Number} mn1 The minimum and bound of the new range.
@param {Number} mx1 The maximum and bound of the new range.
@return {Number} The value v bounded and and flipped to the new range.
**/
_f4u$t.bound_and_flip = function(v, mn1, mx1) {
return _f4u$t.flip(_f4u$t.bound(v, mn1, mx1), mn1, mx1);
}
/**
Finds the sign of a number.
@method sign
@for _f4u$t
@static
@param {Number} x The number whose sign we want.
@return {Number} The sign, either -1, 1 or 0.
**/
_f4u$t.sign = function(x) {
return (x == 0 ? x : Math.floor(Math.abs(x) / x));
}
/**
Chooses left, right, or center of range.
@method linear_combinatin
@for _f4u$t
@static
@param {Number} dir The direction indicating which number to use (left/right/center). Note that up is the same as left and down is the same as right.
@param {Number} v1 The first value of the range.
@param {Number} v2 The second value of the range.
@return {Number} The first value if left, the second value if right, or the center if center.
**/
_f4u$t.linear_combination = function(dir, v1, v2) {
if (dir == _f4u$t.LEFT) {
return v1;
}
if (dir == _f4u$t.RIGHT) {
return v2;
}
return (v1 v2) / 2.0;
}
/**
Chooses one of two values depending on the axis.
@method xy
@for _f4u$t
@static
@param {Number} a The axis, _f4u$t.X_AXIS or _f4u$t.Y_AXIS.
@param {Number} x The value to return if a is equal to _f4u$t.X_AXIS.
@param {Number} y The value to return if a is equal to _f4u$t.X_AXIS.
@return {Number} x or y, depending on what a is.
**/
_f4u$t.xy = function(a,x,y) {
return (a == _f4u$t.X_AXIS ? x : y);
}
/**
Returns the trailing part of an id. The faust naming convention gives
all DOM ids a unique identifier at the end, called the trailing part, and
this function returns it.
@method unique
@for _f4u$t
@static
@param {String} s An ID string
@return {String} The unique trailing part, if one exists.
**/
_f4u$t.unique = function(s) {
var spl = s.toString().split("_");
if (spl.length == 0) {
return s;
}
return spl[spl.length - 1];
}
/**
Returns the type part of an id. The faust naming convention gives
all DOM ids a type as the second entry. Examples are vslider, hslider,
rbutton.
@method unique
@for _f4u$t
@static
@param {String} s An ID string
@return {String} The unique type, if one exists.
**/
_f4u$t.type = function(s) {
var spl = s.toString().split("_");
if (spl.length == 0) {
console.log("incorrect naming of faust object");
return s;
}
return spl[1];
}
/**
Takes an array of three values (R, G, and B) and returns a color
useable by CSS.
@method color_to_css
@for _f4u$t
@static
@param {Array, String} rgb An array with three values for
R, G, and B or a CSS string to bypass the conversion.
@return {String} An RGB value useable by CSS.
**/
_f4u$t.color_to_rgb = function(rgb) {
if (typeof rgb == 'string')
return rgb;
return (rgb ? "rgb(" rgb[0] "," rgb[1] "," rgb[2] ")" : 'none');
}
/**
A function that generates a random 7-letter string.
@method randString
@for _f4u$t
@static
@return {String} A random 7-letter string.
**/
_f4u$t.randString = function() {
var result = '';
var length = 7;
var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (var i = length; i > 0; --i) {
result = chars[Math.round(Math.random() * (chars.length - 1))];
}
return result;
}
/**
A function that returns either a given value or a default if
the given is null
@method initifnull
@for _f4u$t
@static
@param {any} v The value to use.
@param {any} d The default to return if v is null.
@return {any} Either v or d.
**/
_f4u$t.initifnull = function(v,d) {
return (v == null ? d : v);
}
// TRIGONOMETRIC FUNCTIONS
/**
In a range, find all angles that fall at a multiple of 90 degrees.
@method find_all_90s
@for _f4u$t
@static
@param {Number} a0 The initial angle in degrees.
@param {Number} sweep The angle to sweep from the initial in degrees.
@return {Array} All multiples of 90 between a0 and a0 sweep inclusive.
**/
_f4u$t.find_all_90s = function(a0, sweep) {
var total = 0;
var out = [];
while (a0 > total) {
total = 90;
}
while (total <= a0 sweep) {
out.push(total);
total = 90;
}
return out;
}
/**
Given a polar coordiante, find the cartesian point
@method point_from_polar
@for _f4u$t
@static
@param {Number} r Radius.
@param {Number} a Angle in radians.
@return {Array} The cartesian coordinate as [x,y].
**/
_f4u$t.point_from_polar = function(r, a) {
return [r * Math.cos(a), r * Math.sin(a)];
}
/**
Adds two coordinates in the form [x0,y0] and [x1,y1].
@method coord_sub
@for _f4u$t
@static
@param {Array} c0 First cartesian coordinate.
@param {Array} c1 Second cartesian coordinate.
@return {Array} The sum of the two coordinates.
**/
_f4u$t.coord_add = function(c0, c1) {
return [c0[0] c1[0], c0[1] c1[1]];
}
/**
Subtracts two coordinates in the form [x0,y0] and [x1,y1].
@method coord_sub
@for _f4u$t
@static
@param {Array} c0 First cartesian coordinate.
@param {Array} c1 Second cartesian coordinate, subtracted from first.
@return {Array} The difference of the two coordinates.
**/
_f4u$t.coord_sub = function(c0, c1) {
return [c0[0] - c1[0], c0[1] - c1[1]];
}
/**
Converts radians to degrees.
@method r2d
@for _f4u$t
@static
@param {Number} Angle in radians.
@return {Number} Angle in degrees.
**/
_f4u$t.r2d = function(a) {
return a * 180 / Math.PI;
}
/**
Converts degrees to radians.
@method r2d
@for _f4u$t
@static
@param {Number} Angle in degrees.
@return {Number} Angle in radians.
**/
_f4u$t.d2r = function(a) {
return a * Math.PI / 180.;
}
/**
Ajax queue.
@property ajax_queue
@for _f4u$t
@type Object
@default {}
**/
_f4u$t.ajax_queue = {};
/**
Is the ajax queue busy?
@property ajax_queue_busy
@for _f4u$t
@type Boolean
@default false
**/
_f4u$t.ajax_queue_busy = false;
/**
Active addresses being sent to server.
@property active_addresses
@for _f4u$t
@type Array
@default []
**/
_f4u$t.active_addresses = [];
/**
A box class.
@class Box
@namespace _f4u$t
@constructor
**/
_f4u$t.Box = function() {
/**
Resets the x and y of a box to their defaults
@method clear
@for _f4u$t.Box
**/
this.clear = function() {
/**
x range of a Box.
@property x
@for _f4u$t.Box
@type Array
@default [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]
**/
this.x = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
/**
y range of a Box.
@property y
@for _f4u$t.Box
@type Array
@default [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]
**/
this.y = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
}
this.clear();
}
/**
Adds an array of points, with each point in the form [x,y], to the
interior of the box.
@method add_points
@for _f4u$t.Box
@param {Array} pts An array of points.
**/
_f4u$t.Box.prototype.add_points = function(pts) {
for (var i = 0; i < pts.length; i ) {
this.add_point(pts[i]);
}
}
/**
Adds a point in the form [x,y] to the
interior of the box.
@method add_points
@for _f4u$t.Box
@param {Array} pt A point to add.
**/
_f4u$t.Box.prototype.add_point = function(pt) {
this.x = [Math.min(this.x[0], pt[0]), Math.max(this.x[1], pt[0])];
this.y = [Math.min(this.y[0], pt[1]), Math.max(this.y[1], pt[1])];
}
/**
Returns the length of the box along the x and y axes.
@method lens
@for _f4u$t.Box
@return {Array} The length of the box along the x and y axes.
**/
_f4u$t.Box.prototype.lens = function(pt) {
return [this.x[1] - this.x[0], this.y[1] - this.y[0]];
}
/**
Returns the corner points (minima and maxima) of the Box.
@method corners
@for _f4u$t.Box
@return {Array} The corner points of the box.
**/
_f4u$t.Box.prototype.corners = function(pt) {
return [[this.x[0], this.y[0]], [this.x[1], this.y[1]]];
}
/**
Finds the bounding box of a text node in an svg.
@method get_text_bbox
@for _f4u$t
@param {Object} svg The svg node.
@param {Object} text The text node.
@return {Object} The bounding rectangle of the text node.
**/
_f4u$t.get_text_bbox = function(svg, text, kls) {
if (!kls) {
kls = 'faust-label';
}
var dummy = _f4u$t.make_text(svg,null,0,0,text, {'class' : kls});
var bbox = dummy.getBBox();
_f4u$t.svg_remove(svg, dummy);
return bbox;
}
_f4u$t.make_rectangle_via_path = function(svg, parent, rf, x, y, w, h, ops) {
var d = "M{0} {7}L{1} {7}C{2} {7} {2} {7} {2} {3}L{2} {4}C{2} {5} {2} {5} {1} {5}L{0} {5}C{6} {5} {6} {5} {6} {4}L{6} {3}C{6} {7} {6} {7} {0} {7}";
d = d.format([rf x, w - rf x, w x, rf y, h - rf y, h y, x, y]);
var rect = _f4u$t.make_path(
parent,
d,
ops
);
return rect;
}
/**
Makes the UI for the faust application.
@method make_ui
@for _f4u$t
@param {Object} svg The root SVG node.
@param {Object} raw_json The raw JSON describing the UI to build.
**/
_f4u$t.make_ui = function(svg, raw_json, width, height, hash) {
var json = eval ("(" raw_json ")");
var faust_svg = new _f4u$t.SVG(
svg,
width,
height,
{
constrain : false,
title : json["ui"][0].label,
lm : _f4u$t.json_to_ui(json, hash)
}
);
faust_svg.defs();
faust_svg.lm.mom = faust_svg;
faust_svg.make();
}
_f4u$t.assign_parameters_to_values = function(URLParams) {
for (var index in URLParams) {
var split_index = index.toString().split('.');
if (split_index.length != 2) {
continue;
}
if (_f4u$t[split_index[0]]) {
if (_f4u$t[split_index[0]][split_index[1]]) {
_f4u$t[split_index[0]][split_index[1]] = eval(URLParams[index][URLParams[index].length - 1]);
}
}
}
}
/**
The main function called to build the faust UI.
Parses the URL to include any new documents and then builds the UI.
@method main
@for _f4u$t
@param {Object} raw_json A raw JSON object describing the UI to build.
@param {Object} div (optional) The div to place the object in.
**/
_f4u$t.main = function(raw_json, div, callback) {
if (!div) {
div = $( "<div />" );
$("body").append(div);
}
// we create a hash for the object
var hash = $(div).attr('id') ? $(div).attr('id') : _f4u$t.rand_string(8);
// keep it in the div
$(div).attr('id', hash);
// first, we parse URL parameters to change UIs' style if necessary
var URLParams = _f4u$t.parseURLParams(document.URL);
// then we assign parameters
_f4u$t.assign_parameters_to_values(URLParams);
// we make sure all JS and CSS is loaded before we build the UI
_f4u$t.load_css_and_then_js(URLParams.css, URLParams.js);
var width = $(div).width();
if (width == 0) {
// HUOM: this "- 15" is a kludge and should dealt with more elegantly
width = $(window).width() - 15;
}
var height = $(div).height();
if (height == 0) {
// HUOM: this "- 17" is a kludge and should dealt with more elegantly
height = $(window).height() - 17;
}
if (callback) {
_f4u$t.HANDLER_CALLBACKS.push(function(address, value) {
if (_f4u$t.first_part_matches(address, hash)) {
return callback(_f4u$t.remove_from_head_of_string(address, hash), value);
}
});
}
_f4u$t.add_svg_and_onload_to_div(div, raw_json, width, height, hash);
return function(address, value) {
return _f4u$t.update_value_at_address(hash address, value);
}
}
/**
Deletes all references to this div in internal data structures, including
all bindings. Note that this does NOT delete the div from the page, nor
does it delete the div's contents. This is in case the div contains
other information that the person wants to complete.
To delete the UI in the div, call _f4u$t.hard_delete()
**/
_f4u$t.delete = function(div) {
var id = $(div).attr('id');
if (!id) {
console.log("Can't delete because obj does not exist in the system.");
return;
}
var ids_to_delete = [];
var keys_to_delete = []
for (var key in _f4u$t.PATHS_TO_IDS) {
if (_f4u$t.first_part_matches(key, id)) {
ids_to_delete.push(_f4u$t.PATHS_TO_IDS[key]);
keys_to_delete.push(key);
}
}
for (var key_to_delete in keys_to_delete) {
delete _f4u$t.PATHS_TO_IDS[key_to_delete];
}
for (var id_to_delete in ids_to_delete) {
var slim = _f4u$t.unique(id_to_delete);
delete _f4u$t.IDS_TO_ATTRIBUTES[slim];
}
}
_f4u$t.hard_delete = function(div) {
_f4u$t.delete(div);
$(div).empty();
}
/**
To be called when used with the C /LLVM libfaust based FaustNode.
**/
_f4u$t.make_audio_ui = function(dsp, svg) {
var json = eval("(" dsp.json() ")");
var faust_svg = new _f4u$t.SVG(
svg,
// kludge to prevent scroll bars...
$(window).width() - 15,
// kludge to prevent scroll bars...
$(window).height() - 17,
{
constrain : false,
title : json["ui"][0].label,
lm : _f4u$t.json_to_ui(json)
}
);
// Keep audio params in a table
_f4u$t.controls = new Array();
for (var i = 0; i < dsp.numberOfAudioParams(); i ) {
var ctrl = dsp.getAudioParam(i);
_f4u$t.controls[ctrl.name] = ctrl;
}
_f4u$t.fausthandler = function(dest, value) {
_f4u$t.controls[dest].value = value;
}
_f4u$t.main_loop = function() {}
faust_svg.defs();
faust_svg.lm.mom = faust_svg;
faust_svg.make();
}
|