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.

186 lines
6.9 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. 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 ) )
  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.MouseDown && mouseOnTop && currentShader != null )
  84. {
  85. Event.current.Use();
  86. Selection.activeObject = currentShader;
  87. EditorGUIUtility.PingObject( Selection.activeObject );
  88. }
  89. // Material Mode
  90. if ( currentMaterial != null )
  91. {
  92. m_rightButtonStyle = UIUtils.GetCustomStyle( CustomStyle.MaterialModeTitle );
  93. m_rightButtonRect = graphArea;
  94. string matName = ( currentMaterial != null ) ? ( currentMaterial.name ) : NoMaterialStr;
  95. if ( m_previousMaterialName != matName )
  96. {
  97. m_previousMaterialName = matName;
  98. m_rightAuxContent.text = "<size=20>MATERIAL</size>\n" + matName;
  99. }
  100. m_auxVector2 = m_rightButtonStyle.CalcSize( m_rightAuxContent );
  101. m_rightButtonRect.width = m_auxVector2.x + 30 + 4;
  102. m_rightButtonRect.height = 38;
  103. m_rightButtonRect.x = graphArea.xMax - m_rightButtonRect.width - rightPos - 10;
  104. m_rightButtonRect.y = graphArea.yMax - 38 - 15;
  105. bool mouseOnTopRight = m_rightButtonRect.Contains( mousePos );
  106. GUI.color = mouseOnTopRight ? OverallColorOn : OverallColorOff;
  107. GUI.Label( m_rightButtonRect, m_rightAuxContent, m_rightButtonStyle );
  108. if ( currentEventType == EventType.MouseDown && mouseOnTopRight )
  109. {
  110. Event.current.Use();
  111. Selection.activeObject = currentMaterial;
  112. EditorGUIUtility.PingObject( Selection.activeObject );
  113. }
  114. }
  115. }
  116. // Shader Function
  117. else if ( currentMasterNode == null && ParentWindow.CurrentGraph.CurrentOutputNode != null )
  118. {
  119. m_leftButtonStyle = UIUtils.GetCustomStyle( CustomStyle.ShaderFunctionMode );
  120. m_leftButtonRect = graphArea;
  121. m_leftButtonRect.x = 10 + leftPos;
  122. m_leftButtonRect.y += m_leftButtonRect.height - 38 - 15;
  123. string functionName = ( ParentWindow.CurrentGraph.CurrentShaderFunction != null ) ? ( ParentWindow.CurrentGraph.CurrentShaderFunction.name ) : "No Shader Function";
  124. if ( m_previousShaderFunctionName != functionName )
  125. {
  126. m_previousShaderFunctionName = functionName;
  127. m_leftAuxContent.text = "<size=20>SHADER FUNCTION</size>\n" + functionName;
  128. }
  129. m_auxVector2 = m_leftButtonStyle.CalcSize( m_leftAuxContent );
  130. m_leftButtonRect.width = m_auxVector2.x + 30 + 4;
  131. m_leftButtonRect.height = 38;
  132. bool mouseOnTop = m_leftButtonRect.Contains( mousePos );
  133. GUI.color = mouseOnTop ? OverallColorOn : OverallColorOff;
  134. GUI.Label( m_leftButtonRect, m_leftAuxContent, m_leftButtonStyle );
  135. if ( currentEventType == EventType.MouseDown && mouseOnTop && ParentWindow.CurrentGraph.CurrentShaderFunction != null )
  136. {
  137. Event.current.Use();
  138. Selection.activeObject = ParentWindow.CurrentGraph.CurrentShaderFunction;
  139. EditorGUIUtility.PingObject( Selection.activeObject );
  140. }
  141. }
  142. GUI.color = buffereredColor;
  143. }
  144. public override void Destroy()
  145. {
  146. base.Destroy();
  147. m_leftAuxContent = null;
  148. m_rightAuxContent = null;
  149. m_leftButtonStyle = null;
  150. m_rightButtonStyle = null;
  151. }
  152. }
  153. }