Menubar.File.js 6.3 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4
/**
 * @author mrdoob / http://mrdoob.com/
 */

5
Menubar.File = function ( editor ) {
M
Mr.doob 已提交
6

7 8 9 10 11 12 13 14 15 16
	var NUMBER_PRECISION = 6;

	function parseNumber( key, value ) {

		return typeof value === 'number' ? parseFloat( value.toFixed( NUMBER_PRECISION ) ) : value;

	}

	//

17 18
	var container = new UI.Panel();
	container.setClass( 'menu' );
M
Mr.doob 已提交
19

20 21 22 23
	var title = new UI.Panel();
	title.setClass( 'title' );
	title.setTextContent( 'File' );
	container.add( title );
M
Mr.doob 已提交
24

25 26 27
	var options = new UI.Panel();
	options.setClass( 'options' );
	container.add( options );
M
Mr.doob 已提交
28

29
	// New
30

M
Mr.doob 已提交
31
	var option = new UI.Row();
32 33 34
	option.setClass( 'option' );
	option.setTextContent( 'New' );
	option.onClick( function () {
35

M
Mr.doob 已提交
36
		if ( confirm( 'Any unsaved data will be lost. Are you sure?' ) ) {
37

M
Mr.doob 已提交
38
			editor.clear();
M
Mr.doob 已提交
39 40

		}
41

42 43
	} );
	options.add( option );
M
Mr.doob 已提交
44

45
	//
46

47
	options.add( new UI.HorizontalRule() );
48

49
	// Import
50

51 52 53 54
	var form = document.createElement( 'form' );
	form.style.display = 'none';
	document.body.appendChild( form );

55 56 57
	var fileInput = document.createElement( 'input' );
	fileInput.type = 'file';
	fileInput.addEventListener( 'change', function ( event ) {
58

59
		editor.loader.loadFile( fileInput.files[ 0 ] );
60
		form.reset();
M
Mr.doob 已提交
61

62
	} );
63
	form.appendChild( fileInput );
M
Mr.doob 已提交
64

M
Mr.doob 已提交
65
	var option = new UI.Row();
66 67 68
	option.setClass( 'option' );
	option.setTextContent( 'Import' );
	option.onClick( function () {
M
Mr.doob 已提交
69

70
		fileInput.click();
M
Mr.doob 已提交
71

72 73
	} );
	options.add( option );
74

75
	//
76

77
	options.add( new UI.HorizontalRule() );
78

79
	// Export Geometry
80

M
Mr.doob 已提交
81
	var option = new UI.Row();
82 83 84
	option.setClass( 'option' );
	option.setTextContent( 'Export Geometry' );
	option.onClick( function () {
85

86
		var object = editor.selected;
87

88
		if ( object === null ) {
89

90 91
			alert( 'No object selected.' );
			return;
92

93
		}
94

95 96 97 98 99 100
		var geometry = object.geometry;

		if ( geometry === undefined ) {

			alert( 'The selected object doesn\'t have geometry.' );
			return;
101

102
		}
103

104
		var output = geometry.toJSON();
M
Mr.doob 已提交
105

M
makc 已提交
106
		try {
M
Mr.doob 已提交
107

108
			output = JSON.stringify( output, parseNumber, '\t' );
M
makc 已提交
109
			output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
M
Mr.doob 已提交
110

M
Mr.doob 已提交
111
		} catch ( e ) {
M
Mr.doob 已提交
112

M
makc 已提交
113
			output = JSON.stringify( output );
M
Mr.doob 已提交
114

M
makc 已提交
115
		}
116

M
Mr.doob 已提交
117
		saveString( output, 'geometry.json' );
118

119 120 121 122
	} );
	options.add( option );

	// Export Object
M
Mr.doob 已提交
123

M
Mr.doob 已提交
124
	var option = new UI.Row();
125 126 127
	option.setClass( 'option' );
	option.setTextContent( 'Export Object' );
	option.onClick( function () {
128

129 130 131
		var object = editor.selected;

		if ( object === null ) {
132 133 134 135 136 137

			alert( 'No object selected' );
			return;

		}

138
		var output = object.toJSON();
M
Mr.doob 已提交
139

M
makc 已提交
140
		try {
M
Mr.doob 已提交
141

142
			output = JSON.stringify( output, parseNumber, '\t' );
M
makc 已提交
143
			output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
M
Mr.doob 已提交
144

M
Mr.doob 已提交
145
		} catch ( e ) {
M
Mr.doob 已提交
146

M
makc 已提交
147
			output = JSON.stringify( output );
M
Mr.doob 已提交
148

M
makc 已提交
149
		}
150

M
Mr.doob 已提交
151
		saveString( output, 'model.json' );
152

153 154 155 156
	} );
	options.add( option );

	// Export Scene
157

M
Mr.doob 已提交
158
	var option = new UI.Row();
159 160 161
	option.setClass( 'option' );
	option.setTextContent( 'Export Scene' );
	option.onClick( function () {
162

163
		var output = editor.scene.toJSON();
M
Mr.doob 已提交
164

M
makc 已提交
165
		try {
M
Mr.doob 已提交
166

167
			output = JSON.stringify( output, parseNumber, '\t' );
M
makc 已提交
168
			output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
M
Mr.doob 已提交
169

M
Mr.doob 已提交
170
		} catch ( e ) {
M
Mr.doob 已提交
171

M
makc 已提交
172
			output = JSON.stringify( output );
M
Mr.doob 已提交
173

M
makc 已提交
174
		}
175

M
Mr.doob 已提交
176
		saveString( output, 'scene.json' );
177

178 179
	} );
	options.add( option );
M
Mr.doob 已提交
180

M
Mr.doob 已提交
181 182 183 184 185
	//

	options.add( new UI.HorizontalRule() );

	// Export GLTF
186

M
Mr.doob 已提交
187
	var option = new UI.Row();
188
	option.setClass( 'option' );
M
Mr.doob 已提交
189
	option.setTextContent( 'Export GLTF' );
190
	option.onClick( function () {
191

M
Mr.doob 已提交
192
		var exporter = new THREE.GLTFExporter();
193

M
Mr.doob 已提交
194
		exporter.parse( editor.scene, function ( result ) {
195

M
Mr.doob 已提交
196
			saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
197

M
Mr.doob 已提交
198
		} );
199

200

201 202 203
	} );
	options.add( option );

M
Mr.doob 已提交
204
	// Export OBJ
F
Fernando Serrano 已提交
205 206 207

	var option = new UI.Row();
	option.setClass( 'option' );
M
Mr.doob 已提交
208
	option.setTextContent( 'Export OBJ' );
F
Fernando Serrano 已提交
209 210
	option.onClick( function () {

M
Mr.doob 已提交
211
		var object = editor.selected;
F
Fernando Serrano 已提交
212

M
Mr.doob 已提交
213
		if ( object === null ) {
F
Fernando Serrano 已提交
214

M
Mr.doob 已提交
215 216
			alert( 'No object selected.' );
			return;
F
Fernando Serrano 已提交
217

M
Mr.doob 已提交
218
		}
F
Fernando Serrano 已提交
219

M
Mr.doob 已提交
220 221 222
		var exporter = new THREE.OBJExporter();

		saveString( exporter.parse( object ), 'model.obj' );
F
Fernando Serrano 已提交
223 224 225 226

	} );
	options.add( option );

227
	// Export STL
228

M
Mr.doob 已提交
229
	var option = new UI.Row();
230 231 232
	option.setClass( 'option' );
	option.setTextContent( 'Export STL' );
	option.onClick( function () {
M
Mr.doob 已提交
233

M
Mr.doob 已提交
234
		var exporter = new THREE.STLExporter();
235

M
Mr.doob 已提交
236
		saveString( exporter.parse( editor.scene ), 'model.stl' );
M
Mr.doob 已提交
237

238 239 240
	} );
	options.add( option );

M
Mr.doob 已提交
241
	//
242 243

	options.add( new UI.HorizontalRule() );
M
Mr.doob 已提交
244

245
	// Publish
246

M
Mr.doob 已提交
247
	var option = new UI.Row();
248
	option.setClass( 'option' );
249
	option.setTextContent( 'Publish' );
250
	option.onClick( function () {
251

M
Mr.doob 已提交
252 253 254 255
		var zip = new JSZip();

		//

M
Mr.doob 已提交
256
		var output = editor.toJSON();
M
Mr.doob 已提交
257 258 259
		output.metadata.type = 'App';
		delete output.history;

260 261
		var vr = output.project.vr;

262
		output = JSON.stringify( output, parseNumber, '\t' );
M
Mr.doob 已提交
263 264
		output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );

M
Mr.doob 已提交
265
		zip.file( 'app.json', output );
M
Mr.doob 已提交
266 267 268 269 270

		//

		var manager = new THREE.LoadingManager( function () {

M
Mr.doob 已提交
271
			save( zip.generate( { type: 'blob' } ), 'download.zip' );
M
Mr.doob 已提交
272 273 274

		} );

275
		var loader = new THREE.FileLoader( manager );
276 277
		loader.load( 'js/libs/app/index.html', function ( content ) {

278 279 280 281
			var includes = [];

			if ( vr ) {

M
Mr.doob 已提交
282
				includes.push( '<script src="js/WebVR.js"></script>' );
283 284 285 286 287

			}

			content = content.replace( '<!-- includes -->', includes.join( '\n\t\t' ) );

288 289 290
			zip.file( 'index.html', content );

		} );
M
Mr.doob 已提交
291
		loader.load( 'js/libs/app.js', function ( content ) {
M
Mr.doob 已提交
292

M
Mr.doob 已提交
293
			zip.file( 'js/app.js', content );
M
Mr.doob 已提交
294 295

		} );
M
Mr.doob 已提交
296
		loader.load( '../build/three.min.js', function ( content ) {
M
Mr.doob 已提交
297

M
Mr.doob 已提交
298
			zip.file( 'js/three.min.js', content );
M
Mr.doob 已提交
299 300

		} );
301

302
		if ( vr ) {
303

M
Mr.doob 已提交
304
			loader.load( '../examples/js/vr/WebVR.js', function ( content ) {
M
Mr.doob 已提交
305 306 307 308 309

				zip.file( 'js/WebVR.js', content );

			} );

310
		}
311

312 313
	} );
	options.add( option );
