-
Notifications
You must be signed in to change notification settings - Fork 502
/
Copy pathmagBalls.js
311 lines (262 loc) · 9.18 KB
/
magBalls.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
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
//
// Created by Bradley Austin Davis on 2015/08/25
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// FIXME Script paths have to be relative to the caller, in this case libraries/OmniTool.js
Script.include("magBalls/constants.js");
Script.include("magBalls/graph.js");
Script.include("magBalls/edgeSpring.js");
Script.include("magBalls/magBalls.js");
Script.include("libraries/avatarRelativeOverlays.js");
OmniToolModuleType = "MagBallsController"
getMagBallsData = function(id) {
return getEntityCustomData(MAG_BALLS_DATA_NAME, id, {});
}
setMagBallsData = function(id, value) {
setEntityCustomData(MAG_BALLS_DATA_NAME, id, value);
}
var UI_BALL_RADIUS = 0.01;
var MODE_INFO = { };
MODE_INFO[BALL_EDIT_MODE_ADD] = {
uiPosition: {
x: 0.15,
y: -0.08,
z: -0.35,
},
colors: [ COLORS.GREEN, COLORS.BLUE ],
// FIXME use an http path or find a way to get the relative path to the file
url: Script.resolvePath('html/magBalls/addMode.html'),
};
MODE_INFO[BALL_EDIT_MODE_DELETE] = {
uiPosition: {
x: 0.20,
y: -0.08,
z: -0.32,
},
colors: [ COLORS.RED, COLORS.BLUE ],
// FIXME use an http path or find a way to get the relative path to the file
url: Script.resolvePath('html/magBalls/deleteMode.html'),
};
var UI_POSITION_MODE_LABEL = Vec3.multiply(0.5,
Vec3.sum(MODE_INFO[BALL_EDIT_MODE_ADD].uiPosition,
MODE_INFO[BALL_EDIT_MODE_DELETE].uiPosition));
UI_POSITION_MODE_LABEL.y = -0.02;
var UI_BALL_PROTOTYPE = {
size: UI_BALL_RADIUS * 2.0,
alpha: 1.0,
solid: true,
visible: true,
}
OmniToolModules.MagBallsController = function(omniTool, entityId) {
this.omniTool = omniTool;
this.entityId = entityId;
// In hold mode, holding a ball requires that you keep the action
// button pressed, while if this is false, clicking on a ball selects
// it and clicking again will drop it.
this.holdMode = true;
this.highlighter = new Highlighter();
this.magBalls = new MagBalls();
this.highlighter.setSize(BALL_SIZE);
this.ghostEdges = {};
this.selectionRadiusMultipler = 1.5;
this.uiOverlays = new AvatarRelativeOverlays();
// create the overlay relative to the avatar
this.uiOverlays.addOverlay("sphere", mergeObjects(UI_BALL_PROTOTYPE, {
color: MODE_INFO[BALL_EDIT_MODE_ADD].colors[0],
position: MODE_INFO[BALL_EDIT_MODE_ADD].uiPosition,
}));
this.uiOverlays.addOverlay("sphere", mergeObjects(UI_BALL_PROTOTYPE, {
color: MODE_INFO[BALL_EDIT_MODE_DELETE].colors[0],
position: MODE_INFO[BALL_EDIT_MODE_DELETE].uiPosition,
}));
// FIXME find the proper URLs to use
this.modeLabel = this.uiOverlays.addOverlay("web3d", {
isFacingAvatar: true,
alpha: 1.0,
dimensions: { x: 0.16, y: 0.12, z: 0.001},
color: "White",
position: UI_POSITION_MODE_LABEL,
});
this.setMode(BALL_EDIT_MODE_ADD);
// DEBUGGING ONLY - Fix old, bad edge bounding boxes
//for (var edgeId in this.magBalls.edges) {
// Entities.editEntity(edgeId, {
// dimensions: LINE_DIMENSIONS,
// });
//}
// DEBUGGING ONLY - Clear any previous balls
// this.magBalls.clear();
// DEBUGGING ONLY - Attempt to fix connections between balls
// and delete bad connections. Warning... if you haven't looked around
// and caused the domain server to send you all the nearby balls as well as the connections,
// this can break your structures
// this.magBalls.repair();
}
OmniToolModules.MagBallsController.prototype.onUnload = function() {
this.clearGhostEdges();
this.uiOverlays.deleteAll();
}
OmniToolModules.MagBallsController.prototype.setMode = function(mode) {
if (mode === this.mode) {
return;
}
logDebug("Changing mode to '" + mode + "'");
Overlays.editOverlay(this.modeLabel, {
url: MODE_INFO[mode].url
});
this.mode = mode;
var color1;
var color2;
switch (this.mode) {
case BALL_EDIT_MODE_ADD:
color1 = COLORS.BLUE;
color2 = COLORS.GREEN;
break;
case BALL_EDIT_MODE_MOVE:
color1 = COLORS.GREEN;
color2 = COLORS.LIGHT_GREEN;
break;
case BALL_EDIT_MODE_DELETE:
color1 = COLORS.RED;
color2 = COLORS.BLUE;
break;
case BALL_EDIT_MODE_DELETE_SHAPE:
color1 = COLORS.RED;
color2 = COLORS.YELLOW;
break;
}
this.omniTool.model.setTipColors(color1, color2);
}
OmniToolModules.MagBallsController.prototype.findUiBallHit = function() {
var result = null;
for (var mode in MODE_INFO) {
var modeInfo = MODE_INFO[mode];
var spherePoint = getEyeRelativePosition(modeInfo.uiPosition);
if (findSpherePointHit(spherePoint, UI_BALL_RADIUS * 2, this.tipPosition)) {
this.highlighter.highlight(spherePoint);
this.highlighter.setColor("White");
// FIXME why doesn't this work?
this.highlighter.setSize(UI_BALL_RADIUS * 4);
return mode;
}
}
return;
}
OmniToolModules.MagBallsController.prototype.onUpdateSelected = function(deltaTime) {
if (!this.selected) {
return;
}
Entities.editEntity(this.selected, { position: this.tipPosition });
var targetBalls = this.magBalls.findPotentialEdges(this.selected);
for (var ballId in targetBalls) {
var targetPosition = this.magBalls.getNodePosition(ballId);
var distance = Vec3.distance(targetPosition, this.tipPosition);
var variance = this.magBalls.getVariance(distance);
var mix = Math.abs(variance) / this.magBalls.MAX_VARIANCE;
var color = colorMix(COLORS.YELLOW, COLORS.RED, mix);
if (!this.ghostEdges[ballId]) {
// create the ovleray
this.ghostEdges[ballId] = Overlays.addOverlay("line3d", {
start: this.magBalls.getNodePosition(ballId),
end: this.tipPosition,
color: color,
alpha: 1,
lineWidth: 5,
visible: true,
});
} else {
Overlays.editOverlay(this.ghostEdges[ballId], {
end: this.tipPosition,
color: color,
});
}
}
for (var ballId in this.ghostEdges) {
if (!targetBalls[ballId]) {
Overlays.deleteOverlay(this.ghostEdges[ballId]);
delete this.ghostEdges[ballId];
}
}
}
OmniToolModules.MagBallsController.prototype.onUpdate = function(deltaTime) {
this.tipPosition = this.omniTool.getPosition();
this.uiOverlays.onUpdate(deltaTime);
this.onUpdateSelected();
if (this.findUiBallHit()) {
return;
}
if (!this.selected) {
// Find the highlight target and set it.
var target = this.magBalls.findNearestNode(this.tipPosition, BALL_RADIUS * this.selectionRadiusMultipler);
this.highlighter.highlight(target);
this.highlighter.setColor(MODE_INFO[this.mode].colors[0]);
if (!target) {
this.magBalls.onUpdate(deltaTime);
}
return;
}
}
OmniToolModules.MagBallsController.prototype.deselect = function() {
if (!this.selected) {
return false
}
this.clearGhostEdges();
this.magBalls.releaseBall(this.selected);
this.selected = null;
return true;
}
OmniToolModules.MagBallsController.prototype.onClick = function() {
var newMode = this.findUiBallHit();
if (newMode) {
if (this.selected) {
this.magBalls.destroyNode(highlighted);
this.selected = null;
}
this.setMode(newMode);
return;
}
if (this.deselect()) {
return;
}
logDebug("MagBallsController onClick: " + vec3toStr(this.tipPosition));
// TODO add checking against UI shapes for adding or deleting balls.
var highlighted = this.highlighter.highlighted;
if (this.mode == BALL_EDIT_MODE_ADD && !highlighted) {
highlighted = this.magBalls.createBall(this.tipPosition);
}
// Nothing to select or create means we're done here.
if (!highlighted) {
return;
}
switch (this.mode) {
case BALL_EDIT_MODE_ADD:
case BALL_EDIT_MODE_MOVE:
this.magBalls.selectBall(highlighted);
this.selected = highlighted;
logDebug("Selected " + this.selected);
break;
case BALL_EDIT_MODE_DELETE:
this.magBalls.destroyNode(highlighted);
break;
case BALL_EDIT_MODE_DELETE_SHAPE:
logDebug("Not implemented yet");
break;
}
if (this.selected) {
this.highlighter.highlight(null);
}
}
OmniToolModules.MagBallsController.prototype.onRelease = function() {
if (this.holdMode) {
this.deselect();
}
}
OmniToolModules.MagBallsController.prototype.clearGhostEdges = function() {
for(var ballId in this.ghostEdges) {
Overlays.deleteOverlay(this.ghostEdges[ballId]);
delete this.ghostEdges[ballId];
}
}