Sidebar.Attributes.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. Sidebar.Attributes = function ( signals ) {
  2. var scope = this;
  3. var object;
  4. var param = {};
  5. var primaryParams = [
  6. 'name',
  7. 'parent',
  8. 'geometry',
  9. 'material',
  10. 'position',
  11. 'rotation',
  12. 'scale',
  13. 'width',
  14. 'height',
  15. 'depth',
  16. 'widthSegments',
  17. 'heightSegments',
  18. 'depthSegments',
  19. 'radialSegments',
  20. 'tubularSegments',
  21. 'radius',
  22. 'radiusTop',
  23. 'radiusBottom',
  24. 'phiStart',
  25. 'phiLength',
  26. 'thetaStart',
  27. 'thetaLength',
  28. 'tube',
  29. 'arc',
  30. 'detail',
  31. 'p',
  32. 'q',
  33. 'heightScale',
  34. 'openEnded',
  35. 'color',
  36. 'groundColor',
  37. 'ambient',
  38. 'emissive',
  39. 'specular',
  40. 'reflectivity',
  41. 'shininess',
  42. 'intensity',
  43. 'opacity',
  44. 'transparent',
  45. 'metal',
  46. 'wireframe',
  47. 'visible',
  48. 'userData'
  49. ];
  50. var secondaryParams = [
  51. 'castShadow',
  52. 'receiveShadow',
  53. 'useQuaternion',
  54. 'fog',
  55. 'depthTest',
  56. 'depthWrite',
  57. 'dynamic'
  58. ];
  59. var integerParams = [
  60. 'widthSegments',
  61. 'heightSegments',
  62. 'depthSegments',
  63. 'radialSegments',
  64. 'tubularSegments'
  65. ];
  66. var container = new UI.Panel();
  67. var group1 = new UI.Panel().setBorderTop( '1px solid #ccc' ).setPadding( '10px' );
  68. var group2 = new UI.Panel().setBorderTop( '1px solid #ccc' ).setPadding( '10px' ).setOpacity( 0.5 );
  69. var group3 = new UI.Panel().setBorderTop( '1px solid #ccc' ).setPadding( '10px' ).setOpacity( 0.25 );//.setDisplay( 'none' );
  70. container.add( group1, group2, group3 );
  71. signals.objectChanged.add( function ( changed ) {
  72. if ( object === changed ) updateUI();
  73. } );
  74. signals.selected.add( function ( selected ) {
  75. var selected = editor.listSelected();
  76. object = ( selected.length ) ? selected[0] : null;
  77. createUI();
  78. updateUI();
  79. } );
  80. function createUI() {
  81. param = {};
  82. while ( group1.dom.hasChildNodes() ) group1.dom.removeChild( group1.dom.lastChild );
  83. while ( group2.dom.hasChildNodes() ) group2.dom.removeChild( group2.dom.lastChild );
  84. while ( group3.dom.hasChildNodes() ) group3.dom.removeChild( group3.dom.lastChild );
  85. if ( object ) {
  86. for ( var i in primaryParams ) addElement( primaryParams[i], group1 );
  87. for ( var i in secondaryParams ) addElement( secondaryParams[i], group2 );
  88. for ( var key in object ) addElement( key, group3 );
  89. }
  90. }
  91. function addElement( key, parent ) {
  92. if ( object[ key ] !== undefined && param[ key ] === undefined ) {
  93. if ( typeof object[ key ] === 'string' ) {
  94. param[ key ] = new UI.ParamString( key ).onChange( updateParam );
  95. parent.add( param[ key ] );
  96. } else if ( typeof object[ key ] === 'boolean' ) {
  97. param[ key ] = new UI.ParamBool( key ).onChange( updateParam );
  98. parent.add( param[ key ] );
  99. } else if ( typeof object[ key ] === 'number' ) {
  100. if ( integerParams.indexOf( key ) != -1 )
  101. param[ key ] = new UI.ParamInteger( key ).onChange( updateParam );
  102. else
  103. param[ key ] = new UI.ParamFloat( key ).onChange( updateParam );
  104. parent.add( param[ key ] );
  105. } else if ( object[ key ] instanceof THREE.Vector3 ) {
  106. var hasLock = ( key === 'scale' ) ? true : false;
  107. param[ key ] = new UI.ParamVector3( key, hasLock ).onChange( updateParam );
  108. parent.add( param[ key ] );
  109. } else if ( object[ key ] instanceof THREE.Color ) {
  110. param[ key ] = new UI.ParamColor( key ).onChange( updateParam );
  111. parent.add( param[ key ] );
  112. } else if ( key === 'parent' ) {
  113. param[ key ] = new UI.ParamSelect( key ).onChange( updateParam );
  114. param[ key ].name.setColor( '#0080f0' ).onClick( function(){ editor.select( editor.objects[ param[ key ].getValue() ] ) } );
  115. parent.add( param[ key ] );
  116. } else if ( key === 'geometry' ) {
  117. param[ key ] = new UI.ParamSelect( key ).onChange( updateParam );
  118. param[ key ].name.setColor( '#0080f0' ).onClick( function(){ editor.select( editor.geometries[ param[ key ].getValue() ] ) } );
  119. parent.add( param[ key ] );
  120. } else if ( key == 'material' ) {
  121. param[ key ] = new UI.ParamSelect( key ).onChange( updateParam );
  122. param[ key ].name.setColor( '#0080f0' ).onClick( function(){ editor.select( editor.materials[ param[ key ].getValue() ] ) } );
  123. parent.add( param[ key ] );
  124. } else if ( key == 'userData' ) {
  125. param[ key ] = new UI.ParamJson( key ).onChange( updateParam );
  126. parent.add( param[ key ] );
  127. }
  128. }
  129. }
  130. function updateUI() {
  131. if ( object ) {
  132. for ( var key in object ) {
  133. if ( typeof object[ key ] === 'string' ) param[ key ].setValue( object[ key ] );
  134. else if ( typeof object[ key ] === 'boolean' ) param[ key ].setValue( object[ key ] );
  135. else if ( typeof object[ key ] === 'number' ) param[ key ].setValue( object[ key ] );
  136. else if ( object[ key ] instanceof THREE.Vector3 ) param[ key ].setValue( object[ key ] );
  137. else if ( object[ key ] instanceof THREE.Color ) param[ key ].setValue( object[ key ] );
  138. else if ( object[ key ] && key === 'parent' ) {
  139. var options = {};
  140. for ( var uuid in editor.objects ) if ( object.uuid != uuid ) options[ uuid ] = editor.objects[ uuid ].name;
  141. param[ key ].setOptions( options );
  142. if ( object.parent !== undefined ) param[ key ].setValue( object.parent.uuid );
  143. } else if ( object[ key ] && key === 'geometry' ) {
  144. var options = {};
  145. for ( var uuid in editor.geometries ) if ( object.uuid != uuid ) options[ uuid ] = editor.geometries[ uuid ].name;
  146. param[ key ].setOptions( options );
  147. if ( object.geometry !== undefined ) param[ key ].setValue( object.geometry.uuid );
  148. } else if ( object[ key ] && key === 'material' ) {
  149. var options = {};
  150. for ( var uuid in editor.materials ) if ( object.uuid != uuid ) options[ uuid ] = editor.materials[ uuid ].name;
  151. param[ key ].setOptions( options );
  152. if ( object.material !== undefined ) param[ key ].setValue( object.material.uuid );
  153. } else if ( key == 'userData' ) {
  154. try {
  155. param[ key ].setValue( JSON.stringify( object.userData, null, ' ' ) );
  156. } catch ( error ) {
  157. console.log( error );
  158. }
  159. }
  160. }
  161. }
  162. }
  163. function updateParam( event ) {
  164. var key = event.srcElement.name;
  165. if ( typeof object[ key ] === 'string' ) object[ key ] = param[ key ].getValue();
  166. else if ( typeof object[ key ] === 'boolean' ) object[ key ] = param[ key ].getValue();
  167. else if ( typeof object[ key ] === 'number' ) object[ key ] = param[ key ].getValue();
  168. else if ( object[ key ] instanceof THREE.Color ) object[ key ].setHex( param[ key ].getValue() );
  169. else if ( object[ key ] instanceof THREE.Vector3 ) object[ key ].copy( param[ key ].getValue() );
  170. else if ( key === 'parent' ) {
  171. if ( param[ key ].getValue() != object.uuid )
  172. editor.parent( object, editor.objects[ param[ key ].getValue() ] );
  173. } else if ( key === 'geometry' ) {
  174. if ( param[ key ].getValue() != object.geometry.uuid )
  175. object.geometry = editor.geometries[ param[ key ].getValue() ];
  176. } else if ( key === 'material' ) {
  177. if ( param[ key ].getValue() != object.material.uuid )
  178. object.material = editor.materials[ param[ key ].getValue() ];
  179. } else if ( key === 'userData' ) {
  180. try {
  181. object.userData = JSON.parse( param[ key ].getValue() );
  182. } catch ( error ) {
  183. console.log( error );
  184. }
  185. }
  186. if ( object instanceof THREE.Object3D ) {
  187. signals.objectChanged.dispatch( object );
  188. } else if ( object instanceof THREE.Geometry ) {
  189. var geoParams = {};
  190. for ( var i in param ) geoParams[ i ] = param[ i ].getValue();
  191. editor.updateGeometry( object, geoParams );
  192. } else if ( object instanceof THREE.Material ) {
  193. signals.materialChanged.dispatch( object );
  194. }
  195. signals.sceneChanged.dispatch( editor.scene );
  196. }
  197. return container;
  198. }