314

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
	/*
	// Publish (Dropbox)

	var option = new UI.Row();
	option.setClass( 'option' );
	option.setTextContent( 'Publish (Dropbox)' );
	option.onClick( function () {

		var parameters = {
			files: [
				{ 'url': 'data:text/plain;base64,' + window.btoa( "Hello, World" ), 'filename': 'app/test.txt' }
			]
		};

		Dropbox.save( parameters );

	} );
	options.add( option );
	*/

335

336
	//
337

M
Mr.doob 已提交
338 339 340 341
	var link = document.createElement( 'a' );
	link.style.display = 'none';
	document.body.appendChild( link ); // Firefox workaround, see #6594

M
Mr.doob 已提交
342
	function save( blob, filename ) {
343

M
Mr.doob 已提交
344
		link.href = URL.createObjectURL( blob );
M
Mr.doob 已提交
345
		link.download = filename || 'data.json';
M
Mr.doob 已提交
346 347 348 349 350 351 352
		link.click();

		// URL.revokeObjectURL( url ); breaks Firefox...

	}

	function saveString( text, filename ) {
G
gero3 已提交
353

M
Mr.doob 已提交
354
		save( new Blob( [ text ], { type: 'text/plain' } ), filename );
355

M
Mr.doob 已提交
356
	}
357

358
	return container;
M
Mr.doob 已提交
359

360
};