ui.three.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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, UISelect } 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 selectionRow = new UIRow();
  120. var cubeSideSelect = new UISelect().setOptions( {
  121. 0: 'Positive X',
  122. 1: 'Negative X',
  123. 2: 'Positive Y',
  124. 3: 'Negative Y',
  125. 4: 'Positive Z',
  126. 5: 'Negative Z'
  127. } ).setWidth( '150px' ).setFontSize( '12px' ).onChange( refreshUI );
  128. cubeSideSelect.setValue( 0 ); // default posX
  129. selectionRow.add( cubeSideSelect );
  130. var posXRow = new UIRow();
  131. var negXRow = new UIRow();
  132. var posYRow = new UIRow();
  133. var negYRow = new UIRow();
  134. var posZRow = new UIRow();
  135. var negZRow = new UIRow();
  136. var rows = [ posXRow, negXRow, posYRow, negYRow, posZRow, negZRow ];
  137. var posXTexture = new UITexture().onChange( onTextureChanged );
  138. var negXTexture = new UITexture().onChange( onTextureChanged );
  139. var posYTexture = new UITexture().onChange( onTextureChanged );
  140. var negYTexture = new UITexture().onChange( onTextureChanged );
  141. var posZTexture = new UITexture().onChange( onTextureChanged );
  142. var negZTexture = new UITexture().onChange( onTextureChanged );
  143. this.textures.push( posXTexture, negXTexture, posYTexture, negYTexture, posZTexture, negZTexture );
  144. posXRow.add( posXTexture );
  145. negXRow.add( negXTexture );
  146. posYRow.add( posYTexture );
  147. negYRow.add( negYTexture );
  148. posZRow.add( posZTexture );
  149. negZRow.add( negZTexture );
  150. container.add( selectionRow );
  151. container.add( ...rows );
  152. refreshUI();
  153. function refreshUI() {
  154. var currentSelection = cubeSideSelect.getValue();
  155. for ( var i = 0; i < rows.length; i ++ ) {
  156. rows[ i ].setDisplay( 'none' );
  157. }
  158. rows[ currentSelection ].setDisplay( '' );
  159. }
  160. function onTextureChanged() {
  161. var images = [];
  162. for ( var i = 0; i < scope.textures.length; i ++ ) {
  163. var texture = scope.textures[ i ].getValue();
  164. if ( texture !== null ) {
  165. images.push( texture.image );
  166. }
  167. }
  168. if ( images.length === 6 ) {
  169. var cubeTexture = new THREE.CubeTexture( images );
  170. cubeTexture.encoding = THREE.sRGBEncoding;
  171. cubeTexture.needsUpdate = true;
  172. scope.cubeTexture = cubeTexture;
  173. if ( scope.onChangeCallback ) scope.onChangeCallback( cubeTexture );
  174. }
  175. }
  176. };
  177. UICubeTexture.prototype = Object.create( UIElement.prototype );
  178. UICubeTexture.prototype.constructor = UICubeTexture;
  179. UICubeTexture.prototype.setEncoding = function ( encoding ) {
  180. var cubeTexture = this.getValue();
  181. if ( cubeTexture !== null ) {
  182. cubeTexture.encoding = encoding;
  183. }
  184. return this;
  185. };
  186. UICubeTexture.prototype.getValue = function () {
  187. return this.cubeTexture;
  188. };
  189. UICubeTexture.prototype.setValue = function ( cubeTexture ) {
  190. this.cubeTexture = cubeTexture;
  191. if ( cubeTexture !== null ) {
  192. var images = cubeTexture.image;
  193. if ( Array.isArray( images ) === true && images.length === 6 ) {
  194. for ( var i = 0; i < images.length; i ++ ) {
  195. var image = images[ i ];
  196. var texture = new THREE.Texture( image );
  197. this.textures[ i ].setValue( texture );
  198. }
  199. }
  200. } else {
  201. var textures = this.textures;
  202. for ( var i = 0; i < textures.length; i ++ ) {
  203. textures[ i ].setValue( null );
  204. }
  205. }
  206. return this;
  207. };
  208. UICubeTexture.prototype.onChange = function ( callback ) {
  209. this.onChangeCallback = callback;
  210. return this;
  211. };
  212. // UIOutliner
  213. var UIOutliner = function ( editor ) {
  214. UIElement.call( this );
  215. var scope = this;
  216. var dom = document.createElement( 'div' );
  217. dom.className = 'Outliner';
  218. dom.tabIndex = 0; // keyup event is ignored without setting tabIndex
  219. // hack
  220. this.scene = editor.scene;
  221. // Prevent native scroll behavior
  222. dom.addEventListener( 'keydown', function ( event ) {
  223. switch ( event.keyCode ) {
  224. case 38: // up
  225. case 40: // down
  226. event.preventDefault();
  227. event.stopPropagation();
  228. break;
  229. }
  230. }, false );
  231. // Keybindings to support arrow navigation
  232. dom.addEventListener( 'keyup', function ( event ) {
  233. switch ( event.keyCode ) {
  234. case 38: // up
  235. scope.selectIndex( scope.selectedIndex - 1 );
  236. break;
  237. case 40: // down
  238. scope.selectIndex( scope.selectedIndex + 1 );
  239. break;
  240. }
  241. }, false );
  242. this.dom = dom;
  243. this.editor = editor;
  244. this.options = [];
  245. this.selectedIndex = - 1;
  246. this.selectedValue = null;
  247. return this;
  248. };
  249. UIOutliner.prototype = Object.create( UIElement.prototype );
  250. UIOutliner.prototype.constructor = UIOutliner;
  251. UIOutliner.prototype.selectIndex = function ( index ) {
  252. if ( index >= 0 && index < this.options.length ) {
  253. this.setValue( this.options[ index ].value );
  254. var changeEvent = document.createEvent( 'HTMLEvents' );
  255. changeEvent.initEvent( 'change', true, true );
  256. this.dom.dispatchEvent( changeEvent );
  257. }
  258. };
  259. UIOutliner.prototype.setOptions = function ( options ) {
  260. var scope = this;
  261. while ( scope.dom.children.length > 0 ) {
  262. scope.dom.removeChild( scope.dom.firstChild );
  263. }
  264. function onClick() {
  265. scope.setValue( this.value );
  266. var changeEvent = document.createEvent( 'HTMLEvents' );
  267. changeEvent.initEvent( 'change', true, true );
  268. scope.dom.dispatchEvent( changeEvent );
  269. }
  270. // Drag
  271. var currentDrag;
  272. function onDrag() {
  273. currentDrag = this;
  274. }
  275. function onDragStart( event ) {
  276. event.dataTransfer.setData( 'text', 'foo' );
  277. }
  278. function onDragOver( event ) {
  279. if ( this === currentDrag ) return;
  280. var area = event.offsetY / this.clientHeight;
  281. if ( area < 0.25 ) {
  282. this.className = 'option dragTop';
  283. } else if ( area > 0.75 ) {
  284. this.className = 'option dragBottom';
  285. } else {
  286. this.className = 'option drag';
  287. }
  288. }
  289. function onDragLeave() {
  290. if ( this === currentDrag ) return;
  291. this.className = 'option';
  292. }
  293. function onDrop( event ) {
  294. if ( this === currentDrag ) return;
  295. this.className = 'option';
  296. var scene = scope.scene;
  297. var object = scene.getObjectById( currentDrag.value );
  298. var area = event.offsetY / this.clientHeight;
  299. if ( area < 0.25 ) {
  300. var nextObject = scene.getObjectById( this.value );
  301. moveObject( object, nextObject.parent, nextObject );
  302. } else if ( area > 0.75 ) {
  303. var nextObject = scene.getObjectById( this.nextSibling.value );
  304. moveObject( object, nextObject.parent, nextObject );
  305. } else {
  306. var parentObject = scene.getObjectById( this.value );
  307. moveObject( object, parentObject );
  308. }
  309. }
  310. function moveObject( object, newParent, nextObject ) {
  311. if ( nextObject === null ) nextObject = undefined;
  312. var newParentIsChild = false;
  313. object.traverse( function ( child ) {
  314. if ( child === newParent ) newParentIsChild = true;
  315. } );
  316. if ( newParentIsChild ) return;
  317. var editor = scope.editor;
  318. editor.execute( new MoveObjectCommand( editor, object, newParent, nextObject ) );
  319. var changeEvent = document.createEvent( 'HTMLEvents' );
  320. changeEvent.initEvent( 'change', true, true );
  321. scope.dom.dispatchEvent( changeEvent );
  322. }
  323. //
  324. scope.options = [];
  325. for ( var i = 0; i < options.length; i ++ ) {
  326. var div = options[ i ];
  327. div.className = 'option';
  328. scope.dom.appendChild( div );
  329. scope.options.push( div );
  330. div.addEventListener( 'click', onClick, false );
  331. if ( div.draggable === true ) {
  332. div.addEventListener( 'drag', onDrag, false );
  333. div.addEventListener( 'dragstart', onDragStart, false ); // Firefox needs this
  334. div.addEventListener( 'dragover', onDragOver, false );
  335. div.addEventListener( 'dragleave', onDragLeave, false );
  336. div.addEventListener( 'drop', onDrop, false );
  337. }
  338. }
  339. return scope;
  340. };
  341. UIOutliner.prototype.getValue = function () {
  342. return this.selectedValue;
  343. };
  344. UIOutliner.prototype.setValue = function ( value ) {
  345. for ( var i = 0; i < this.options.length; i ++ ) {
  346. var element = this.options[ i ];
  347. if ( element.value === value ) {
  348. element.classList.add( 'active' );
  349. // scroll into view
  350. var y = element.offsetTop - this.dom.offsetTop;
  351. var bottomY = y + element.offsetHeight;
  352. var minScroll = bottomY - this.dom.offsetHeight;
  353. if ( this.dom.scrollTop > y ) {
  354. this.dom.scrollTop = y;
  355. } else if ( this.dom.scrollTop < minScroll ) {
  356. this.dom.scrollTop = minScroll;
  357. }
  358. this.selectedIndex = i;
  359. } else {
  360. element.classList.remove( 'active' );
  361. }
  362. }
  363. this.selectedValue = value;
  364. return this;
  365. };
  366. var UIPoints = function ( onAddClicked ) {
  367. UIElement.call( this );
  368. var span = new UISpan().setDisplay( 'inline-block' );
  369. this.pointsList = new UIDiv();
  370. span.add( this.pointsList );
  371. var row = new UIRow();
  372. span.add( row );
  373. var addPointButton = new UIButton( '+' ).onClick( onAddClicked );
  374. row.add( addPointButton );
  375. this.update = function () {
  376. if ( this.onChangeCallback !== null ) {
  377. this.onChangeCallback();
  378. }
  379. }.bind( this );
  380. this.dom = span.dom;
  381. this.pointsUI = [];
  382. this.lastPointIdx = 0;
  383. this.onChangeCallback = null;
  384. return this;
  385. };
  386. UIPoints.prototype = Object.create( UIElement.prototype );
  387. UIPoints.prototype.constructor = UIPoints;
  388. UIPoints.prototype.onChange = function ( callback ) {
  389. this.onChangeCallback = callback;
  390. return this;
  391. };
  392. UIPoints.prototype.clear = function () {
  393. for ( var i = 0; i < this.pointsUI.length; ++ i ) {
  394. if ( this.pointsUI[ i ] ) {
  395. this.deletePointRow( i, true );
  396. }
  397. }
  398. this.lastPointIdx = 0;
  399. };
  400. UIPoints.prototype.deletePointRow = function ( idx, dontUpdate ) {
  401. if ( ! this.pointsUI[ idx ] ) return;
  402. this.pointsList.remove( this.pointsUI[ idx ].row );
  403. this.pointsUI[ idx ] = null;
  404. if ( dontUpdate !== true ) {
  405. this.update();
  406. }
  407. };
  408. var UIPoints2 = function () {
  409. UIPoints.call( this, UIPoints2.addRow.bind( this ) );
  410. return this;
  411. };
  412. UIPoints2.prototype = Object.create( UIPoints.prototype );
  413. UIPoints2.prototype.constructor = UIPoints2;
  414. UIPoints2.addRow = function () {
  415. if ( this.pointsUI.length === 0 ) {
  416. this.pointsList.add( this.createPointRow( 0, 0 ) );
  417. } else {
  418. var point = this.pointsUI[ this.pointsUI.length - 1 ];
  419. this.pointsList.add( this.createPointRow( point.x.getValue(), point.y.getValue() ) );
  420. }
  421. this.update();
  422. };
  423. UIPoints2.prototype.getValue = function () {
  424. var points = [];
  425. var count = 0;
  426. for ( var i = 0; i < this.pointsUI.length; i ++ ) {
  427. var pointUI = this.pointsUI[ i ];
  428. if ( ! pointUI ) continue;
  429. points.push( new THREE.Vector2( pointUI.x.getValue(), pointUI.y.getValue() ) );
  430. ++ count;
  431. pointUI.lbl.setValue( count );
  432. }
  433. return points;
  434. };
  435. UIPoints2.prototype.setValue = function ( points ) {
  436. this.clear();
  437. for ( var i = 0; i < points.length; i ++ ) {
  438. var point = points[ i ];
  439. this.pointsList.add( this.createPointRow( point.x, point.y ) );
  440. }
  441. this.update();
  442. return this;
  443. };
  444. UIPoints2.prototype.createPointRow = function ( x, y ) {
  445. var pointRow = new UIDiv();
  446. var lbl = new UIText( this.lastPointIdx + 1 ).setWidth( '20px' );
  447. var txtX = new UINumber( x ).setWidth( '30px' ).onChange( this.update );
  448. var txtY = new UINumber( y ).setWidth( '30px' ).onChange( this.update );
  449. var idx = this.lastPointIdx;
  450. var scope = this;
  451. var btn = new UIButton( '-' ).onClick( function () {
  452. if ( scope.isEditing ) return;
  453. scope.deletePointRow( idx );
  454. } );
  455. this.pointsUI.push( { row: pointRow, lbl: lbl, x: txtX, y: txtY } );
  456. ++ this.lastPointIdx;
  457. pointRow.add( lbl, txtX, txtY, btn );
  458. return pointRow;
  459. };
  460. var UIPoints3 = function () {
  461. UIPoints.call( this, UIPoints3.addRow.bind( this ) );
  462. return this;
  463. };
  464. UIPoints3.prototype = Object.create( UIPoints.prototype );
  465. UIPoints3.prototype.constructor = UIPoints3;
  466. UIPoints3.addRow = function () {
  467. if ( this.pointsUI.length === 0 ) {
  468. this.pointsList.add( this.createPointRow( 0, 0, 0 ) );
  469. } else {
  470. var point = this.pointsUI[ this.pointsUI.length - 1 ];
  471. this.pointsList.add( this.createPointRow( point.x.getValue(), point.y.getValue(), point.z.getValue() ) );
  472. }
  473. this.update();
  474. };
  475. UIPoints3.prototype.getValue = function () {
  476. var points = [];
  477. var count = 0;
  478. for ( var i = 0; i < this.pointsUI.length; i ++ ) {
  479. var pointUI = this.pointsUI[ i ];
  480. if ( ! pointUI ) continue;
  481. points.push( new THREE.Vector3( pointUI.x.getValue(), pointUI.y.getValue(), pointUI.z.getValue() ) );
  482. ++ count;
  483. pointUI.lbl.setValue( count );
  484. }
  485. return points;
  486. };
  487. UIPoints3.prototype.setValue = function ( points ) {
  488. this.clear();
  489. for ( var i = 0; i < points.length; i ++ ) {
  490. var point = points[ i ];
  491. this.pointsList.add( this.createPointRow( point.x, point.y, point.z ) );
  492. }
  493. this.update();
  494. return this;
  495. };
  496. UIPoints3.prototype.createPointRow = function ( x, y, z ) {
  497. var pointRow = new UIDiv();
  498. var lbl = new UIText( this.lastPointIdx + 1 ).setWidth( '20px' );
  499. var txtX = new UINumber( x ).setWidth( '30px' ).onChange( this.update );
  500. var txtY = new UINumber( y ).setWidth( '30px' ).onChange( this.update );
  501. var txtZ = new UINumber( z ).setWidth( '30px' ).onChange( this.update );
  502. var idx = this.lastPointIdx;
  503. var scope = this;
  504. var btn = new UIButton( '-' ).onClick( function () {
  505. if ( scope.isEditing ) return;
  506. scope.deletePointRow( idx );
  507. } );
  508. this.pointsUI.push( { row: pointRow, lbl: lbl, x: txtX, y: txtY, z: txtZ } );
  509. ++ this.lastPointIdx;
  510. pointRow.add( lbl, txtX, txtY, txtZ, btn );
  511. return pointRow;
  512. };
  513. var UIBoolean = function ( boolean, text ) {
  514. UISpan.call( this );
  515. this.setMarginRight( '10px' );
  516. this.checkbox = new UICheckbox( boolean );
  517. this.text = new UIText( text ).setMarginLeft( '3px' );
  518. this.add( this.checkbox );
  519. this.add( this.text );
  520. };
  521. UIBoolean.prototype = Object.create( UISpan.prototype );
  522. UIBoolean.prototype.constructor = UIBoolean;
  523. UIBoolean.prototype.getValue = function () {
  524. return this.checkbox.getValue();
  525. };
  526. UIBoolean.prototype.setValue = function ( value ) {
  527. return this.checkbox.setValue( value );
  528. };
  529. export { UITexture, UICubeTexture, UIOutliner, UIPoints, UIPoints2, UIPoints3, UIBoolean };