123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- // Amplify Shader Editor - Visual Shader Editing Tool
- // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
- using UnityEngine;
- using UnityEditor;
- using System.Collections.Generic;
- namespace AmplifyShaderEditor
- {
- public sealed class ToolsMenuButton : ToolsMenuButtonParent
- {
- public delegate void ToolButtonPressed( ToolButtonType type );
- public event ToolButtonPressed ToolButtonPressedEvt;
- private Rect m_buttonArea;
- private List<Texture2D> m_buttonTexture;
- private string m_buttonTexturePath;
- private ToolButtonType m_buttonType;
- private GUIStyle m_style;
- private bool m_enabled = true;
- private bool m_drawOnFunction = true;
- private List<string> m_cachedStates;
- private int m_bufferedState = -1;
- private string m_bufferedTooltip = string.Empty;
- 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 )
- {
- m_buttonArea = new Rect( x, y, width, height );
- m_buttonType = type;
- m_buttonTexturePath = texturePath;
- m_cachedStates = new List<string>();
- m_drawOnFunction = drawOnFunction;
- }
-
- public void AddState( string state )
- {
- m_cachedStates.Add( state );
- }
- public override void Destroy()
- {
- ToolButtonPressedEvt = null;
- if ( m_buttonTexture != null )
- {
- for ( int i = 0; i < m_buttonTexture.Count; i++ )
- {
- Resources.UnloadAsset( m_buttonTexture[ i ] );
- }
- m_buttonTexture.Clear();
- }
- m_buttonTexture = null;
- }
- protected override void Init()
- {
- base.Init();
- if ( m_buttonTexture == null )
- {
- m_buttonTexturePath = AssetDatabase.GUIDToAssetPath( m_buttonTexturePath );
- m_buttonTexture = new List<Texture2D>();
- m_buttonTexture.Add( AssetDatabase.LoadAssetAtPath( m_buttonTexturePath, typeof( Texture2D ) ) as Texture2D );
- }
- if ( m_cachedStates.Count > 0 )
- {
- for ( int i = 0; i < m_cachedStates.Count; i++ )
- {
- m_cachedStates[ i ] = AssetDatabase.GUIDToAssetPath( m_cachedStates[ i ] );
- m_buttonTexture.Add( AssetDatabase.LoadAssetAtPath( m_cachedStates[ i ], typeof( Texture2D ) ) as Texture2D );
- }
- m_cachedStates.Clear();
- }
- if ( m_style == null )
- {
- m_style = new GUIStyle( /*UIUtils.Button*/ GUIStyle.none );
- m_style.normal.background = m_buttonTexture[ 0 ];
- m_style.hover.background = m_buttonTexture[ 0 ];
- m_style.hover.textColor = m_style.normal.textColor;
- m_style.active.background = m_buttonTexture[ 0 ];
- m_style.active.textColor = m_style.normal.textColor;
- m_style.onNormal.background = m_buttonTexture[ 0 ];
- m_style.onNormal.textColor = m_style.normal.textColor;
- m_style.onHover.background = m_buttonTexture[ 0 ];
- m_style.onHover.textColor = m_style.normal.textColor;
- m_style.onActive.background = m_buttonTexture[ 0 ];
- m_style.onActive.textColor = m_style.normal.textColor;
- m_style.clipping = TextClipping.Overflow;
- m_style.fontStyle = FontStyle.Bold;
- m_style.alignment = TextAnchor.LowerCenter;
- m_style.contentOffset = new Vector2( 0, 15 );
- m_style.fontSize = 10;
- bool resizeFromTexture = false;
- if ( m_buttonArea.width > 0 )
- {
- m_style.fixedWidth = m_buttonArea.width;
- }
- else
- {
- resizeFromTexture = true;
- }
- if ( m_buttonArea.height > 0 )
- {
- m_style.fixedHeight = m_buttonArea.height;
- }
- else
- {
- resizeFromTexture = true;
- }
- if ( resizeFromTexture )
- {
- m_buttonArea.width = m_style.fixedWidth = m_buttonTexture[ 0 ].width;
- m_buttonArea.height = m_style.fixedHeight = m_buttonTexture[ 0 ].height;
- }
- }
- }
- public override void Draw()
- {
- base.Draw();
- bool guiEnabledBuffer = GUI.enabled;
- GUI.enabled = m_enabled;
- if ( GUILayout.Button( m_content, m_style ) && ToolButtonPressedEvt != null )
- {
- ToolButtonPressedEvt( m_buttonType );
- }
- GUI.enabled = guiEnabledBuffer;
- }
- public override void Draw( float x, float y )
- {
- if ( !(m_parentWindow.CameraDrawInfo.CurrentEventType == EventType.MouseDown || m_parentWindow.CameraDrawInfo.CurrentEventType == EventType.Repaint ) )
- return;
- if ( m_parentWindow.CurrentGraph.CurrentMasterNode == null && !m_drawOnFunction)
- return;
- base.Draw( x, y );
- if ( m_bufferedState > -1 )
- {
- if ( string.IsNullOrEmpty( m_bufferedTooltip ) )
- {
- SetStateOnButton( m_bufferedState );
- }
- else
- {
- SetStateOnButton( m_bufferedState, m_bufferedTooltip );
- }
- m_bufferedState = -1;
- m_bufferedTooltip = string.Empty;
- }
- m_buttonArea.x = x;
- m_buttonArea.y = y;
-
- if ( m_parentWindow.CameraDrawInfo.CurrentEventType == EventType.MouseDown && m_buttonArea.Contains( m_parentWindow.CameraDrawInfo.MousePosition ) && ToolButtonPressedEvt != null )
- {
- ToolButtonPressedEvt( m_buttonType );
- Event.current.Use();
- m_parentWindow.CameraDrawInfo.CurrentEventType = EventType.Used;
- }
- else if ( m_parentWindow.CameraDrawInfo.CurrentEventType == EventType.Repaint )
- {
- GUI.Label( m_buttonArea, m_content, m_style );
- }
- //if ( GUI.Button( m_buttonArea, m_content, m_style ) && ToolButtonPressedEvt != null )
- //{
- // ToolButtonPressedEvt( m_buttonType );
- //}
- }
- public override void Draw( Vector2 pos )
- {
- Draw( pos.x, pos.y );
- }
- public override void SetStateOnButton( int state, string tooltip )
- {
- if ( m_buttonTexture == null || m_style == null )
- {
- m_bufferedState = state;
- m_bufferedTooltip = tooltip;
- return;
- }
- if ( state < 0 || state >= m_buttonTexture.Count )
- {
- return;
- }
-
- base.SetStateOnButton( state, tooltip );
- m_style.normal.background = m_buttonTexture[ state ];
- m_style.hover.background = m_buttonTexture[ state ];
- m_style.active.background = m_buttonTexture[ state ];
- m_style.onNormal.background = m_buttonTexture[ state ];
- m_style.onHover.background = m_buttonTexture[ state ];
- m_style.onActive.background = m_buttonTexture[ state ];
- }
- public override void SetStateOnButton( int state )
- {
- if ( m_buttonTexture == null || m_style == null )
- {
- m_bufferedState = state;
- return;
- }
- if ( state < 0 || state >= m_buttonTexture.Count )
- {
- return;
- }
- base.SetStateOnButton( state );
- m_style.normal.background = m_buttonTexture[ state ];
- m_style.hover.background = m_buttonTexture[ state ];
- m_style.active.background = m_buttonTexture[ state ];
- m_style.onNormal.background = m_buttonTexture[ state ];
- m_style.onHover.background = m_buttonTexture[ state ];
- m_style.onActive.background = m_buttonTexture[ state ];
- }
- public bool IsInside( Vector2 pos )
- {
- return m_buttonArea.Contains( pos );
- }
- public bool Enabled
- {
- get { return m_enabled; }
- set { m_enabled = value; }
- }
- }
- }
|