ui.three.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 input = document.createElement( 'input' );
  9. input.type = 'file';
  10. input.addEventListener( 'change', function ( event ) {
  11. loadFile( event.target.files[ 0 ] );
  12. } );
  13. var canvas = document.createElement( 'canvas' );
  14. canvas.width = 32;
  15. canvas.height = 16;
  16. canvas.style.cursor = 'pointer';
  17. canvas.style.marginRight = '5px';
  18. canvas.style.border = '1px solid #888';
  19. canvas.addEventListener( 'click', function ( event ) {
  20. input.click();
  21. }, false );
  22. canvas.addEventListener( 'drop', function ( event ) {
  23. event.preventDefault();
  24. event.stopPropagation();
  25. loadFile( event.dataTransfer.files[ 0 ] );
  26. }, false );
  27. dom.appendChild( canvas );
  28. var name = document.createElement( 'input' );
  29. name.disabled = true;
  30. name.style.width = '64px';
  31. name.style.border = '1px solid #ccc';
  32. dom.appendChild( name );
  33. var loadFile = function ( file ) {
  34. if ( file.type.match( 'image.*' ) ) {
  35. var reader = new FileReader();
  36. reader.addEventListener( 'load', function ( event ) {
  37. var image = document.createElement( 'img' );
  38. image.addEventListener( 'load', function( event ) {
  39. var texture = new THREE.Texture( this, mapping );
  40. texture.sourceFile = file.name;
  41. texture.needsUpdate = true;
  42. scope.setValue( texture );
  43. if ( scope.onChangeCallback ) scope.onChangeCallback();
  44. }, false );
  45. image.src = event.target.result;
  46. }, false );
  47. reader.readAsDataURL( file );
  48. }
  49. };
  50. this.dom = dom;
  51. this.texture = null;
  52. this.onChangeCallback = null;
  53. return this;
  54. };
  55. UI.Texture.prototype = Object.create( UI.Element.prototype );
  56. UI.Texture.prototype.constructor = UI.Texture;
  57. UI.Texture.prototype.getValue = function () {
  58. return this.texture;
  59. };
  60. UI.Texture.prototype.setValue = function ( texture ) {
  61. var canvas = this.dom.children[ 0 ];
  62. var name = this.dom.children[ 1 ];
  63. var context = canvas.getContext( '2d' );
  64. if ( texture !== null ) {
  65. var image = texture.image;
  66. if ( image !== undefined && image.width > 0 ) {
  67. name.value = texture.sourceFile;
  68. var scale = canvas.width / image.width;
  69. context.drawImage( image, 0, 0, image.width * scale, image.height * scale );
  70. } else {
  71. name.value = texture.sourceFile + ' (error)';
  72. context.clearRect( 0, 0, canvas.width, canvas.height );
  73. }
  74. } else {
  75. name.value = '';
  76. context.clearRect( 0, 0, canvas.width, canvas.height );
  77. }
  78. this.texture = texture;
  79. };
  80. UI.Texture.prototype.onChange = function ( callback ) {
  81. this.onChangeCallback = callback;
  82. return this;
  83. };
  84. // Outliner
  85. UI.Outliner = function ( editor ) {
  86. UI.Element.call( this );
  87. var scope = this;
  88. var dom = document.createElement( 'div' );
  89. dom.className = 'Outliner';
  90. dom.tabIndex = 0; // keyup event is ignored without setting tabIndex
  91. // hack
  92. this.scene = editor.scene;
  93. // Prevent native scroll behavior
  94. dom.addEventListener( 'keydown', function ( event ) {
  95. switch ( event.keyCode ) {
  96. case 38: // up
  97. case 40: // down
  98. event.preventDefault();
  99. event.stopPropagation();
  100. break;
  101. }
  102. }, false );
  103. // Keybindings to support arrow navigation
  104. dom.addEventListener( 'keyup', function ( event ) {
  105. switch ( event.keyCode ) {
  106. case 38: // up
  107. scope.selectIndex( scope.selectedIndex - 1 );
  108. break;
  109. case 40: // down
  110. scope.selectIndex( scope.selectedIndex + 1 );
  111. break;
  112. }
  113. }, false );
  114. this.dom = dom;
  115. this.options = [];
  116. this.selectedIndex = - 1;
  117. this.selectedValue = null;
  118. return this;
  119. };
  120. UI.Outliner.prototype = Object.create( UI.Element.prototype );
  121. UI.Outliner.prototype.constructor = UI.Outliner;
  122. UI.Outliner.prototype.selectIndex = function ( index ) {
  123. if ( index >= 0 && index < this.options.length ) {
  124. this.setValue( this.options[ index ].value );
  125. var changeEvent = document.createEvent( 'HTMLEvents' );
  126. changeEvent.initEvent( 'change', true, true );
  127. this.dom.dispatchEvent( changeEvent );
  128. }
  129. };
  130. UI.Outliner.prototype.setOptions = function ( options ) {
  131. var scope = this;
  132. while ( scope.dom.children.length > 0 ) {
  133. scope.dom.removeChild( scope.dom.firstChild );
  134. }
  135. function onClick() {
  136. scope.setValue( this.value );
  137. var changeEvent = document.createEvent( 'HTMLEvents' );
  138. changeEvent.initEvent( 'change', true, true );
  139. scope.dom.dispatchEvent( changeEvent );
  140. }
  141. // Drag
  142. var currentDrag;
  143. function onDrag( event ) {
  144. currentDrag = this;
  145. }
  146. function onDragStart( event ) {
  147. event.dataTransfer.setData( 'text', 'foo' );
  148. }
  149. function onDragOver( event ) {
  150. if ( this === currentDrag ) return;
  151. var area = event.offsetY / this.clientHeight;
  152. if ( area < 0.25 ) {
  153. this.className = 'option dragTop';
  154. } else if ( area > 0.75 ) {
  155. this.className = 'option dragBottom';
  156. } else {
  157. this.className = 'option drag';
  158. }
  159. }
  160. function onDragLeave() {
  161. if ( this === currentDrag ) return;
  162. this.className = 'option';
  163. }
  164. function onDrop( event ) {
  165. if ( this === currentDrag ) return;
  166. this.className = 'option';
  167. var scene = scope.scene;
  168. var object = scene.getObjectById( currentDrag.value );
  169. var area = event.offsetY / this.clientHeight;
  170. if ( area < 0.25 ) {
  171. var nextObject = scene.getObjectById( this.value );
  172. moveObject( object, nextObject.parent, nextObject );
  173. } else if ( area > 0.75 ) {
  174. var nextObject = scene.getObjectById( this.nextSibling.value );
  175. moveObject( object, nextObject.parent, nextObject );
  176. } else {
  177. var parentObject = scene.getObjectById( this.value );
  178. moveObject( object, parentObject );
  179. }
  180. }
  181. function moveObject( object, newParent, nextObject ) {
  182. if ( nextObject === null ) nextObject = undefined;
  183. var newParentIsChild = false;
  184. object.traverse( function ( child ) {
  185. if ( child === newParent ) newParentIsChild = true;
  186. } );
  187. if ( newParentIsChild ) return;
  188. editor.execute( new MoveObjectCommand( object, newParent, nextObject ) );
  189. var changeEvent = document.createEvent( 'HTMLEvents' );
  190. changeEvent.initEvent( 'change', true, true );
  191. scope.dom.dispatchEvent( changeEvent );
  192. }
  193. //
  194. scope.options = [];
  195. for ( var i = 0; i < options.length; i ++ ) {
  196. var option = options[ i ];
  197. var div = document.createElement( 'div' );
  198. div.className = 'option';
  199. div.innerHTML = option.html;
  200. div.value = option.value;
  201. scope.dom.appendChild( div );
  202. scope.options.push( div );
  203. div.addEventListener( 'click', onClick, false );
  204. if ( option.static !== true ) {
  205. div.draggable = true;
  206. div.addEventListener( 'drag', onDrag, false );
  207. div.addEventListener( 'dragstart', onDragStart, false ); // Firefox needs this
  208. div.addEventListener( 'dragover', onDragOver, false );
  209. div.addEventListener( 'dragleave', onDragLeave, false );
  210. div.addEventListener( 'drop', onDrop, false );
  211. }
  212. }
  213. return scope;
  214. };
  215. UI.Outliner.prototype.getValue = function () {
  216. return this.selectedValue;
  217. };
  218. UI.Outliner.prototype.setValue = function ( value ) {
  219. for ( var i = 0; i < this.options.length; i ++ ) {
  220. var element = this.options[ i ];
  221. if ( element.value === value ) {
  222. element.classList.add( 'active' );
  223. // scroll into view
  224. var y = element.offsetTop - this.dom.offsetTop;
  225. var bottomY = y + element.offsetHeight;
  226. var minScroll = bottomY - this.dom.offsetHeight;
  227. if ( this.dom.scrollTop > y ) {
  228. this.dom.scrollTop = y;
  229. } else if ( this.dom.scrollTop < minScroll ) {
  230. this.dom.scrollTop = minScroll;
  231. }
  232. this.selectedIndex = i;
  233. } else {
  234. element.classList.remove( 'active' );
  235. }
  236. }
  237. this.selectedValue = value;
  238. return this;
  239. };
  240. UI.THREE = {};
  241. UI.THREE.Boolean = function ( boolean, text ) {
  242. UI.Span.call( this );
  243. this.setMarginRight( '10px' );
  244. this.checkbox = new UI.Checkbox( boolean );
  245. this.text = new UI.Text( text ).setMarginLeft( '3px' );
  246. this.add( this.checkbox );
  247. this.add( this.text );
  248. };
  249. UI.THREE.Boolean.prototype = Object.create( UI.Span.prototype );
  250. UI.THREE.Boolean.prototype.constructor = UI.THREE.Boolean;
  251. UI.THREE.Boolean.prototype.getValue = function () {
  252. return this.checkbox.getValue();
  253. };
  254. UI.THREE.Boolean.prototype.setValue = function ( value ) {
  255. return this.checkbox.setValue( value );
  256. };