Menubar.File.js 4.8 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
	var container = new UI.Panel();
	container.setClass( 'menu' );
M
Mr.doob 已提交
9

10 11 12 13
	var title = new UI.Panel();
	title.setClass( 'title' );
	title.setTextContent( 'File' );
	container.add( title );
M
Mr.doob 已提交
14

15 16 17
	var options = new UI.Panel();
	options.setClass( 'options' );
	container.add( options );
M
Mr.doob 已提交
18

19
	// New
20

M
Mr.doob 已提交
21
	var option = new UI.Row();
22 23 24
	option.setClass( 'option' );
	option.setTextContent( 'New' );
	option.onClick( function () {
25

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

M
Mr.doob 已提交
28
			editor.clear();
M
Mr.doob 已提交
29 30

		}
31

32 33
	} );
	options.add( option );
M
Mr.doob 已提交
34

35
	//
36

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

39
	// Import
40

41 42 43
	var fileInput = document.createElement( 'input' );
	fileInput.type = 'file';
	fileInput.addEventListener( 'change', function ( event ) {
44

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

47
	} );
M
Mr.doob 已提交
48

M
Mr.doob 已提交
49
	var option = new UI.Row();
50 51 52
	option.setClass( 'option' );
	option.setTextContent( 'Import' );
	option.onClick( function () {
M
Mr.doob 已提交
53

54
		fileInput.click();
M
Mr.doob 已提交
55

56 57
	} );
	options.add( option );
58

59
	//
60

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

63
	// Export Geometry
64

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

70
		var object = editor.selected;
71

72
		if ( object === null ) {
73

74 75
			alert( 'No object selected.' );
			return;
76

77
		}
78

79 80 81 82 83 84
		var geometry = object.geometry;

		if ( geometry === undefined ) {

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

86
		}
87

88
		var output = geometry.toJSON();
M
Mr.doob 已提交
89

M
makc 已提交
90
		try {
M
Mr.doob 已提交
91

M
makc 已提交
92 93
			output = JSON.stringify( output, null, '\t' );
			output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
M
Mr.doob 已提交
94

M
Mr.doob 已提交
95
		} catch ( e ) {
M
Mr.doob 已提交
96

M
makc 已提交
97
			output = JSON.stringify( output );
M
Mr.doob 已提交
98

M
makc 已提交
99
		}
100

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

103 104 105 106
	} );
	options.add( option );

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

M
Mr.doob 已提交
108
	var option = new UI.Row();
109 110 111
	option.setClass( 'option' );
	option.setTextContent( 'Export Object' );
	option.onClick( function () {
112

113 114 115
		var object = editor.selected;

		if ( object === null ) {
116 117 118 119 120 121

			alert( 'No object selected' );
			return;

		}

122
		var output = object.toJSON();
M
Mr.doob 已提交
123

M
makc 已提交
124
		try {
M
Mr.doob 已提交
125

M
makc 已提交
126 127
			output = JSON.stringify( output, null, '\t' );
			output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
M
Mr.doob 已提交
128

M
Mr.doob 已提交
129
		} catch ( e ) {
M
Mr.doob 已提交
130

M
makc 已提交
131
			output = JSON.stringify( output );
M
Mr.doob 已提交
132

M
makc 已提交
133
		}
134

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

137 138 139 140
	} );
	options.add( option );

	// Export Scene
141

M
Mr.doob 已提交
142
	var option = new UI.Row();
143 144 145
	option.setClass( 'option' );
	option.setTextContent( 'Export Scene' );
	option.onClick( function () {
146

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

M
makc 已提交
149
		try {
M
Mr.doob 已提交
150

M
makc 已提交
151 152
			output = JSON.stringify( output, null, '\t' );
			output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
M
Mr.doob 已提交
153

M
Mr.doob 已提交
154
		} catch ( e ) {
M
Mr.doob 已提交
155

M
makc 已提交
156
			output = JSON.stringify( output );
M
Mr.doob 已提交
157

M
makc 已提交
158
		}
159

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

162 163
	} );
	options.add( option );
M
Mr.doob 已提交
164

165 166
	// Export OBJ

M
Mr.doob 已提交
167
	var option = new UI.Row();
168 169 170
	option.setClass( 'option' );
	option.setTextContent( 'Export OBJ' );
	option.onClick( function () {
171

172 173 174 175 176 177 178 179 180
		var object = editor.selected;

		if ( object === null ) {

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

		}

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

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

185 186 187 188
	} );
	options.add( option );

	// Export STL
189

M
Mr.doob 已提交
190
	var option = new UI.Row();
191 192 193
	option.setClass( 'option' );
	option.setTextContent( 'Export STL' );
	option.onClick( function () {
M
Mr.doob 已提交
194

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

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

199 200 201
	} );
	options.add( option );

M
Mr.doob 已提交
202
	//
203 204

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

206
	// Publish
207

M
Mr.doob 已提交
208
	var option = new UI.Row();
209
	option.setClass( 'option' );
210
	option.setTextContent( 'Publish' );
211
	option.onClick( function () {
212

M
Mr.doob 已提交
213 214 215 216
		var zip = new JSZip();

		//

M
Mr.doob 已提交
217
		var output = editor.toJSON();
M
Mr.doob 已提交
218 219 220
		output.metadata.type = 'App';
		delete output.history;

M
Mr.doob 已提交
221 222 223
		output = JSON.stringify( output, null, '\t' );
		output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );

M
Mr.doob 已提交
224
		zip.file( 'app.json', output );
M
Mr.doob 已提交
225 226 227 228 229

		//

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

M
Mr.doob 已提交
230
			save( zip.generate( { type: 'blob' } ), 'download.zip' );
M
Mr.doob 已提交
231 232 233 234

		} );

		var loader = new THREE.XHRLoader( manager );
235 236 237 238 239
		loader.load( 'js/libs/app/index.html', function ( content ) {

			zip.file( 'index.html', content );

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

M
Mr.doob 已提交
242
			zip.file( 'js/app.js', content );
M
Mr.doob 已提交
243 244

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

M
Mr.doob 已提交
247
			zip.file( 'js/three.min.js', content );
M
Mr.doob 已提交
248 249

		} );
250

251 252
	} );
	options.add( option );
253

254

255
	//
256

M
Mr.doob 已提交
257 258 259 260
	var link = document.createElement( 'a' );
	link.style.display = 'none';
	document.body.appendChild( link ); // Firefox workaround, see #6594

M
Mr.doob 已提交
261
	function save( blob, filename ) {
262

M
Mr.doob 已提交
263
		link.href = URL.createObjectURL( blob );
M
Mr.doob 已提交
264
		link.download = filename || 'data.json';
M
Mr.doob 已提交
265 266 267 268 269 270 271
		link.click();

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

	}

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

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

M
Mr.doob 已提交
275
	}
276

277
	return container;
M
Mr.doob 已提交
278

279
};