2
0

ui.three.js 14 KB

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