ui.three.js 14 KB

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