-
Notifications
You must be signed in to change notification settings - Fork 0
/
deserialize.js
97 lines (93 loc) · 1.87 KB
/
deserialize.js
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
const path = require('path');
const Max = require('max-api');
const id = "pad";
var leftPoint = null;
var rightPoint = null;
var isOn = 0;
var state = "none";
var input;
function sideTest(point) {
if (point["x"] < 0.5) {
return "left";
};
return "right";
};
function isNear(p1, p2) {
const distance = Math.sqrt((p1["x"] - p2["x"])**2 + (p1["y"] - p2["y"])**2);
if (distance < 0.2) {
return true;
};
return false;
};
Max.addHandler("singleMode", (msg) => {
input = JSON.parse(msg);
if (input.length === 0) {
isOn = 0;
input = new Object();
} else {
isOn = 1;
input = Object.assign({}, input);
};
Max.setDict(id, input);
Max.outlet(isOn);
})
Max.addHandler("doubleMode", (msg) => {
input = JSON.parse(msg);
// change state based on inputs and previous state
if (input.length === 0) {
state = "none";
} else if (input.length === 1) {
if (state === "none") {
state = sideTest(input[0])
} else if (state.includes("both")) {
if (isNear(input[0], leftPoint)) {
state = "left";
} else {
state = "right";
};
};
} else {
if (!state) {
if (sideTest(input[0]) === "left") {
state = "both";
} else {
state = "!both";
};
} else if (state === "left") {
state = "both";
} else if (state === "right") {
state = "!both";
};
};
// assign inputs based on state
switch (state) {
case "none":
isOn = 0;
leftPoint = null;
rightPoint = null;
break;
case "left":
isOn = 1;
leftPoint = input[0];
rightPoint = null;
break;
case "right":
isOn = 2;
leftPoint = null;
rightPoint = input[0];
break;
case "both":
isOn = 3;
leftPoint = input[0];
rightPoint = input[1];
break;
case "!both":
isOn = 3;
leftPoint = input[1];
rightPoint = input[0];
break;
}
// update dictionary and output On/Off boolean
Max.setDict(id, {"left":leftPoint, "right":rightPoint});
Max.outlet(isOn);
});