ShaderEditorModeWindow.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace AmplifyShaderEditor
  6. {
  7. public sealed class ShaderEditorModeWindow : MenuParent
  8. {
  9. private static readonly Color OverallColorOn = new Color( 1f, 1f, 1f, 0.9f );
  10. private static readonly Color OverallColorOff = new Color( 1f, 1f, 1f, 0.3f );
  11. private static readonly Color FontColorOff = new Color( 1f, 1f, 1f, 0.4f );
  12. private const float DeltaY = 15;
  13. private const float DeltaX = 10;
  14. private const float CollSizeX = 180;
  15. private const float CollSizeY = 70;
  16. //private static string MatFormat = "<size=20>MATERIAL</size>\n{0}";
  17. //private static string ShaderFormat = "<size=20>SHADER</size>\n{0}";
  18. //private const string CurrMatStr = "MATERIAL";
  19. //private const string CurrShaderStr = "SHADER";
  20. private const string NoMaterialStr = "No Material";
  21. private const string NoShaderStr = "No Shader";
  22. private bool m_init = true;
  23. private string m_previousShaderName = string.Empty;
  24. private string m_previousMaterialName = string.Empty;
  25. private string m_previousShaderFunctionName = string.Empty;
  26. private Vector2 m_auxVector2;
  27. private GUIContent m_leftAuxContent = new GUIContent();
  28. private GUIContent m_rightAuxContent = new GUIContent();
  29. private GUIStyle m_leftButtonStyle = null;
  30. private GUIStyle m_rightButtonStyle = null;
  31. private Rect m_leftButtonRect;
  32. private Rect m_rightButtonRect;
  33. public ShaderEditorModeWindow( AmplifyShaderEditorWindow parentWindow ) : base( parentWindow, 0, 0, 0, 0, "ShaderEditorModeWindow", MenuAnchor.BOTTOM_CENTER, MenuAutoSize.NONE ) { }
  34. public void ConfigStyle( GUIStyle style )
  35. {
  36. style.normal.textColor = FontColorOff;
  37. style.hover.textColor = FontColorOff;
  38. style.active.textColor = FontColorOff;
  39. style.focused.textColor = FontColorOff;
  40. style.onNormal.textColor = FontColorOff;
  41. style.onHover.textColor = FontColorOff;
  42. style.onActive.textColor = FontColorOff;
  43. style.onFocused.textColor = FontColorOff;
  44. }
  45. public void Draw( Rect graphArea, Vector2 mousePos, Shader currentShader, Material currentMaterial, float usableArea, float leftPos, float rightPos /*, bool showLastSelection*/ )
  46. {
  47. EventType currentEventType = Event.current.type;
  48. if( !( currentEventType == EventType.Repaint || currentEventType == EventType.MouseDown || currentEventType == EventType.MouseMove ) )
  49. return;
  50. if ( m_init )
  51. {
  52. m_init = false;
  53. GUIStyle shaderModeTitle = UIUtils.GetCustomStyle( CustomStyle.ShaderModeTitle );
  54. GUIStyle shaderModeNoShader = UIUtils.GetCustomStyle( CustomStyle.ShaderModeNoShader );
  55. GUIStyle materialModeTitle = UIUtils.GetCustomStyle( CustomStyle.MaterialModeTitle );
  56. GUIStyle shaderNoMaterialModeTitle = UIUtils.GetCustomStyle( CustomStyle.ShaderNoMaterialModeTitle );
  57. ConfigStyle( shaderModeTitle );
  58. ConfigStyle( shaderModeNoShader );
  59. ConfigStyle( materialModeTitle );
  60. ConfigStyle( shaderNoMaterialModeTitle );
  61. }
  62. Color buffereredColor = GUI.color;
  63. MasterNode currentMasterNode = ParentWindow.CurrentGraph.CurrentMasterNode;
  64. // Shader Mode
  65. if ( currentMasterNode != null )
  66. {
  67. m_leftButtonStyle = UIUtils.GetCustomStyle( currentShader == null ? CustomStyle.ShaderModeNoShader : CustomStyle.ShaderModeTitle );
  68. m_leftButtonRect = graphArea;
  69. m_leftButtonRect.x = 10 + leftPos;
  70. m_leftButtonRect.y += m_leftButtonRect.height - 38 - 15;
  71. string shaderName = ( currentShader != null ) ? ( currentShader.name ) : NoShaderStr;
  72. if ( m_previousShaderName != shaderName )
  73. {
  74. m_previousShaderName = shaderName;
  75. m_leftAuxContent.text = "<size=20>SHADER</size>\n" + shaderName;
  76. }
  77. m_auxVector2 = m_leftButtonStyle.CalcSize( m_leftAuxContent );
  78. m_leftButtonRect.width = m_auxVector2.x + 30 + 4;
  79. m_leftButtonRect.height = 38;
  80. bool mouseOnTop = m_leftButtonRect.Contains( mousePos );
  81. GUI.color = mouseOnTop ? OverallColorOn : OverallColorOff;
  82. GUI.Label( m_leftButtonRect, m_leftAuxContent, m_leftButtonStyle );
  83. if( currentEventType == EventType.MouseMove && mouseOnTop )
  84. m_parentWindow.MarkToRepaint();
  85. if ( currentEventType == EventType.MouseDown && mouseOnTop && currentShader != null )
  86. {
  87. Event.current.Use();
  88. Selection.activeObject = currentShader;
  89. EditorGUIUtility.PingObject( Selection.activeObject );
  90. }
  91. // Material Mode
  92. if ( currentMaterial != null )
  93. {
  94. m_rightButtonStyle = UIUtils.GetCustomStyle( CustomStyle.MaterialModeTitle );
  95. m_rightButtonRect = graphArea;
  96. string matName = ( currentMaterial != null ) ? ( currentMaterial.name ) : NoMaterialStr;
  97. if ( m_previousMaterialName != matName )
  98. {
  99. m_previousMaterialName = matName;
  100. m_rightAuxContent.text = "<size=20>MATERIAL</size>\n" + matName;
  101. }
  102. m_auxVector2 = m_rightButtonStyle.CalcSize( m_rightAuxContent );
  103. m_rightButtonRect.width = m_auxVector2.x + 30 + 4;
  104. m_rightButtonRect.height = 38;
  105. m_rightButtonRect.x = graphArea.xMax - m_rightButtonRect.width - rightPos - 10;
  106. m_rightButtonRect.y = graphArea.yMax - 38 - 15;
  107. bool mouseOnTopRight = m_rightButtonRect.Contains( mousePos );
  108. GUI.color = mouseOnTopRight ? OverallColorOn : OverallColorOff;
  109. GUI.Label( m_rightButtonRect, m_rightAuxContent, m_rightButtonStyle );
  110. if( currentEventType == EventType.MouseMove && mouseOnTopRight )
  111. m_parentWindow.MarkToRepaint();
  112. if ( currentEventType == EventType.MouseDown && mouseOnTopRight )
  113. {
  114. Event.current.Use();
  115. Selection.activeObject = currentMaterial;
  116. EditorGUIUtility.PingObject( Selection.activeObject );
  117. }
  118. }
  119. }
  120. // Shader Function
  121. else if ( currentMasterNode == null && ParentWindow.CurrentGraph.CurrentOutputNode != null )
  122. {
  123. m_leftButtonStyle = UIUtils.GetCustomStyle( CustomStyle.ShaderFunctionMode );
  124. m_leftButtonRect = graphArea;
  125. m_leftButtonRect.x = 10 + leftPos;
  126. m_leftButtonRect.y += m_leftButtonRect.height - 38 - 15;
  127. string functionName = ( ParentWindow.CurrentGraph.CurrentShaderFunction != null ) ? ( ParentWindow.CurrentGraph.CurrentShaderFunction.name ) : "No Shader Function";
  128. if ( m_previousShaderFunctionName != functionName )
  129. {
  130. m_previousShaderFunctionName = functionName;
  131. m_leftAuxContent.text = "<size=20>SHADER FUNCTION</size>\n" + functionName;
  132. }
  133. m_auxVector2 = m_leftButtonStyle.CalcSize( m_leftAuxContent );
  134. m_leftButtonRect.width = m_auxVector2.x + 30 + 4;
  135. m_leftButtonRect.height = 38;
  136. bool mouseOnTop = m_leftButtonRect.Contains( mousePos );
  137. GUI.color = mouseOnTop ? OverallColorOn : OverallColorOff;
  138. GUI.Label( m_leftButtonRect, m_leftAuxContent, m_leftButtonStyle );
  139. if ( currentEventType == EventType.MouseDown && mouseOnTop && ParentWindow.CurrentGraph.CurrentShaderFunction != null )
  140. {
  141. Event.current.Use();
  142. Selection.activeObject = ParentWindow.CurrentGraph.CurrentShaderFunction;
  143. EditorGUIUtility.PingObject( Selection.activeObject );
  144. }
  145. }
  146. GUI.color = buffereredColor;
  147. }
  148. public override void Destroy()
  149. {
  150. base.Destroy();
  151. m_leftAuxContent = null;
  152. m_rightAuxContent = null;
  153. m_leftButtonStyle = null;
  154. m_rightButtonStyle = null;
  155. }
  156. }
  157. }