Editor.js 4.2 KB
Newer Older
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
var Editor = function () {

	var SIGNALS = signals;

	this.signals = {

		// actions

		flattenSelectedObject: new SIGNALS.Signal(),
		cloneSelectedObject: new SIGNALS.Signal(),
		removeSelectedObject: new SIGNALS.Signal(),
		playAnimations: new SIGNALS.Signal(),

		// notifications

		transformModeChanged: new SIGNALS.Signal(),
		snapChanged: new SIGNALS.Signal(),
		rendererChanged: new SIGNALS.Signal(),
		sceneChanged: new SIGNALS.Signal(),
		objectAdded: new SIGNALS.Signal(),
		objectSelected: new SIGNALS.Signal(),
		objectChanged: new SIGNALS.Signal(),
		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();
33
	this.sceneHelpers = new THREE.Scene();
34 35 36 37 38 39

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

40 41 42
	this.selected = null;
	this.helpers = {};

43 44 45 46 47 48
};

Editor.prototype = {

	setScene: function ( scene ) {

49 50 51 52 53 54 55 56 57 58 59
		this.scene.name = scene.name;
		this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );

		while ( scene.children.length > 0 ) {

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

		}

		this.signals.sceneChanged.dispatch( this.scene );

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
	addObject: function ( object ) {

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

		this.signals.objectAdded.dispatch( object );
		this.signals.sceneChanged.dispatch();

	},

	removeObject: function ( object ) {

		if ( object === this.scene ) return;

		var name = object.name ?  '"' + object.name + '"': "selected object";

		if ( confirm( 'Delete ' + name + '?' ) === false ) return;

		this.scene.remove( object );
		this.removeHelper( object );
		this.signals.sceneChanged.dispatch();
85 86 87 88 89 90 91

	},

	addGeometry: function ( geometry  ) {

	},

92 93 94 95
	removeGeometry: function ( geometry  ) {

	},

96 97 98 99
	addMaterial: function ( material ) {

	},

100 101 102 103
	removeMaterial: function ( material ) {

	},

104 105 106 107
	addTexture: function ( texture ) {

	},

108 109 110 111
	removeTexture: function ( texture ) {

	},

112 113 114 115
	//

	addHelper: function ( object ) {

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
		if ( object instanceof THREE.PointLight ) {

			this.helpers[ object.id ] = new THREE.PointLightHelper( object, 10 );
			this.sceneHelpers.add( this.helpers[ object.id ] );
			this.helpers[ object.id ].lightSphere.id = object.id;

		} else if ( object instanceof THREE.DirectionalLight ) {

			this.helpers[ object.id ] = new THREE.DirectionalLightHelper( object, 10 );
			this.sceneHelpers.add( this.helpers[ object.id ] );
			this.helpers[ object.id ].lightSphere.id = object.id;

		} else if ( object instanceof THREE.SpotLight ) {

			this.helpers[ object.id ] = new THREE.SpotLightHelper( object, 10 );
			this.sceneHelpers.add( this.helpers[ object.id ] );
			// this.helpers[ object.id ].lightSphere.id = object.id;

		} else if ( object instanceof THREE.HemisphereLight ) {

			this.helpers[ object.id ] = new THREE.HemisphereLightHelper( object, 10 );
			this.sceneHelpers.add( this.helpers[ object.id ] );
			this.helpers[ object.id ].lightSphere.id = object.id;

		}

		this.signals.sceneChanged.dispatch();

144 145 146 147
	},

	removeHelper: function ( object ) {

148 149 150 151 152 153 154
		if ( this.helpers[ object.id ] !== undefined ) {

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

		}

155 156 157 158 159 160
	},

	//

	select: function ( object ) {

161 162 163
		this.selected = object;
		this.signals.objectSelected.dispatch( object );

164 165
	},

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	selectById: function ( id ) {

		var scope = this;

		this.scene.traverse( function ( node ) {

			if ( node.id === id ) {

				scope.select( node );

			}

		} );

	},

	deselect: function () {

		this.selected = null;
185 186 187 188 189 190 191

	},

	//

	cloneObject: function ( object ) {

192 193
		this.addObject( object.clone() );

194 195 196 197
	},

	flattenObject: function ( object ) {

198 199 200 201 202 203 204 205 206 207 208 209 210 211
		var name = object.name ?  '"' + object.name + '"': "selected object";

		if ( confirm( 'Flatten ' + name + '?' ) === false ) return;

		delete object.__webglInit; // TODO: Remove hack (WebGLRenderer refactoring)

		var geometry = object.geometry.clone();
		geometry.applyMatrix( object.matrix );

		object.setGeometry( geometry );

		object.position.set( 0, 0, 0 );
		object.rotation.set( 0, 0, 0 );
		object.scale.set( 1, 1, 1 );
212

213
		signals.objectChanged.dispatch( object );
214 215 216 217

	}

}