ui.three.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. UI.Texture = function ( mapping ) {
  5. UI.Element.call( this );
  6. var scope = this;
  7. var dom = document.createElement( 'span' );
  8. var form = document.createElement( 'form' );
  9. var input = document.createElement( 'input' );
  10. input.type = 'file';
  11. input.addEventListener( 'change', function ( event ) {
  12. loadFile( event.target.files[ 0 ] );
  13. } );
  14. form.appendChild( input );
  15. var canvas = document.createElement( 'canvas' );
  16. canvas.width = 32;
  17. canvas.height = 16;
  18. canvas.style.cursor = 'pointer';
  19. canvas.style.marginRight = '5px';
  20. canvas.style.border = '1px solid #888';
  21. canvas.addEventListener( 'click', function ( event ) {
  22. input.click();
  23. }, false );
  24. canvas.addEventListener( 'drop', function ( event ) {
  25. event.preventDefault();
  26. event.stopPropagation();
  27. loadFile( event.dataTransfer.files[ 0 ] );
  28. }, false );
  29. dom.appendChild( canvas );
  30. var name = document.createElement( 'input' );
  31. name.disabled = true;
  32. name.style.width = '64px';
  33. name.style.border = '1px solid #ccc';
  34. dom.appendChild( name );
  35. function loadFile( file ) {
  36. if ( file.type.match( 'image.*' ) ) {
  37. var reader = new FileReader();
  38. if ( file.type === 'image/targa' ) {
  39. reader.addEventListener( 'load', function ( event ) {
  40. var canvas = new THREE.TGALoader().parse( event.target.result );
  41. var texture = new THREE.CanvasTexture( canvas, mapping );
  42. texture.sourceFile = file.name;
  43. scope.setValue( texture );
  44. if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
  45. }, false );
  46. reader.readAsArrayBuffer( file );
  47. } else {
  48. reader.addEventListener( 'load', function ( event ) {
  49. var image = document.createElement( 'img' );
  50. image.addEventListener( 'load', function ( event ) {
  51. var texture = new THREE.Texture( this, mapping );
  52. texture.sourceFile = file.name;
  53. texture.format = file.type === 'image/jpeg' ? THREE.RGBFormat : THREE.RGBAFormat;
  54. texture.needsUpdate = true;
  55. scope.setValue( texture );
  56. if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
  57. }, false );
  58. image.src = event.target.result;
  59. }, false );
  60. reader.readAsDataURL( file );
  61. }
  62. }
  63. form.reset();
  64. }
  65. this.dom = dom;
  66. this.texture = null;
  67. this.onChangeCallback = null;
  68. return this;
  69. };
  70. UI.Texture.prototype = Object.create( UI.Element.prototype );
  71. UI.Texture.prototype.constructor = UI.Texture;
  72. UI.Texture.prototype.getValue = function () {
  73. return this.texture;
  74. };
  75. UI.Texture.prototype.setValue = function ( texture ) {
  76. var canvas = this.dom.children[ 0 ];
  77. var name = this.dom.children[ 1 ];
  78. var context = canvas.getContext( '2d' );
  79. if ( texture !== null ) {
  80. var image = texture.image;
  81. if ( image !== undefined && image.width > 0 ) {
  82. name.value = texture.sourceFile;
  83. var scale = canvas.width / image.width;
  84. context.drawImage( image, 0, 0, image.width * scale, image.height * scale );
  85. } else {
  86. name.value = texture.sourceFile + ' (error)';
  87. context.clearRect( 0, 0, canvas.width, canvas.height );
  88. }
  89. } else {
  90. name.value = '';
  91. if ( context !== null ) {
  92. // Seems like context can be null if the canvas is not visible
  93. context.clearRect( 0, 0, canvas.width, canvas.height );
  94. }
  95. }
  96. this.texture = texture;
  97. };
  98. UI.Texture.prototype.setEncoding = function ( encoding ) {
  99. var texture = this.getValue();
  100. if ( texture !== null ) {
  101. texture.encoding = encoding;
  102. }
  103. return this;
  104. };
  105. UI.Texture.prototype.onChange = function ( callback ) {
  106. this.onChangeCallback = callback;
  107. return this;
  108. };
  109. // Outliner
  110. UI.Outliner = function ( editor ) {
  111. UI.Element.call( this );
  112. var scope = this;
  113. var dom = document.createElement( 'div' );
  114. dom.className = 'Outliner';
  115. dom.tabIndex = 0; // keyup event is ignored without setting tabIndex
  116. // hack
  117. this.scene = editor.scene;
  118. // Prevent native scroll behavior
  119. dom.addEventListener( 'keydown', function ( event ) {
  120. switch ( event.keyCode ) {
  121. case 38: // up
  122. case 40: // down
  123. event.preventDefault();
  124. event.stopPropagation();
  125. break;
  126. }
  127. }, false );
  128. // Keybindings to support arrow navigation
  129. dom.addEventListener( 'keyup', function ( event ) {
  130. switch ( event.keyCode ) {
  131. case 38: // up
  132. scope.selectIndex( scope.selectedIndex - 1 );
  133. break;
  134. case 40: // down
  135. scope.selectIndex( scope.selectedIndex + 1 );
  136. break;
  137. }
  138. }, false );
  139. this.dom = dom;
  140. this.options = [];
  141. this.selectedIndex = - 1;
  142. this.selectedValue = null;
  143. return this;
  144. };
  145. UI.Outliner.prototype = Object.create( UI.Element.prototype );
  146. UI.Outliner.prototype.constructor = UI.Outliner;
  147. UI.Outliner.prototype.selectIndex = function ( index ) {
  148. if ( index >= 0 && index < this.options.length ) {
  149. this.setValue( this.options[ index ].value );
  150. var changeEvent = document.createEvent( 'HTMLEvents' );
  151. changeEvent.initEvent( 'change', true, true );
  152. this.dom.dispatchEvent( changeEvent );
  153. }
  154. };
  155. UI.Outliner.prototype.setOptions = function ( options ) {
  156. var scope = this;
  157. while ( scope.dom.children.length > 0 ) {
  158. scope.dom.removeChild( scope.dom.firstChild );
  159. }
  160. function onClick() {
  161. scope.setValue( this.value );
  162. var changeEvent = document.createEvent( 'HTMLEvents' );
  163. changeEvent.initEvent( 'change', true, true );
  164. scope.dom.dispatchEvent( changeEvent );
  165. }
  166. // Drag
  167. var currentDrag;
  168. function onDrag( event ) {
  169. currentDrag = this;
  170. }
  171. function onDragStart( event ) {
  172. event.dataTransfer.setData( 'text', 'foo' );
  173. }
  174. function onDragOver( event ) {
  175. if ( this === currentDrag ) return;
  176. var area = event.offsetY / this.clientHeight;
  177. if ( area < 0.25 ) {
  178. this.className = 'option dragTop';
  179. } else if ( area > 0.75 ) {
  180. this.className = 'option dragBottom';
  181. } else {
  182. this.className = 'option drag';
  183. }
  184. }
  185. function onDragLeave() {
  186. if ( this === currentDrag ) return;
  187. this.className = 'option';
  188. }
  189. function onDrop( event ) {
  190. if ( this === currentDrag ) return;
  191. this.className = 'option';
  192. var scene = scope.scene;
  193. var object = scene.getObjectById( currentDrag.value );
  194. var area = event.offsetY / this.clientHeight;
  195. if ( area < 0.25 ) {
  196. var nextObject = scene.getObjectById( this.value );
  197. moveObject( object, nextObject.parent, nextObject );
  198. } else if ( area > 0.75 ) {
  199. var nextObject = scene.getObjectById( this.nextSibling.value );
  200. moveObject( object, nextObject.parent, nextObject );
  201. } else {
  202. var parentObject = scene.getObjectById( this.value );
  203. moveObject( object, parentObject );
  204. }
  205. }
  206. function moveObject( object, newParent, nextObject ) {
  207. if ( nextObject === null ) nextObject = undefined;
  208. var newParentIsChild = false;
  209. object.traverse( function ( child ) {
  210. if ( child === newParent ) newParentIsChild = true;
  211. } );
  212. if ( newParentIsChild ) return;
  213. editor.execute( new MoveObjectCommand( object, newParent, nextObject ) );
  214. var changeEvent = document.createEvent( 'HTMLEvents' );
  215. changeEvent.initEvent( 'change', true, true );
  216. scope.dom.dispatchEvent( changeEvent );
  217. }
  218. //
  219. scope.options = [];
  220. for ( var i = 0; i < options.length; i ++ ) {
  221. var div = options[ i ];
  222. div.className = 'option';
  223. scope.dom.appendChild( div );
  224. scope.options.push( div );
  225. div.addEventListener( 'click', onClick, false );
  226. if ( div.draggable === true ) {
  227. div.addEventListener( 'drag', onDrag, false );
  228. div.addEventListener( 'dragstart', onDragStart, false ); // Firefox needs this
  229. div.addEventListener( 'dragover', onDragOver, false );
  230. div.addEventListener( 'dragleave', onDragLeave, false );
  231. div.addEventListener( 'drop', onDrop, false );
  232. }
  233. }
  234. return scope;
  235. };
  236. UI.Outliner.prototype.getValue = function () {
  237. return this.selectedValue;
  238. };
  239. UI.Outliner.prototype.setValue = function ( value ) {
  240. for ( var i = 0; i < this.options.length; i ++ ) {
  241. var element = this.options[ i ];
  242. if ( element.value === value ) {
  243. element.classList.add( 'active' );
  244. // scroll into view
  245. var y = element.offsetTop - this.dom.offsetTop;
  246. var bottomY = y + element.offsetHeight;
  247. var minScroll = bottomY - this.dom.offsetHeight;
  248. if ( this.dom.scrollTop > y ) {
  249. this.dom.scrollTop = y;
  250. } else if ( this.dom.scrollTop < minScroll ) {
  251. this.dom.scrollTop = minScroll;
  252. }
  253. this.selectedIndex = i;
  254. } else {
  255. element.classList.remove( 'active' );
  256. }
  257. }
  258. this.selectedValue = value;
  259. return this;
  260. };
  261. var Points = function ( onAddClicked ) {
  262. UI.Element.call( this );
  263. var span = new UI.Span().setDisplay( 'inline-block' );
  264. this.pointsList = new UI.Div();
  265. span.add( this.pointsList );
  266. var row = new UI.Row();
  267. span.add( row );
  268. var addPointButton = new UI.Button( '+' ).onClick( onAddClicked );
  269. row.add( addPointButton );
  270. this.update = function () {
  271. if ( this.onChangeCallback !== null ) {
  272. this.onChangeCallback();
  273. }
  274. }.bind( this );
  275. this.dom = span.dom;
  276. this.pointsUI = [];
  277. this.lastPointIdx = 0;
  278. this.onChangeCallback = null;
  279. return this;
  280. };
  281. Points.prototype = Object.create( UI.Element.prototype );
  282. Points.prototype.constructor = Points;
  283. Points.prototype.onChange = function ( callback ) {
  284. this.onChangeCallback = callback;
  285. return this;
  286. };
  287. Points.prototype.clear = function () {
  288. for ( var i = 0; i < this.pointsUI.length; ++ i ) {
  289. if ( this.pointsUI[ i ] ) {
  290. this.deletePointRow( i, true );
  291. }
  292. }
  293. this.lastPointIdx = 0;
  294. };
  295. Points.prototype.deletePointRow = function ( idx, dontUpdate ) {
  296. if ( ! this.pointsUI[ idx ] ) return;
  297. this.pointsList.remove( this.pointsUI[ idx ].row );
  298. this.pointsUI[ idx ] = null;
  299. if ( dontUpdate !== true ) {
  300. this.update();
  301. }
  302. };
  303. UI.Points2 = function () {
  304. Points.call( this, UI.Points2.addRow.bind( this ) );
  305. return this;
  306. };
  307. UI.Points2.prototype = Object.create( Points.prototype );
  308. UI.Points2.prototype.constructor = UI.Points2;
  309. UI.Points2.addRow = function () {
  310. if ( this.pointsUI.length === 0 ) {
  311. this.pointsList.add( this.createPointRow( 0, 0 ) );
  312. } else {
  313. var point = this.pointsUI[ this.pointsUI.length - 1 ];
  314. this.pointsList.add( this.createPointRow( point.x.getValue(), point.y.getValue() ) );
  315. }
  316. this.update();
  317. };
  318. UI.Points2.prototype.getValue = function () {
  319. var points = [];
  320. var count = 0;
  321. for ( var i = 0; i < this.pointsUI.length; i ++ ) {
  322. var pointUI = this.pointsUI[ i ];
  323. if ( ! pointUI ) continue;
  324. points.push( new THREE.Vector2( pointUI.x.getValue(), pointUI.y.getValue() ) );
  325. ++ count;
  326. pointUI.lbl.setValue( count );
  327. }
  328. return points;
  329. };
  330. UI.Points2.prototype.setValue = function ( points ) {
  331. this.clear();
  332. for ( var i = 0; i < points.length; i ++ ) {
  333. var point = points[ i ];
  334. this.pointsList.add( this.createPointRow( point.x, point.y ) );
  335. }
  336. this.update();
  337. return this;
  338. };
  339. UI.Points2.prototype.createPointRow = function ( x, y ) {
  340. var pointRow = new UI.Div();
  341. var lbl = new UI.Text( this.lastPointIdx + 1 ).setWidth( '20px' );
  342. var txtX = new UI.Number( x ).setWidth( '30px' ).onChange( this.update );
  343. var txtY = new UI.Number( y ).setWidth( '30px' ).onChange( this.update );
  344. var idx = this.lastPointIdx;
  345. var scope = this;
  346. var btn = new UI.Button( '-' ).onClick( function () {
  347. if ( scope.isEditing ) return;
  348. scope.deletePointRow( idx );
  349. } );
  350. this.pointsUI.push( { row: pointRow, lbl: lbl, x: txtX, y: txtY } );
  351. ++ this.lastPointIdx;
  352. pointRow.add( lbl, txtX, txtY, btn );
  353. return pointRow;
  354. };
  355. UI.Points3 = function () {
  356. Points.call( this, UI.Points3.addRow.bind( this ) );
  357. return this;
  358. };
  359. UI.Points3.prototype = Object.create( Points.prototype );
  360. UI.Points3.prototype.constructor = UI.Points3;
  361. UI.Points3.addRow = function () {
  362. if ( this.pointsUI.length === 0 ) {
  363. this.pointsList.add( this.createPointRow( 0, 0, 0 ) );
  364. } else {
  365. var point = this.pointsUI[ this.pointsUI.length - 1 ];
  366. this.pointsList.add( this.createPointRow( point.x.getValue(), point.y.getValue(), point.z.getValue() ) );
  367. }
  368. this.update();
  369. };
  370. UI.Points3.prototype.getValue = function () {
  371. var points = [];
  372. var count = 0;
  373. for ( var i = 0; i < this.pointsUI.length; i ++ ) {
  374. var pointUI = this.pointsUI[ i ];
  375. if ( ! pointUI ) continue;
  376. points.push( new THREE.Vector3( pointUI.x.getValue(), pointUI.y.getValue(), pointUI.z.getValue() ) );
  377. ++ count;
  378. pointUI.lbl.setValue( count );
  379. }
  380. return points;
  381. };
  382. UI.Points3.prototype.setValue = function ( points ) {
  383. this.clear();
  384. for ( var i = 0; i < points.length; i ++ ) {
  385. var point = points[ i ];
  386. this.pointsList.add( this.createPointRow( point.x, point.y, point.z ) );
  387. }
  388. this.update();
  389. return this;
  390. };
  391. UI.Points3.prototype.createPointRow = function ( x, y, z ) {
  392. var pointRow = new UI.Div();
  393. var lbl = new UI.Text( this.lastPointIdx + 1 ).setWidth( '20px' );
  394. var txtX = new UI.Number( x ).setWidth( '30px' ).onChange( this.update );
  395. var txtY = new UI.Number( y ).setWidth( '30px' ).onChange( this.update );
  396. var txtZ = new UI.Number( z ).setWidth( '30px' ).onChange( this.update );
  397. var idx = this.lastPointIdx;
  398. var scope = this;
  399. var btn = new UI.Button( '-' ).onClick( function () {
  400. if ( scope.isEditing ) return;
  401. scope.deletePointRow( idx );
  402. } );
  403. this.pointsUI.push( { row: pointRow, lbl: lbl, x: txtX, y: txtY, z: txtZ } );
  404. ++ this.lastPointIdx;
  405. pointRow.add( lbl, txtX, txtY, txtZ, btn );
  406. return pointRow;
  407. };
  408. UI.THREE = {};
  409. UI.THREE.Boolean = function ( boolean, text ) {
  410. UI.Span.call( this );
  411. this.setMarginRight( '10px' );
  412. this.checkbox = new UI.Checkbox( boolean );
  413. this.text = new UI.Text( text ).setMarginLeft( '3px' );
  414. this.add( this.checkbox );
  415. this.add( this.text );
  416. };
  417. UI.THREE.Boolean.prototype = Object.create( UI.Span.prototype );
  418. UI.THREE.Boolean.prototype.constructor = UI.THREE.Boolean;
  419. UI.THREE.Boolean.prototype.getValue = function () {
  420. return this.checkbox.getValue();
  421. };
  422. UI.THREE.Boolean.prototype.setValue = function ( value ) {
  423. return this.checkbox.setValue( value );
  424. };