Editor.js 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var Editor = function () {

	var SIGNALS = signals;

	this.signals = {

		// actions

		playAnimations: new SIGNALS.Signal(),

		// notifications

		transformModeChanged: new SIGNALS.Signal(),
		snapChanged: new SIGNALS.Signal(),
		rendererChanged: new SIGNALS.Signal(),
M
Mr.doob 已提交
16

17 18
		sceneGraphChanged: new SIGNALS.Signal(),

M
Mr.doob 已提交
19
		objectSelected: new SIGNALS.Signal(),
20 21
		objectAdded: new SIGNALS.Signal(),
		objectChanged: new SIGNALS.Signal(),
M
Mr.doob 已提交
22
		objectRemoved: new SIGNALS.Signal(),
M
Mr.doob 已提交
23 24

		helperAdded: new SIGNALS.Signal(),
M
Mr.doob 已提交
25
		helperRemoved: new SIGNALS.Signal(),
M
Mr.doob 已提交
26

27 28 29 30 31 32 33 34 35 36
		materialChanged: new SIGNALS.Signal(),
		clearColorChanged: new SIGNALS.Signal(),
		fogTypeChanged: new SIGNALS.Signal(),
		fogColorChanged: new SIGNALS.Signal(),
		fogParametersChanged: new SIGNALS.Signal(),
		windowResize: new SIGNALS.Signal()

	};

	this.scene = new THREE.Scene();
37
	this.sceneHelpers = new THREE.Scene();
38 39 40 41 42 43

	this.object = {};
	this.geometries = {};
	this.materials = {};
	this.textures = {};

44 45 46
	this.selected = null;
	this.helpers = {};

47 48 49 50 51 52
};

Editor.prototype = {

	setScene: function ( scene ) {

53 54 55 56 57 58 59 60 61
		this.scene.name = scene.name;
		this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );

		while ( scene.children.length > 0 ) {

			this.addObject( scene.children[ 0 ] );

		}

62 63 64 65
	},

	//

66 67
	addObject: function ( object ) {

68 69 70 71 72 73 74 75
		var scope = this;

		object.traverse( function ( child ) {

			scope.addHelper( child );

		} );

76 77 78
		this.scene.add( object );

		this.signals.objectAdded.dispatch( object );
79
		this.signals.sceneGraphChanged.dispatch();
80 81 82 83 84

	},

	removeObject: function ( object ) {

M
Mr.doob 已提交
85
		if ( object.parent === undefined ) return; // avoid deleting the camera or scene
86

M
Mr.doob 已提交
87
		if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
88

89 90 91 92 93 94 95 96
		var scope = this;

		object.traverse( function ( child ) {

			scope.removeHelper( child );

		} );

97
		object.parent.remove( object );
M
Mr.doob 已提交
98 99

		this.signals.objectRemoved.dispatch( object );
100
		this.signals.sceneGraphChanged.dispatch();
101 102 103 104 105 106 107

	},

	addGeometry: function ( geometry  ) {

	},

108 109 110 111
	removeGeometry: function ( geometry  ) {

	},

112 113 114 115
	addMaterial: function ( material ) {

	},

116 117 118 119
	removeMaterial: function ( material ) {

	},

120 121 122 123
	addTexture: function ( texture ) {

	},

124 125 126 127
	removeTexture: function ( texture ) {

	},

128 129 130 131
	//

	addHelper: function ( object ) {

132 133
		if ( object instanceof THREE.PointLight ) {

M
Mr.doob 已提交
134
			var helper = new THREE.PointLightHelper( object, 10 );
135
			helper.userData.object = object;
M
Mr.doob 已提交
136 137
			this.sceneHelpers.add( helper );
			this.helpers[ object.id ] = helper;
M
Mr.doob 已提交
138

M
Mr.doob 已提交
139
			this.signals.helperAdded.dispatch( helper );
140 141 142

		} else if ( object instanceof THREE.DirectionalLight ) {

M
Mr.doob 已提交
143
			var helper = new THREE.DirectionalLightHelper( object, 10 );
144
			helper.userData.object = object;
M
Mr.doob 已提交
145 146
			this.sceneHelpers.add( helper );
			this.helpers[ object.id ] = helper;
M
Mr.doob 已提交
147

M
Mr.doob 已提交
148
			this.signals.helperAdded.dispatch( helper );
149 150 151

		} else if ( object instanceof THREE.SpotLight ) {

M
Mr.doob 已提交
152
			var helper = new THREE.SpotLightHelper( object, 10 );
153
			helper.userData.object = object;
M
Mr.doob 已提交
154 155
			this.sceneHelpers.add( helper );
			this.helpers[ object.id ] = helper;
M
Mr.doob 已提交
156

M
Mr.doob 已提交
157
			this.signals.helperAdded.dispatch( helper );
158 159 160

		} else if ( object instanceof THREE.HemisphereLight ) {

M
Mr.doob 已提交
161
			var helper = new THREE.HemisphereLightHelper( object, 10 );
162
			helper.userData.object = object;
M
Mr.doob 已提交
163 164
			this.sceneHelpers.add( helper );
			this.helpers[ object.id ] = helper;
M
Mr.doob 已提交
165

M
Mr.doob 已提交
166
			this.signals.helperAdded.dispatch( helper );
167

M
Mr.doob 已提交
168
		}
169

170 171 172 173
	},

	removeHelper: function ( object ) {

174 175
		if ( this.helpers[ object.id ] !== undefined ) {

176 177 178
			var helper = this.helpers[ object.id ];
			helper.parent.remove( helper );

179 180
			delete this.helpers[ object.id ];

M
Mr.doob 已提交
181 182
			this.signals.helperRemoved.dispatch( helper );

183 184
		}

185 186 187 188
	},

	//

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	parent: function ( object, parent ) {

		if ( parent === undefined ) {

			parent = this.scene;

		}

		parent.add( object );

		this.signals.sceneGraphChanged.dispatch();

	},

	//

205 206
	select: function ( object ) {

207 208 209
		this.selected = object;
		this.signals.objectSelected.dispatch( object );

210 211
	},

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	selectById: function ( id ) {

		var scope = this;

		this.scene.traverse( function ( node ) {

			if ( node.id === id ) {

				scope.select( node );

			}

		} );

	},

	deselect: function () {

M
Mr.doob 已提交
230
		this.select( null );
231 232 233 234

	}

}