Editor.js 3.9 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 68 69 70 71
	addObject: function ( object ) {

		this.scene.add( object );
		this.addHelper( object );

		this.signals.objectAdded.dispatch( object );
72
		this.signals.sceneGraphChanged.dispatch();
73 74 75 76 77

	},

	removeObject: function ( object ) {

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

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

82
		object.parent.remove( object );
83
		this.removeHelper( object );
M
Mr.doob 已提交
84 85

		this.signals.objectRemoved.dispatch( object );
86
		this.signals.sceneGraphChanged.dispatch();
87 88 89 90 91 92 93

	},

	addGeometry: function ( geometry  ) {

	},

94 95 96 97
	removeGeometry: function ( geometry  ) {

	},

98 99 100 101
	addMaterial: function ( material ) {

	},

102 103 104 105
	removeMaterial: function ( material ) {

	},

106 107 108 109
	addTexture: function ( texture ) {

	},

110 111 112 113
	removeTexture: function ( texture ) {

	},

114 115 116 117
	//

	addHelper: function ( object ) {

118 119
		if ( object instanceof THREE.PointLight ) {

M
Mr.doob 已提交
120 121 122 123
			var helper = new THREE.PointLightHelper( object, 10 );
			helper.lightSphere.id = object.id;
			this.sceneHelpers.add( helper );
			this.helpers[ object.id ] = helper;
M
Mr.doob 已提交
124

M
Mr.doob 已提交
125
			this.signals.helperAdded.dispatch( helper );
126 127 128

		} else if ( object instanceof THREE.DirectionalLight ) {

M
Mr.doob 已提交
129 130 131 132
			var helper = new THREE.DirectionalLightHelper( object, 10 );
			helper.lightSphere.id = object.id;
			this.sceneHelpers.add( helper );
			this.helpers[ object.id ] = helper;
M
Mr.doob 已提交
133

M
Mr.doob 已提交
134
			this.signals.helperAdded.dispatch( helper );
135 136 137

		} else if ( object instanceof THREE.SpotLight ) {

M
Mr.doob 已提交
138 139 140 141
			var helper = new THREE.SpotLightHelper( object, 10 );
			helper.lightSphere.id = object.id;
			this.sceneHelpers.add( helper );
			this.helpers[ object.id ] = helper;
M
Mr.doob 已提交
142

M
Mr.doob 已提交
143
			this.signals.helperAdded.dispatch( helper );
144 145 146

		} else if ( object instanceof THREE.HemisphereLight ) {

M
Mr.doob 已提交
147 148 149 150
			var helper = new THREE.HemisphereLightHelper( object, 10 );
			helper.lightSphere.id = object.id;
			this.sceneHelpers.add( helper );
			this.helpers[ object.id ] = helper;
M
Mr.doob 已提交
151

M
Mr.doob 已提交
152
			this.signals.helperAdded.dispatch( helper );
153

M
Mr.doob 已提交
154
		}
155

156 157 158 159
	},

	removeHelper: function ( object ) {

160 161 162 163 164
		if ( this.helpers[ object.id ] !== undefined ) {

			this.helpers[ object.id ].parent.remove( this.helpers[ object.id ] );
			delete this.helpers[ object.id ];

M
Mr.doob 已提交
165 166
			this.signals.helperRemoved.dispatch( helper );

167 168
		}

169 170 171 172
	},

	//

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	parent: function ( object, parent ) {

		if ( parent === undefined ) {

			parent = this.scene;

		}

		parent.add( object );

		this.signals.sceneGraphChanged.dispatch();

	},

	//

189 190
	select: function ( object ) {

191 192 193
		this.selected = object;
		this.signals.objectSelected.dispatch( object );

194 195
	},

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	selectById: function ( id ) {

		var scope = this;

		this.scene.traverse( function ( node ) {

			if ( node.id === id ) {

				scope.select( node );

			}

		} );

	},

	deselect: function () {

M
Mr.doob 已提交
214
		this.select( null );
215 216 217 218

	}

}