ui.three.js 16 KB

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