This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathResourcesDescriptor.js
120 lines (94 loc) · 3.13 KB
/
ResourcesDescriptor.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
import * as THREE from 'three';
import invariant from 'fbjs/lib/invariant';
import THREEElementDescriptor from '../THREEElementDescriptor';
import ResourceContainer from '../../Resources/ResourceContainer';
class ResourcesDescriptor extends THREEElementDescriptor {
construct() {
return new ResourceContainer();
}
unmount(threeObject) {
const parentMarkup = threeObject.userData.markup.parentMarkup;
const parentEvents = parentMarkup.threeObject.userData.events;
threeObject.resourceIds.forEach((id) => {
parentEvents.emit('resource.removed', {
id,
distance: 0,
resource: threeObject.resourceMap[id],
});
});
super.unmount(threeObject);
}
addChildren(threeObject, children) {
children.forEach((child) => {
const resourceId = child.userData._resourceId;
if (process.env.NODE_ENV !== 'production') {
invariant(!!resourceId, 'Resource container can only hold resources. ' +
'Found children without `resourceId` properties:' +
` ${children.filter(currentChild => !currentChild.userData._resourceId)
.map(currentChild =>
`<${currentChild.userData.react3internalComponent._elementType}/>`)
.join(', ')}.`);
} else {
invariant(!!resourceId);
}
threeObject.resourceIds.push(resourceId);
threeObject.resourceMap[resourceId] = child;
const parentMarkup = threeObject.userData.markup.parentMarkup;
if (parentMarkup) {
parentMarkup.threeObject.userData.events.emit('resource.added', {
id: resourceId,
distance: 0,
resource: child,
});
}
});
}
addChild(threeObject, child) {
this.addChildren(threeObject, [child]);
}
removeChild(threeObject, child) {
const resourceId = child.userData._resourceId;
delete threeObject.resourceIds[resourceId];
const parentMarkup = threeObject.userData.markup.parentMarkup;
if (parentMarkup) {
parentMarkup.threeObject.userData.events.emit('resource.removed', {
id: resourceId,
distance: 0,
resource: child,
});
}
}
setParent(threeObject, parentObject) {
super.setParent(threeObject, parentObject);
const parentEvents = parentObject.userData.events;
parentObject.userData._resources = threeObject;
threeObject.resourceIds.forEach((id) => {
parentEvents.emit('resource.added', {
id,
distance: 0,
resource: threeObject.resourceMap[id],
});
});
}
highlight(threeObject) {
const ownerObject = threeObject.userData.markup.parentMarkup.threeObject;
if (!(ownerObject.updateMatrixWorld && ownerObject.traverse)) {
return;
}
threeObject.userData.events.emit('highlight', {
uuid: threeObject.uuid,
boundingBoxFunc: () => {
const boundingBox = new THREE.Box3();
boundingBox.setFromObject(ownerObject);
return [boundingBox];
},
});
}
hideHighlight(threeObject) {
threeObject.userData.events.emit('hideHighlight');
}
moveChild() {
// child order doesn't matter
}
}
module.exports = ResourcesDescriptor;