Menubar.File.js 5.6 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

21 22 23 24
	var option = new UI.Panel();
	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

49 50 51 52
	var option = new UI.Panel();
	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

65 66 67 68
	var option = new UI.Panel();
	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 89 90
		var output = geometry.toJSON();
		output = JSON.stringify( output, null, '\t' );
		output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
91

A
Alexander 已提交
92
		exportString( output, "geometry.json" );
93

94 95 96 97
	} );
	options.add( option );

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

99 100 101 102
	var option = new UI.Panel();
	option.setClass( 'option' );
	option.setTextContent( 'Export Object' );
	option.onClick( function () {
103

104 105 106
		var object = editor.selected;

		if ( object === null ) {
107 108 109 110 111 112

			alert( 'No object selected' );
			return;

		}

113 114 115 116
		var output = object.toJSON();
		output = JSON.stringify( output, null, '\t' );
		output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );

A
Alexander 已提交
117
		exportString( output, "model.json" );
118

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

	// Export Scene
123

124 125 126 127
	var option = new UI.Panel();
	option.setClass( 'option' );
	option.setTextContent( 'Export Scene' );
	option.onClick( function () {
128

129 130 131 132
		var output = editor.scene.toJSON();
		output = JSON.stringify( output, null, '\t' );
		output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );

A
Alexander 已提交
133
		exportString( output, "scene.json" );
134

135 136
	} );
	options.add( option );
M
Mr.doob 已提交
137

138 139 140 141 142 143
	// Export OBJ

	var option = new UI.Panel();
	option.setClass( 'option' );
	option.setTextContent( 'Export OBJ' );
	option.onClick( function () {
144

145 146 147 148 149 150 151 152 153
		var object = editor.selected;

		if ( object === null ) {

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

		}

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

A
Alexander 已提交
156
		exportString( exporter.parse( object ), "model.obj" );
157

158 159 160 161
	} );
	options.add( option );

	// Export STL
162

163 164 165 166
	var option = new UI.Panel();
	option.setClass( 'option' );
	option.setTextContent( 'Export STL' );
	option.onClick( function () {
M
Mr.doob 已提交
167

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

A
Alexander 已提交
170
		exportString( exporter.parse( editor.scene ), "model.stl" );
M
Mr.doob 已提交
171

172 173 174
	} );
	options.add( option );

M
Mr.doob 已提交
175
	//
176 177

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

179
	// Publish
180 181 182

	var option = new UI.Panel();
	option.setClass( 'option' );
183
	option.setTextContent( 'Publish' );
184
	option.onClick( function () {
185

M
Mr.doob 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
		var camera = editor.camera;

		var zip = new JSZip();

		zip.file( 'index.html', [

			'<!DOCTYPE html>',
			'<html lang="en">',
			'	<head>',
			'		<title>three.js</title>',
			'		<meta charset="utf-8">',
			'		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">',
			'		<style>',
			'		body {',
			'			margin: 0px;',
			'			overflow: hidden;',
			'		}',
			'		</style>',
			'	</head>',
			'	<body ontouchstart="">',
			'		<script src="js/three.min.js"></script>',
M
Mr.doob 已提交
207
			'		<script src="js/app.js"></script>',
M
Mr.doob 已提交
208
			'		<script>',
M
Mr.doob 已提交
209
			'',
M
Mr.doob 已提交
210 211
			'			var loader = new THREE.XHRLoader();',
			'			loader.load( \'app.json\', function ( text ) {',
M
Mr.doob 已提交
212
			'',
M
Mr.doob 已提交
213 214 215 216
			'				var player = new APP.Player();',
			'				player.load( JSON.parse( text ) );',
			'				player.setSize( window.innerWidth, window.innerHeight );',
			'				player.play();',
M
Mr.doob 已提交
217
			'',
M
Mr.doob 已提交
218
			'				document.body.appendChild( player.dom );',
M
Mr.doob 已提交
219
			'',
M
Mr.doob 已提交
220
			'			} );',
M
Mr.doob 已提交
221
			'',
M
Mr.doob 已提交
222 223 224 225 226 227 228 229
			'		</script>',
			'	</body>',
			'</html>'

		].join( '\n' ) );

		//

M
Mr.doob 已提交
230
		var output = editor.toJSON();
M
Mr.doob 已提交
231 232 233
		output = JSON.stringify( output, null, '\t' );
		output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );

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

		//

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

			location.href = 'data:application/zip;base64,' + zip.generate();

		} );

		var loader = new THREE.XHRLoader( manager );
M
Mr.doob 已提交
245
		loader.load( 'js/libs/app.js', function ( content ) {
M
Mr.doob 已提交
246

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

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

M
Mr.doob 已提交
252
			zip.file( 'js/three.min.js', content );
M
Mr.doob 已提交
253 254

		} );
255

256 257
	} );
	options.add( option );
258

M
Mr.doob 已提交
259
	/*
260
	// Test
M
Mr.doob 已提交
261 262 263

	var option = new UI.Panel();
	option.setClass( 'option' );
264
	option.setTextContent( 'Test' );
M
Mr.doob 已提交
265 266
	option.onClick( function () {

267 268
		var text = new UI.Text( 'blah' );
		editor.showDialog( text );
M
Mr.doob 已提交
269 270 271

	} );
	options.add( option );
M
Mr.doob 已提交
272
	*/
M
Mr.doob 已提交
273

274

275
	//
276

A
Alexander 已提交
277
	var exportString = function ( output, filename ) {
278 279 280 281

		var blob = new Blob( [ output ], { type: 'text/plain' } );
		var objectURL = URL.createObjectURL( blob );

A
Alexander 已提交
282 283 284 285 286
		var link = document.createElement('a');
		link.href = objectURL;
		link.download = filename || "data.json";
		link.target = "_blank";
		link.click();
287 288

	};
289

290
	return container;
M
Mr.doob 已提交
291

292
};