You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
6.8 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.Collections.Generic;
  6. namespace AmplifyShaderEditor
  7. {
  8. public sealed class ToolsMenuButton : ToolsMenuButtonParent
  9. {
  10. public delegate void ToolButtonPressed( ToolButtonType type );
  11. public event ToolButtonPressed ToolButtonPressedEvt;
  12. private Rect m_buttonArea;
  13. private List<Texture2D> m_buttonTexture;
  14. private string m_buttonTexturePath;
  15. private ToolButtonType m_buttonType;
  16. private GUIStyle m_style;
  17. private bool m_enabled = true;
  18. private bool m_drawOnFunction = true;
  19. private List<string> m_cachedStates;
  20. private int m_bufferedState = -1;
  21. private string m_bufferedTooltip = string.Empty;
  22. public ToolsMenuButton( AmplifyShaderEditorWindow parentWindow, ToolButtonType type, float x, float y, float width, float height, string texturePath, string text, string tooltip, float buttonSpacing = -1, bool drawOnFunction = true ) : base( parentWindow, text, tooltip, buttonSpacing )
  23. {
  24. m_buttonArea = new Rect( x, y, width, height );
  25. m_buttonType = type;
  26. m_buttonTexturePath = texturePath;
  27. m_cachedStates = new List<string>();
  28. m_drawOnFunction = drawOnFunction;
  29. }
  30. public void AddState( string state )
  31. {
  32. m_cachedStates.Add( state );
  33. }
  34. public override void Destroy()
  35. {
  36. ToolButtonPressedEvt = null;
  37. if ( m_buttonTexture != null )
  38. {
  39. for ( int i = 0; i < m_buttonTexture.Count; i++ )
  40. {
  41. Resources.UnloadAsset( m_buttonTexture[ i ] );
  42. }
  43. m_buttonTexture.Clear();
  44. }
  45. m_buttonTexture = null;
  46. }
  47. protected override void Init()
  48. {
  49. base.Init();
  50. if ( m_buttonTexture == null )
  51. {
  52. m_buttonTexturePath = AssetDatabase.GUIDToAssetPath( m_buttonTexturePath );
  53. m_buttonTexture = new List<Texture2D>();
  54. m_buttonTexture.Add( AssetDatabase.LoadAssetAtPath( m_buttonTexturePath, typeof( Texture2D ) ) as Texture2D );
  55. }
  56. if ( m_cachedStates.Count > 0 )
  57. {
  58. for ( int i = 0; i < m_cachedStates.Count; i++ )
  59. {
  60. m_cachedStates[ i ] = AssetDatabase.GUIDToAssetPath( m_cachedStates[ i ] );
  61. m_buttonTexture.Add( AssetDatabase.LoadAssetAtPath( m_cachedStates[ i ], typeof( Texture2D ) ) as Texture2D );
  62. }
  63. m_cachedStates.Clear();
  64. }
  65. if ( m_style == null )
  66. {
  67. m_style = new GUIStyle( UIUtils.Button );
  68. m_style.normal.background = m_buttonTexture[ 0 ];
  69. m_style.hover.background = m_buttonTexture[ 0 ];
  70. m_style.hover.textColor = m_style.normal.textColor;
  71. m_style.active.background = m_buttonTexture[ 0 ];
  72. m_style.active.textColor = m_style.normal.textColor;
  73. m_style.onNormal.background = m_buttonTexture[ 0 ];
  74. m_style.onNormal.textColor = m_style.normal.textColor;
  75. m_style.onHover.background = m_buttonTexture[ 0 ];
  76. m_style.onHover.textColor = m_style.normal.textColor;
  77. m_style.onActive.background = m_buttonTexture[ 0 ];
  78. m_style.onActive.textColor = m_style.normal.textColor;
  79. m_style.clipping = TextClipping.Overflow;
  80. m_style.fontStyle = FontStyle.Bold;
  81. m_style.alignment = TextAnchor.LowerCenter;
  82. m_style.contentOffset = new Vector2( 0, 15 );
  83. m_style.fontSize = 10;
  84. bool resizeFromTexture = false;
  85. if ( m_buttonArea.width > 0 )
  86. {
  87. m_style.fixedWidth = m_buttonArea.width;
  88. }
  89. else
  90. {
  91. resizeFromTexture = true;
  92. }
  93. if ( m_buttonArea.height > 0 )
  94. {
  95. m_style.fixedHeight = m_buttonArea.height;
  96. }
  97. else
  98. {
  99. resizeFromTexture = true;
  100. }
  101. if ( resizeFromTexture )
  102. {
  103. m_buttonArea.width = m_style.fixedWidth = m_buttonTexture[ 0 ].width;
  104. m_buttonArea.height = m_style.fixedHeight = m_buttonTexture[ 0 ].height;
  105. }
  106. }
  107. }
  108. public override void Draw()
  109. {
  110. base.Draw();
  111. bool guiEnabledBuffer = GUI.enabled;
  112. GUI.enabled = m_enabled;
  113. if ( GUILayout.Button( m_content, m_style ) && ToolButtonPressedEvt != null )
  114. {
  115. ToolButtonPressedEvt( m_buttonType );
  116. }
  117. GUI.enabled = guiEnabledBuffer;
  118. }
  119. public override void Draw( float x, float y )
  120. {
  121. if ( !(m_parentWindow.CameraDrawInfo.CurrentEventType == EventType.MouseDown || m_parentWindow.CameraDrawInfo.CurrentEventType == EventType.Repaint ) )
  122. return;
  123. if ( m_parentWindow.CurrentGraph.CurrentMasterNode == null && !m_drawOnFunction)
  124. return;
  125. base.Draw( x, y );
  126. if ( m_bufferedState > -1 )
  127. {
  128. if ( string.IsNullOrEmpty( m_bufferedTooltip ) )
  129. {
  130. SetStateOnButton( m_bufferedState );
  131. }
  132. else
  133. {
  134. SetStateOnButton( m_bufferedState, m_bufferedTooltip );
  135. }
  136. m_bufferedState = -1;
  137. m_bufferedTooltip = string.Empty;
  138. }
  139. m_buttonArea.x = x;
  140. m_buttonArea.y = y;
  141. if ( m_parentWindow.CameraDrawInfo.CurrentEventType == EventType.MouseDown && m_buttonArea.Contains( m_parentWindow.CameraDrawInfo.MousePosition ) && ToolButtonPressedEvt != null )
  142. {
  143. ToolButtonPressedEvt( m_buttonType );
  144. Event.current.Use();
  145. m_parentWindow.CameraDrawInfo.CurrentEventType = EventType.Used;
  146. }
  147. else if ( m_parentWindow.CameraDrawInfo.CurrentEventType == EventType.Repaint )
  148. {
  149. GUI.Label( m_buttonArea, m_content, m_style );
  150. }
  151. //if ( GUI.Button( m_buttonArea, m_content, m_style ) && ToolButtonPressedEvt != null )
  152. //{
  153. // ToolButtonPressedEvt( m_buttonType );
  154. //}
  155. }
  156. public override void Draw( Vector2 pos )
  157. {
  158. Draw( pos.x, pos.y );
  159. }
  160. public override void SetStateOnButton( int state, string tooltip )
  161. {
  162. if ( m_buttonTexture == null || m_style == null )
  163. {
  164. m_bufferedState = state;
  165. m_bufferedTooltip = tooltip;
  166. return;
  167. }
  168. if ( state < 0 || state >= m_buttonTexture.Count )
  169. {
  170. return;
  171. }
  172. base.SetStateOnButton( state, tooltip );
  173. m_style.normal.background = m_buttonTexture[ state ];
  174. m_style.hover.background = m_buttonTexture[ state ];
  175. m_style.active.background = m_buttonTexture[ state ];
  176. m_style.onNormal.background = m_buttonTexture[ state ];
  177. m_style.onHover.background = m_buttonTexture[ state ];
  178. m_style.onActive.background = m_buttonTexture[ state ];
  179. }
  180. public override void SetStateOnButton( int state )
  181. {
  182. if ( m_buttonTexture == null || m_style == null )
  183. {
  184. m_bufferedState = state;
  185. return;
  186. }
  187. if ( state < 0 || state >= m_buttonTexture.Count )
  188. {
  189. return;
  190. }
  191. base.SetStateOnButton( state );
  192. m_style.normal.background = m_buttonTexture[ state ];
  193. m_style.hover.background = m_buttonTexture[ state ];
  194. m_style.active.background = m_buttonTexture[ state ];
  195. m_style.onNormal.background = m_buttonTexture[ state ];
  196. m_style.onHover.background = m_buttonTexture[ state ];
  197. m_style.onActive.background = m_buttonTexture[ state ];
  198. }
  199. public bool IsInside( Vector2 pos )
  200. {
  201. return m_buttonArea.Contains( pos );
  202. }
  203. public bool Enabled
  204. {
  205. get { return m_enabled; }
  206. set { m_enabled = value; }
  207. }
  208. }
  209. }