ui.three.js 14 KB

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