Menubar.File.js 6.1 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
	var fileInput = document.createElement( 'input' );
	fileInput.type = 'file';
	fileInput.addEventListener( 'change', function ( event ) {
54

55
		editor.loader.loadFile( fileInput.files[ 0 ] );
M
Mr.doob 已提交
56

57
	} );
M
Mr.doob 已提交
58

M
Mr.doob 已提交
59
	var option = new UI.Row();
60 61 62
	option.setClass( 'option' );
	option.setTextContent( 'Import' );
	option.onClick( function () {
M
Mr.doob 已提交
63

64
		fileInput.click();
M
Mr.doob 已提交
65

66 67
	} );
	options.add( option );
68

69
	//
70

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

73
	// Export Geometry
74

M
Mr.doob 已提交
75
	var option = new UI.Row();
76 77 78
	option.setClass( 'option' );
	option.setTextContent( 'Export Geometry' );
	option.onClick( function () {
79

80
		var object = editor.selected;
81

82
		if ( object === null ) {
83

84 85
			alert( 'No object selected.' );
			return;
86

87
		}
88

89 90 91 92 93 94
		var geometry = object.geometry;

		if ( geometry === undefined ) {

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

96
		}
97

98
		var output = geometry.toJSON();
M
Mr.doob 已提交
99

M
makc 已提交
100
		try {
M
Mr.doob 已提交
101

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

M
Mr.doob 已提交
105
		} catch ( e ) {
M
Mr.doob 已提交
106

M
makc 已提交
107
			output = JSON.stringify( output );
M
Mr.doob 已提交
108

M
makc 已提交
109
		}
110

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

113 114 115 116
	} );
	options.add( option );

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

M
Mr.doob 已提交
118
	var option = new UI.Row();
119 120 121
	option.setClass( 'option' );
	option.setTextContent( 'Export Object' );
	option.onClick( function () {
122

123 124 125
		var object = editor.selected;

		if ( object === null ) {
126 127 128 129 130 131

			alert( 'No object selected' );
			return;

		}

132
		var output = object.toJSON();
M
Mr.doob 已提交
133

M
makc 已提交
134
		try {
M
Mr.doob 已提交
135

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

M
Mr.doob 已提交
139
		} catch ( e ) {
M
Mr.doob 已提交
140

M
makc 已提交
141
			output = JSON.stringify( output );
M
Mr.doob 已提交
142

M
makc 已提交
143
		}
144

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

147 148 149 150
	} );
	options.add( option );

	// Export Scene
151

M
Mr.doob 已提交
152
	var option = new UI.Row();
153 154 155
	option.setClass( 'option' );
	option.setTextContent( 'Export Scene' );
	option.onClick( function () {
156

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

M
makc 已提交
159
		try {
M
Mr.doob 已提交
160

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

M
Mr.doob 已提交
164
		} catch ( e ) {
M
Mr.doob 已提交
165

M
makc 已提交
166
			output = JSON.stringify( output );
M
Mr.doob 已提交
167

M
makc 已提交
168
		}
169

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

172 173
	} );
	options.add( option );
M
Mr.doob 已提交
174

175 176
	// Export OBJ

M
Mr.doob 已提交
177
	var option = new UI.Row();
178 179 180
	option.setClass( 'option' );
	option.setTextContent( 'Export OBJ' );
	option.onClick( function () {
181

182 183 184 185 186 187 188 189 190
		var object = editor.selected;

		if ( object === null ) {

			alert( 'No object selected.' );
			return;

		}

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

M
Mr.doob 已提交
193
		saveString( exporter.parse( object ), 'model.obj' );
194

195 196 197 198
	} );
	options.add( option );

	// Export STL
199

M
Mr.doob 已提交
200
	var option = new UI.Row();
201 202 203
	option.setClass( 'option' );
	option.setTextContent( 'Export STL' );
	option.onClick( function () {
M
Mr.doob 已提交
204

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

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

209 210 211
	} );
	options.add( option );

M
Mr.doob 已提交
212
	//
213 214

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

216
	// Publish
217

M
Mr.doob 已提交
218
	var option = new UI.Row();
219
	option.setClass( 'option' );
220
	option.setTextContent( 'Publish' );
221
	option.onClick( function () {
222

M
Mr.doob 已提交
223 224 225 226
		var zip = new JSZip();

		//

M
Mr.doob 已提交
227
		var output = editor.toJSON();
M
Mr.doob 已提交
228 229 230
		output.metadata.type = 'App';
		delete output.history;

231 232
		var vr = output.project.vr;

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

M
Mr.doob 已提交
236
		zip.file( 'app.json', output );
M
Mr.doob 已提交
237 238 239 240 241

		//

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

M
Mr.doob 已提交
242
			save( zip.generate( { type: 'blob' } ), 'download.zip' );
M
Mr.doob 已提交
243 244 245

		} );

246
		var loader = new THREE.FileLoader( manager );
247 248
		loader.load( 'js/libs/app/index.html', function ( content ) {

249 250 251 252 253 254
			var includes = [];

			if ( vr ) {

				includes.push( '<script src="js/VRControls.js"></script>' );
				includes.push( '<script src="js/VREffect.js"></script>' );
M
Mr.doob 已提交
255
				includes.push( '<script src="js/WebVR.js"></script>' );
256 257 258 259 260

			}

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

261 262 263
			zip.file( 'index.html', content );

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

M
Mr.doob 已提交
266
			zip.file( 'js/app.js', content );
M
Mr.doob 已提交
267 268

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

M
Mr.doob 已提交
271
			zip.file( 'js/three.min.js', content );
M
Mr.doob 已提交
272 273

		} );
274

275
		if ( vr ) {
276

277
			loader.load( '../examples/js/controls/VRControls.js', function ( content ) {
278

279
				zip.file( 'js/VRControls.js', content );
280

281
			} );
282

283
			loader.load( '../examples/js/effects/VREffect.js', function ( content ) {
284

285 286 287 288
				zip.file( 'js/VREffect.js', content );

			} );

M
Mr.doob 已提交
289 290 291 292 293 294
			loader.load( '../examples/js/WebVR.js', function ( content ) {

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

			} );

295
		}
296

297 298
	} );
	options.add( option );
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	/*
	// 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 );
	*/

320

321
	//
322

M
Mr.doob 已提交
323 324 325 326
	var link = document.createElement( 'a' );
	link.style.display = 'none';
	document.body.appendChild( link ); // Firefox workaround, see #6594

M
Mr.doob 已提交
327
	function save( blob, filename ) {
328

M
Mr.doob 已提交
329
		link.href = URL.createObjectURL( blob );
M
Mr.doob 已提交
330
		link.download = filename || 'data.json';
M
Mr.doob 已提交
331 332 333 334 335 336 337
		link.click();

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

	}

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

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

M
Mr.doob 已提交
341
	}
342

343
	return container;
M
Mr.doob 已提交
344

345
};