Menubar.File.js 4.7 KB
Newer Older
1
Menubar.File = function ( editor ) {
M
Mr.doob 已提交
2

M
Mr.doob 已提交
3
	var container = new UI.Panel();
M
Mr.doob 已提交
4
	container.setClass( 'menu' );
5 6
	container.onMouseOver( function () { options.setDisplay( 'block' ) } );
	container.onMouseOut( function () { options.setDisplay( 'none' ) } );
M
Mr.doob 已提交
7
	container.onClick( function () { options.setDisplay( 'block' ) } );
M
Mr.doob 已提交
8 9 10 11

	var title = new UI.Panel();
	title.setTextContent( 'File' ).setColor( '#666' );
	title.setMargin( '0px' );
M
Mr.doob 已提交
12
	title.setPadding( '8px' );
M
Mr.doob 已提交
13 14 15 16 17
	container.add( title );

	//

	var options = new UI.Panel();
M
Mr.doob 已提交
18
	options.setClass( 'options' );
19
	options.setDisplay( 'none' );
M
Mr.doob 已提交
20 21
	container.add( options );

M
Mr.doob 已提交
22
	// new
M
Mr.doob 已提交
23

M
Mr.doob 已提交
24
	var option = new UI.Panel();
M
Mr.doob 已提交
25
	option.setClass( 'option' );
M
Mr.doob 已提交
26
	option.setTextContent( 'New' );
27 28 29 30 31 32 33 34 35 36
	option.onClick( function () {

		if ( confirm( 'Are you sure?' ) ) {

			if ( localStorage.threejsEditor !== undefined ) {

				delete localStorage.threejsEditor;

			}

M
Mr.doob 已提交
37
			location.href = location.pathname;
38 39 40 41 42 43

		}

	} );
	options.add( option );

M
Mr.doob 已提交
44 45 46
	options.add( new UI.HorizontalRule() );


M
Mr.doob 已提交
47 48 49 50 51 52 53 54 55
	// import

	var input = document.createElement( 'input' );
	input.type = 'file';
	input.addEventListener( 'change', function ( event ) {

		editor.loader.loadFile( input.files[ 0 ] );

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

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

M
Mr.doob 已提交
62
		input.click();
M
Mr.doob 已提交
63 64 65 66 67 68 69

	} );
	options.add( option );

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


70 71 72 73 74 75 76 77 78 79 80 81 82
	// export buffergeometry

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

		exportGeometry( THREE.BufferGeometryExporter );

	} );
	options.add( option );


83 84
	// export geometry

M
Mr.doob 已提交
85
	var option = new UI.Panel();
M
Mr.doob 已提交
86 87
	option.setClass( 'option' );
	option.setTextContent( 'Export Geometry' );
88 89 90 91 92
	option.onClick( function () {

		exportGeometry( THREE.GeometryExporter );

	} );
M
Mr.doob 已提交
93 94
	options.add( option );

M
Mr.doob 已提交
95 96
	/*

97 98
	// export scene

M
Mr.doob 已提交
99
	var option = new UI.Panel();
M
Mr.doob 已提交
100 101
	option.setClass( 'option' );
	option.setTextContent( 'Export Scene' );
102 103 104 105 106
	option.onClick( function () {

		exportScene( THREE.SceneExporter );

	} );
M
Mr.doob 已提交
107 108
	options.add( option );

M
Mr.doob 已提交
109 110
	*/

111
	// export object
M
Mr.doob 已提交
112 113 114

	var option = new UI.Panel();
	option.setClass( 'option' );
115
	option.setTextContent( 'Export Object' );
116 117
	option.onClick( function () {

118 119 120 121 122 123 124 125 126 127 128 129 130
		exportObject( THREE.ObjectExporter );

	} );
	options.add( option );

	// export scene

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

		exportScene( THREE.ObjectExporter );
131 132

	} );
M
Mr.doob 已提交
133 134
	options.add( option );

135 136
	// export OBJ

M
Mr.doob 已提交
137
	var option = new UI.Panel();
M
Mr.doob 已提交
138 139
	option.setClass( 'option' );
	option.setTextContent( 'Export OBJ' );
140 141 142 143 144
	option.onClick( function () {

		exportGeometry( THREE.OBJExporter );

	} );
M
Mr.doob 已提交
145
	options.add( option );
M
Mr.doob 已提交
146

147 148
	var exportGeometry = function ( exporterClass ) {

M
Mr.doob 已提交
149 150 151
		var object = editor.selected;

		if ( object.geometry === undefined ) {
152 153 154 155 156 157 158 159 160 161

			alert( "Selected object doesn't have any geometry" );
			return;

		}

		var exporter = new exporterClass();

		var output;

162
		if ( exporter instanceof THREE.BufferGeometryExporter || exporter instanceof THREE.GeometryExporter ) {
163

M
Mr.doob 已提交
164
			output = JSON.stringify( exporter.parse( object.geometry ), null, '\t' );
165 166 167 168
			output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );

		} else {

M
Mr.doob 已提交
169
			output = exporter.parse( object.geometry );
170 171 172 173 174 175 176 177 178 179 180

		}

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

		window.open( objectURL, '_blank' );
		window.focus();

	};

181 182 183 184
	var exportObject = function ( exporterClass ) {

		var exporter = new exporterClass();

M
Mr.doob 已提交
185 186 187
		var object = editor.selected;

		var output = JSON.stringify( exporter.parse( object ), null, '\t' );
188 189 190 191 192 193 194 195 196 197
		output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );

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

		window.open( objectURL, '_blank' );
		window.focus();

	};

198 199 200 201
	var exportScene = function ( exporterClass ) {

		var exporter = new exporterClass();

M
Mr.doob 已提交
202
		var output = JSON.stringify( exporter.parse( editor.scene ), null, '\t' );
203 204 205 206 207 208 209 210 211 212
		output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );

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

		window.open( objectURL, '_blank' );
		window.focus();

	};

M
Mr.doob 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	options.add( new UI.HorizontalRule() );


	// share

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

		var exporter = new THREE.ObjectExporter();
		var string = JSON.stringify( exporter.parse( editor.scene ) );
		window.location.hash = 'A/' + window.btoa( RawDeflate.deflate( string ) );

	} );
	options.add( option );


M
Mr.doob 已提交
231 232 233
	return container;

}