-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectTree.js
210 lines (191 loc) · 8.97 KB
/
objectTree.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
var handleChildParentDragstart = function(e, draggedElement){
e.dataTransfer.effectAllowed = "link";
e.dataTransfer.setData('text/html', draggedElement.id);
e.stopPropagation();
if(draggedElement.parentNode.id!="objects_in_scene")
document.getElementById("parent_actions").style.display = "block";
};
var handleDragover = function(e){
e.preventDefault();
e.dataTransfer.dropEffect = 'link';
};
var handleChildParentDrop = function(id, newParent){
if(id==newParent.id){
showNotification("Cannot make the active object the active object's parent", "fa-exclamation-circle", "info");
// same element
}else if(newParent.parentNode.id!="objects_in_scene" &&
Array.prototype.indexOf.call(newParent.parentNode.childNodes, newParent)==0 &&
(hasChildNodeWithId(newParent.parentNode, id))){
showNotification("The object is already a child", "fa-exclamation-circle", "info");
// TODO: implement removing parent
// already a child
}else{
var newChild = document.getElementById(id);
if(newChild.className == "parent_wrapper"){
objects[Number(newChild.firstChild.id)].parent = objects[Number(newParent.id)];
cameraPreview.objects[Number(newChild.firstChild.id)].parent = cameraPreview.objects[Number(newParent.id)];
}else{
objects[Number(newChild.id)].parent = objects[Number(newParent.id)];
cameraPreview.objects[Number(newChild.id)].parent = cameraPreview.objects[Number(newParent.id)];
}
if(newParent.parentNode.id == "wrapper_of_"+newParent.id){
newParent.parentNode.appendChild(newChild);
newChild.style.marginLeft = "10px";
}else {
var wrapper = document.createElement("div");
var newParentCopy = newParent.cloneNode(true); // need copy to replace the original with the wrapper + copy later
newParentCopy.style.marginLeft = "0px";
newParentCopy.addEventListener("dragover", function (e) {
handleDragover(e)
});
newParentCopy.addEventListener("drop", function (e) {
handleChildParentDrop(e.dataTransfer.getData("text/html"), this)
});
newParentCopy.addEventListener("click", handleClickOnObject);
wrapper.appendChild(newParentCopy);
wrapper.appendChild(newChild);
wrapper.style.marginLeft = newParent.style.marginLeft;
wrapper.id = "wrapper_of_" + newParentCopy.dataset.id;
wrapper.className = "parent_wrapper";
wrapper.addEventListener("dragstart", function (e) { // enables moving parent with children
handleChildParentDragstart(e, this);
});
wrapper.addEventListener("dragend", function (e) { // disables unlink buttons
handleChildParentDragend();
});
newParent.parentNode.replaceChild(wrapper, newParent);
newChild.style.marginLeft = "10px";
newChild.addEventListener("click", handleClickOnObject);
document.getElementById("delete_"+newParentCopy.id).addEventListener("click", function (e) {
e.stopPropagation();
if(newParentCopy.parentNode.firstChild == newParentCopy){
deleteParent(newParentCopy);
}else{
deleteObject(newParentCopy);
}
});
uiFactory.updateParentField();
}
}
};
var handleChildParentDragend = function(){
document.getElementById("parent_actions").style.display = "none";
};
document.getElementById("parent_actions_unlink").addEventListener("dragover", function(e){
handleDragover(e);
});
document.getElementById("parent_actions_unlink").addEventListener("drop", function(e){
var element = document.getElementById(e.dataTransfer.getData("text/html")); // element that is being dropped
var parent = element.parentNode; // the element's div wrapper / #objects_in_scene
var currentScene = document.getElementById("objects_in_scene"); // #objects_in_scene
currentScene.appendChild(element);
cleanUpObjectNode(parent);
element.style.marginLeft = "0px";
objects[Number(element.id)].parent = null;
cameraPreview.objects[Number(element.id)].parent = null;
});
var cleanUpObjectNode = function(wrapper){ // removes unnecessary wrappers
if(wrapper.childNodes.length==1 && wrapper.id!="objects_in_scene"){ // if only first (former parent) element is left, remove the div wrapper
var copy = wrapper.firstChild.cloneNode(true);
wrapper.parentNode.replaceChild(copy, wrapper);
copy.style.marginLeft = wrapper.style.marginLeft;
copy.addEventListener("dragstart", function(e){
handleChildParentDragstart(e, this);
});
copy.addEventListener("dragover", function(e){
handleDragover(e);
});
copy.addEventListener("drop", function(e){
handleChildParentDrop(e.dataTransfer.getData("text/html"), this);
});
copy.addEventListener("click", handleClickOnObject);
document.getElementById("delete_"+copy.id).addEventListener("click", function (e) {
e.stopPropagation();
deleteObject(copy);
});
}
};
document.getElementById("parent_actions_cancel").addEventListener("dragover", function(e){
handleDragover(e);
});
document.getElementById("parent_actions_cancel").addEventListener("drop", function(e){}); // do nothing on cancel
var appendObjectInSceneChildNode = function(type){
var child = document.createElement('li');
if(type=="camera") {
child.innerHTML = cameraPreview.modifier.ayceUI.screenName;
// TODO: camera deletion
//child.dataset.id = type;
//child.id = type;
}else if(type=="skybox"){
child.innerHTML = "skybox</div>";/* +
"<a class='delete_object_from_scene' id='delete_"+(objects.length-1)+"' data-id='"+(objects.length-1)+"'>×</a>";*/ // TODO: skybox deletion
child.dataset.id = (objects.length-1); //TODO: eliminate data-id
child.id = objects.length-1;
}else{
child.innerHTML = objects[objects.length-1].ayceUI.screenName+"</div>" +
"<a class='delete_object_from_scene' id='delete_"+(objects.length-1)+"' data-id='"+(objects.length-1)+"'>×</a>";
child.dataset.id = (objects.length-1); //TODO: eliminate data-id
child.id = objects.length-1;
}
child.dataset.type = type;
child.className = "object_in_scene button_dark";
child.addEventListener("click", handleClickOnObject);
child.draggable = true;
child.addEventListener("dragstart", function(e){
handleChildParentDragstart(e, this);
});
child.addEventListener("dragover", function(e){
handleDragover(e);
});
child.addEventListener("drop", function(e){
handleChildParentDrop(e.dataTransfer.getData("text/html"), this);
});
child.addEventListener("dragend", function(){
handleChildParentDragend();
});
document.getElementById("objects_in_scene_div").style.display = "block";
document.getElementById("objects_in_scene").appendChild(child);
if(type!=="camera" && type!=="skybox") {
document.getElementById("delete_" + child.id).addEventListener("click", function (e) {
e.stopPropagation();
deleteObject(child);
});
}
if(type!=="camera" && type!=="skybox"){
if(objects[objects.length-1].parent){
handleChildParentDrop(
objects[objects.length-1].ayceUI.id,
document.getElementById(objects[objects.length-1].parent.ayceUI.id));
}
}
return child;
};
var handleDeletedObject = function(child){
var parent = child.parentNode;
parent.removeChild(child);
if(parent.id!="objects_in_scene" && parent.childNodes.length > 0){
parent.id = "wrapper_of_" + parent.firstChild.id;
parent.className = "parent_wrapper";
}
cleanUpObjectNode(parent);
hideProperties();
};
var deleteObject = function(child){
if(!(objects[child.id] instanceof EmptyObject)) {
scene.removeFromScene(objects[child.id]);
cameraPreview.scene.removeFromScene(cameraPreview.objects[child.id]);
}
objects[child.id] = null;
cameraPreview.objects[child.id] = null;
handleDeletedObject(child);
};
var deleteParent = function(child){
for(var i = child.parentNode.childNodes.length-1; i > 0; i--){ // delete all child nodex except for the first (parent object)
if(child.parentNode.childNodes[i].className == "parent_wrapper"){
deleteParent(child.parentNode.childNodes[i].firstChild);
}else{
deleteObject(child.parentNode.childNodes[i]);
}
}
deleteObject(document.getElementById(child.parentNode.firstChild.id)); // delete first node separately because it doesn't have a wrapper anymore
};