ui.three.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. var scene = editor.scene;
  92. var sortable = Sortable.create( dom, {
  93. draggable: '.draggable',
  94. onUpdate: function ( event ) {
  95. var item = event.item;
  96. var object = scene.getObjectById( item.value );
  97. var prevObject = scene.getObjectById( item.previousSibling.value );
  98. editor.parent( object, prevObject.parent, prevObject );
  99. }
  100. } );
  101. // Broadcast for object selection after arrow navigation
  102. var changeEvent = document.createEvent('HTMLEvents');
  103. changeEvent.initEvent( 'change', true, true );
  104. // Prevent native scroll behavior
  105. dom.addEventListener( 'keydown', function (event) {
  106. switch ( event.keyCode ) {
  107. case 38: // up
  108. case 40: // down
  109. event.preventDefault();
  110. event.stopPropagation();
  111. break;
  112. }
  113. }, false);
  114. // Keybindings to support arrow navigation
  115. dom.addEventListener( 'keyup', function (event) {
  116. switch ( event.keyCode ) {
  117. case 38: // up
  118. case 40: // down
  119. scope.selectedIndex += ( event.keyCode == 38 ) ? -1 : 1;
  120. if ( scope.selectedIndex >= 0 && scope.selectedIndex < scope.options.length ) {
  121. // Highlight selected dom elem and scroll parent if needed
  122. scope.setValue( scope.options[ scope.selectedIndex ].value );
  123. scope.dom.dispatchEvent( changeEvent );
  124. }
  125. break;
  126. }
  127. }, false);
  128. this.dom = dom;
  129. this.options = [];
  130. this.selectedIndex = -1;
  131. this.selectedValue = null;
  132. return this;
  133. };
  134. UI.Outliner.prototype = Object.create( UI.Element.prototype );
  135. UI.Outliner.prototype.constructor = UI.Outliner;
  136. UI.Outliner.prototype.setOptions = function ( options ) {
  137. var scope = this;
  138. var changeEvent = document.createEvent( 'HTMLEvents' );
  139. changeEvent.initEvent( 'change', true, true );
  140. while ( scope.dom.children.length > 0 ) {
  141. scope.dom.removeChild( scope.dom.firstChild );
  142. }
  143. scope.options = [];
  144. for ( var i = 0; i < options.length; i ++ ) {
  145. var option = options[ i ];
  146. var div = document.createElement( 'div' );
  147. div.className = 'option ' + ( option.static === true ? '': 'draggable' );
  148. div.innerHTML = option.html;
  149. div.value = option.value;
  150. scope.dom.appendChild( div );
  151. scope.options.push( div );
  152. div.addEventListener( 'click', function ( event ) {
  153. scope.setValue( this.value );
  154. scope.dom.dispatchEvent( changeEvent );
  155. }, false );
  156. }
  157. return scope;
  158. };
  159. UI.Outliner.prototype.getValue = function () {
  160. return this.selectedValue;
  161. };
  162. UI.Outliner.prototype.setValue = function ( value ) {
  163. for ( var i = 0; i < this.options.length; i ++ ) {
  164. var element = this.options[ i ];
  165. if ( element.value === value ) {
  166. element.classList.add( 'active' );
  167. // scroll into view
  168. var y = element.offsetTop - this.dom.offsetTop;
  169. var bottomY = y + element.offsetHeight;
  170. var minScroll = bottomY - this.dom.offsetHeight;
  171. if ( this.dom.scrollTop > y ) {
  172. this.dom.scrollTop = y
  173. } else if ( this.dom.scrollTop < minScroll ) {
  174. this.dom.scrollTop = minScroll;
  175. }
  176. this.selectedIndex = i;
  177. } else {
  178. element.classList.remove( 'active' );
  179. }
  180. }
  181. this.selectedValue = value;
  182. return this;
  183. };