-
Notifications
You must be signed in to change notification settings - Fork 1
/
sceneSvc.js
276 lines (222 loc) · 8.41 KB
/
sceneSvc.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
/**
* Created by John
*/
app.factory("sceneSvc", function ($rootScope, constants, gameSvc, scarlettSvc, $q) {
var svc = {};
var scope = $rootScope.$new();
AngularHelper.sceneSvc = svc;
svc._activeGameScenePath = null;
svc._activeGameScene = null; // the active game scene, all operations on the scene should be made with this consideration
svc._selectedObjects = [];
scope.$on(constants.EVENTS.PROJECT_LOADED, (function (e, project) {
svc._activeGameScene = null;
svc._activeGameScenePath = null;
}).bind(this));
scope.$on(constants.EVENTS.GAME_INITIALIZE, (function (e, project) {
/*if (project.editor && project.editor.lastScene) {
svc.loadSceneFromFile(scarlettSvc.getActiveProjectPath() + project.editor.lastScene);
}*/
}).bind(this));
scope.$on(constants.EVENTS.GAME_SCENE_CHANGED, (function (e, scene) {
svc._selectedObjects = [];
}).bind(this));
scope.$on(constants.EVENTS.GAME_OBJECT_REMOVED, (function (e, gameObject) {
// remove from the selected objects if the case:
let idx = svc._selectedObjects.indexOfObject(gameObject);
if (idx >= 0) {
// remove from the selected objects:
svc._selectedObjects.splice(idx, 1);
// broadcast the event so other components know
EventManager.emit(constants.EVENTS.GAME_OBJECT_SELECTION_CHANGED, svc._selectedObjects);
}
}).bind(this));
/**
* sets the active game scene
* @param scene
*/
svc.setActiveGameScene = function (scene) {
if (!isGameScene(scene)) {
return;
}
if (svc._activeGameScene && scene.getUID() === svc._activeGameScene.getUID()) {
// it's the same one, no need for further processing..
return;
}
svc._activeGameScene = scene;
// update the game active scene:
scene.getGame().changeScene(scene);
// update the project editor last scene:
if (svc._activeGameScenePath) {
scarlettSvc.getActiveProjectWorkspace().lastScene = Path.makeRelative(scarlettSvc.activeProjectPath, svc._activeGameScenePath);
}
// clear undo/redo history:
AngularHelper.commandHistory.clear();
// broadcast this event
$rootScope.$broadcast(constants.EVENTS.GAME_SCENE_CHANGED, scene);
};
/**
* save the active game scene
*/
svc.saveActiveScene = function () {
if (!isGameScene(svc._activeGameScene)) {
return;
}
// first we get the scene data
let sceneData = svc._activeGameScene.objectify();
// the scene already exists on disc?
if (svc._activeGameScenePath) {
// write the scene data:
NativeInterface.writeFile(svc._activeGameScenePath, JSON.stringify(sceneData));
} else {
// it doesn't, let's query the user:
let params = {
filters: [{name: '', extensions: ['ss']}]
};
NativeInterface.saveFileDialog(Path.wrapDirectoryPath(scarlettSvc.activeProjectPath), params, function (path) {
if (path) {
if (Path.relativeTo(path, scarlettSvc.activeProjectPath)) {
// also, update the active scene path variable:
svc._activeGameScenePath = path;
// .. and update the editor values as well:
scarlettSvc.getActiveProjectWorkspace().lastScene = Path.makeRelative(scarlettSvc.activeProjectPath, svc._activeGameScenePath);
// write the scene data:
NativeInterface.writeFile(path, JSON.stringify(sceneData));
}
}
});
}
};
/**
* loads a scene from a given path
* @param path
*/
svc.loadSceneFromFile = function (path) {
let defer = $q.defer();
NativeInterface.readFile(path, function (result) {
if (result === false) {
// the file failed to load..
defer.reject(error);
} else {
try {
// TODO: make this an editor game scene
var gameScene = EditorGameScene.restore(JSON.parse(result));
svc._activeGameScenePath = path;
svc.setActiveGameScene(gameScene);
defer.resolve(gameScene);
} catch (error) {
// the project failed while parsing..
defer.reject(error);
}
}
});
return defer.promise;
};
/**
* gets the active game scene
* @returns {*|null}
*/
svc.getActiveGameScene = function () {
return this._activeGameScene;
};
/**
* Add the given object to the selected scene
* @param gameObject
*/
svc.addGameObjectToScene = function (gameObject) {
if (svc._activeGameScene === null) {
// cannot add without a active scene..
return;
}
let parent = null;
// is there any object selected? if so, we will add it as a child (if it's only one)
if (svc._selectedObjects.length === 1) {
parent = svc._selectedObjects[0];
parent.addChild(gameObject);
} else {
// since there is no parent, we add it directly to the scene container:
svc._activeGameScene.addGameObject(gameObject);
}
// finally, broadcast this so other components know the game object was added to the scene:
$rootScope.$broadcast(constants.EVENTS.GAME_OBJECT_ADDED, gameObject, parent);
};
/**
* removes all the given game objects from the scene
* @param gameObjects
*/
svc.removeGameObjectsFromScene = function (gameObjects) {
function recursive(array, toDelete) {
for (let i = array.length - 1; i >= 0; i--) {
let idx = toDelete.indexOfObject(array[i]);
if (idx >= 0) {
// announce the removal:
$rootScope.$broadcast(constants.EVENTS.GAME_OBJECT_REMOVED, array[i]);
// remove the game object from the current array:
array.splice(i, 1);
// remove from the selection:
toDelete.splice(idx, 1);
// any more to look for?
if (toDelete.length == 0) {
return true;
}
} else {
// let's check on the child nodes though:
var end = recursive(array[i].getChildren(), toDelete);
if (end) {
return end;
}
}
}
}
recursive(svc._activeGameScene.getGameObjects(), gameObjects);
};
/**
* gets the selected game objects
*/
svc.getSelectedObjects = function () {
return svc._selectedObjects;
};
/**
* gets an array of the selected game objects uid values
*/
svc.getSelectedObjectsUIDs = function () {
if (!svc._selectedObjects || svc._selectedObjects.length == 0) {
return [];
}
let arr = [];
svc._selectedObjects.forEach(function (obj) {
arr.push(obj.getUID());
});
return arr;
};
/**
* set the selected game objects
* @param objects
*/
svc.setSelectedObjects = function (objects, disableBroadcast, disableCommandStore) {
// store the command ?
if (!disableCommandStore) {
AngularHelper.commandHistory.execute(new GameObjectSelectionCommand(this, svc._selectedObjects.slice(), objects.slice()));
} else {
// update the selected objects object:
svc._selectedObjects = objects;
}
// broadcast the event so other components know
if (!disableBroadcast) {
EventManager.emit(constants.EVENTS.GAME_OBJECT_SELECTION_CHANGED, objects);
}
};
/**
* checks if a given object is already selected
* @param object
*/
svc.isObjectSelected = function (object) {
return svc._selectedObjects.indexOf(object) >= 0;
};
/**
* clear the selected objects array
*/
svc.clearSelectedObjects = function () {
svc._selectedObjects = [];
};
return svc;
});