| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 | 
							- // Amplify Shader Editor - Visual Shader Editing Tool
 
- // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
 
- using UnityEngine;
 
- using UnityEditor;
 
- namespace AmplifyShaderEditor
 
- {
 
- 	public enum MenuAnchor
 
- 	{
 
- 		TOP_LEFT = 0,
 
- 		TOP_CENTER,
 
- 		TOP_RIGHT,
 
- 		MIDDLE_LEFT,
 
- 		MIDDLE_CENTER,
 
- 		MIDDLE_RIGHT,
 
- 		BOTTOM_LEFT,
 
- 		BOTTOM_CENTER,
 
- 		BOTTOM_RIGHT,
 
- 		NONE
 
- 	}
 
- 	public enum MenuAutoSize
 
- 	{
 
- 		MATCH_VERTICAL = 0,
 
- 		MATCH_HORIZONTAL,
 
- 		NONE
 
- 	}
 
- 	public class MenuParent
 
- 	{
 
- 		protected AmplifyShaderEditorWindow m_parentWindow = null;
 
- 		protected const float MinimizeButtonXSpacing = 5;
 
- 		protected const float MinimizeButtonYSpacing = 5.5f;
 
- 		protected const float ResizeAreaWidth = 5;
 
- 		protected const float MinimizeCollisionAdjust = 5;
 
- 		protected GUIStyle m_style;
 
- 		protected GUIContent m_content;
 
- 		protected Rect m_maximizedArea;
 
- 		protected Rect m_transformedArea;
 
- 		protected Rect m_resizeArea;
 
- 		protected MenuAnchor m_anchor;
 
- 		protected MenuAutoSize m_autoSize;
 
- 		protected bool m_isActive = true;
 
- 		protected bool m_isMaximized = true;
 
- 		protected bool m_lockOnMinimize = false;
 
- 		protected bool m_preLockState = false;
 
- 		protected Rect m_minimizedArea;
 
- 		protected Rect m_minimizeButtonPos;
 
- 		protected float m_realWidth;
 
- 		protected GUIStyle m_empty = new GUIStyle();
 
- 		protected float m_resizeDelta;
 
- 		protected bool m_isResizing = false;
 
- 		protected bool m_resizable = false;
 
- 		protected GUIStyle m_resizeAreaStyle;
 
- 		protected bool m_isMouseInside = false;
 
- 		protected Vector2 m_currentScrollPos;
 
- 		public MenuParent( AmplifyShaderEditorWindow parentWindow, float x, float y, float width, float height, string name, MenuAnchor anchor = MenuAnchor.NONE, MenuAutoSize autoSize = MenuAutoSize.NONE )
 
- 		{
 
- 			m_parentWindow = parentWindow;
 
- 			m_anchor = anchor;
 
- 			m_autoSize = autoSize;
 
- 			m_maximizedArea = new Rect( x, y, width, height );
 
- 			m_content = new GUIContent( GUIContent.none );
 
- 			m_content.text = name;
 
- 			m_transformedArea = new Rect();
 
- 			m_resizeArea = new Rect();
 
- 			m_resizeArea.width = ResizeAreaWidth;
 
- 			m_resizeAreaStyle = GUIStyle.none;
 
- 			m_currentScrollPos = Vector2.zero;
 
- 		}
 
- 		public void SetMinimizedArea( float x, float y, float width, float height )
 
- 		{
 
- 			m_minimizedArea = new Rect( x, y, width, height );
 
- 		}
 
- 		protected void InitDraw( Rect parentPosition, Vector2 mousePosition, int mouseButtonId )
 
- 		{
 
- 			if ( m_style == null )
 
- 			{
 
- 				m_style = new GUIStyle( UIUtils.TextArea );
 
- 				m_style.normal.background = m_style.normal.scaledBackgrounds[ 0 ];
 
- 				m_style.normal.scaledBackgrounds = null;
 
- 				m_style.border = new RectOffset( 4, 4, 4, 4 );
 
- 				m_style.stretchHeight = true;
 
- 				m_style.stretchWidth = true;
 
- 				m_style.fontSize = ( int ) Constants.DefaultTitleFontSize;
 
- 				m_style.fontStyle = FontStyle.Normal;
 
- 				Texture minimizeTex = UIUtils.GetCustomStyle( CustomStyle.MaximizeButton ).normal.background;
 
- 				m_minimizeButtonPos = new Rect( 0, 0, minimizeTex.width, minimizeTex.height );
 
- 			}
 
- 			Rect currentArea = m_isMaximized ? m_maximizedArea : m_minimizedArea;
 
- 			if ( m_isMaximized )
 
- 			{
 
- 				if ( m_resizable )
 
- 				{
 
- 					if ( m_isResizing )
 
- 					{
 
- 						if ( m_anchor == MenuAnchor.TOP_LEFT )
 
- 							m_resizeDelta = ( ParentWindow.CurrentEvent.mousePosition.x - m_maximizedArea.width );
 
- 						else if ( m_anchor == MenuAnchor.TOP_RIGHT )
 
- 							m_resizeDelta = ParentWindow.CurrentEvent.mousePosition.x - ( parentPosition.width - m_maximizedArea.width);
 
- 					}
 
- 				}
 
- 				m_realWidth = m_maximizedArea.width;
 
- 				if ( m_resizable )
 
- 				{
 
- 					if ( m_anchor == MenuAnchor.TOP_LEFT )
 
- 					{
 
- 						currentArea.width += m_resizeDelta;
 
- 						m_realWidth += m_resizeDelta;
 
- 					}
 
- 					else if ( m_anchor == MenuAnchor.TOP_RIGHT )
 
- 					{
 
- 						currentArea.width -= m_resizeDelta;
 
- 						m_realWidth -= m_resizeDelta;
 
- 					}
 
- 				}
 
- 			}
 
- 			else
 
- 			{
 
- 				if ( currentArea.x < 0 )
 
- 				{
 
- 					m_realWidth = currentArea.width + currentArea.x;
 
- 				}
 
- 				else if ( ( currentArea.x + currentArea.width ) > parentPosition.width )
 
- 				{
 
- 					m_realWidth = parentPosition.width - currentArea.x;
 
- 				}
 
- 				if ( m_realWidth < 0 )
 
- 					m_realWidth = 0;
 
- 			}
 
- 			switch ( m_anchor )
 
- 			{
 
- 				case MenuAnchor.TOP_LEFT:
 
- 				{
 
- 					m_transformedArea.x = currentArea.x;
 
- 					m_transformedArea.y = currentArea.y;
 
- 					if ( m_isMaximized )
 
- 					{
 
- 						m_minimizeButtonPos.x = m_transformedArea.x + m_transformedArea.width - m_minimizeButtonPos.width - MinimizeButtonXSpacing;
 
- 						m_minimizeButtonPos.y = m_transformedArea.y + MinimizeButtonYSpacing;
 
- 						m_resizeArea.x = m_transformedArea.x + m_transformedArea.width;
 
- 						m_resizeArea.y = m_minimizeButtonPos.y;
 
- 						m_resizeArea.height = m_transformedArea.height;
 
- 					}
 
- 					else
 
- 					{
 
- 						float width = ( m_transformedArea.width - m_transformedArea.x );
 
- 						m_minimizeButtonPos.x = m_transformedArea.x + width * 0.5f - m_minimizeButtonPos.width * 0.5f;
 
- 						m_minimizeButtonPos.y = m_transformedArea.height * 0.5f - m_minimizeButtonPos.height * 0.5f;
 
- 					}
 
- 				}
 
- 				break;
 
- 				case MenuAnchor.TOP_CENTER:
 
- 				{
 
- 					m_transformedArea.x = parentPosition.width * 0.5f + currentArea.x;
 
- 					m_transformedArea.y = currentArea.y;
 
- 				}
 
- 				break;
 
- 				case MenuAnchor.TOP_RIGHT:
 
- 				{
 
- 					m_transformedArea.x = parentPosition.width - currentArea.x - currentArea.width;
 
- 					m_transformedArea.y = currentArea.y;
 
- 					if ( m_isMaximized )
 
- 					{
 
- 						m_minimizeButtonPos.x = m_transformedArea.x + MinimizeButtonXSpacing;
 
- 						m_minimizeButtonPos.y = m_transformedArea.y + MinimizeButtonYSpacing;
 
- 						m_resizeArea.x = m_transformedArea.x - ResizeAreaWidth;
 
- 						m_resizeArea.y = m_minimizeButtonPos.y;
 
- 						m_resizeArea.height = m_transformedArea.height;
 
- 					}
 
- 					else
 
- 					{
 
- 						float width = ( parentPosition.width - m_transformedArea.x );
 
- 						m_minimizeButtonPos.x = m_transformedArea.x + width * 0.5f - m_minimizeButtonPos.width * 0.5f;
 
- 						m_minimizeButtonPos.y = m_transformedArea.height * 0.5f - m_minimizeButtonPos.height * 0.5f;
 
- 					}
 
- 				}
 
- 				break;
 
- 				case MenuAnchor.MIDDLE_LEFT:
 
- 				{
 
- 					m_transformedArea.x = currentArea.x;
 
- 					m_transformedArea.y = parentPosition.height * 0.5f + currentArea.y;
 
- 				}
 
- 				break;
 
- 				case MenuAnchor.MIDDLE_CENTER:
 
- 				{
 
- 					m_transformedArea.x = parentPosition.width * 0.5f + currentArea.x;
 
- 					m_transformedArea.y = parentPosition.height * 0.5f + currentArea.y;
 
- 				}
 
- 				break;
 
- 				case MenuAnchor.MIDDLE_RIGHT:
 
- 				{
 
- 					m_transformedArea.x = parentPosition.width - currentArea.x - currentArea.width;
 
- 					m_transformedArea.y = parentPosition.height * 0.5f + currentArea.y;
 
- 				}
 
- 				break;
 
- 				case MenuAnchor.BOTTOM_LEFT:
 
- 				{
 
- 					m_transformedArea.x = currentArea.x;
 
- 					m_transformedArea.y = parentPosition.height - currentArea.y - currentArea.height;
 
- 				}
 
- 				break;
 
- 				case MenuAnchor.BOTTOM_CENTER:
 
- 				{
 
- 					m_transformedArea.x = parentPosition.width * 0.5f + currentArea.x;
 
- 					m_transformedArea.y = parentPosition.height - currentArea.y - currentArea.height;
 
- 				}
 
- 				break;
 
- 				case MenuAnchor.BOTTOM_RIGHT:
 
- 				{
 
- 					m_transformedArea.x = parentPosition.width - currentArea.x - currentArea.width;
 
- 					m_transformedArea.y = parentPosition.height - currentArea.y - currentArea.height;
 
- 				}
 
- 				break;
 
- 				case MenuAnchor.NONE:
 
- 				{
 
- 					m_transformedArea.x = currentArea.x;
 
- 					m_transformedArea.y = currentArea.y;
 
- 				}
 
- 				break;
 
- 			}
 
- 			switch ( m_autoSize )
 
- 			{
 
- 				case MenuAutoSize.MATCH_HORIZONTAL:
 
- 				{
 
- 					m_transformedArea.width = parentPosition.width - m_transformedArea.x;
 
- 					m_transformedArea.height = currentArea.height;
 
- 				}
 
- 				break;
 
- 				case MenuAutoSize.MATCH_VERTICAL:
 
- 				{
 
- 					m_transformedArea.width = currentArea.width;
 
- 					m_transformedArea.height = parentPosition.height - m_transformedArea.y;
 
- 				}
 
- 				break;
 
- 				case MenuAutoSize.NONE:
 
- 				{
 
- 					m_transformedArea.width = currentArea.width;
 
- 					m_transformedArea.height = currentArea.height;
 
- 				}
 
- 				break;
 
- 			}
 
- 		}
 
- 		public virtual void Draw( Rect parentPosition, Vector2 mousePosition, int mouseButtonId, bool hasKeyboadFocus )
 
- 		{
 
- 			InitDraw( parentPosition, mousePosition, mouseButtonId );
 
- 			if ( ParentWindow.CurrentEvent.type == EventType.MouseDrag && ParentWindow.CurrentEvent.button > 0 /*catches both middle and right mouse button*/ )
 
- 			{
 
- 				m_isMouseInside = IsInside( mousePosition );
 
- 				if ( m_isMouseInside )
 
- 				{
 
- 					m_currentScrollPos.x += Constants.MenuDragSpeed * ParentWindow.CurrentEvent.delta.x;
 
- 					if ( m_currentScrollPos.x < 0 )
 
- 						m_currentScrollPos.x = 0;
 
- 					m_currentScrollPos.y += Constants.MenuDragSpeed * ParentWindow.CurrentEvent.delta.y;
 
- 					if ( m_currentScrollPos.y < 0 )
 
- 						m_currentScrollPos.y = 0;
 
- 				}
 
- 			}
 
- 		}
 
- 		public void PostDraw()
 
- 		{
 
- 			if ( !m_isMaximized )
 
- 			{
 
- 				m_transformedArea.height = 35;
 
- 				GUI.Label( m_transformedArea, m_content, m_style );
 
- 			}
 
- 			Color colorBuffer = GUI.color;
 
- 			GUI.color = EditorGUIUtility.isProSkin ? Color.white : Color.black;
 
- 			bool guiEnabledBuffer = GUI.enabled;
 
- 			GUI.enabled = !m_lockOnMinimize;
 
- 			Rect buttonArea = m_minimizeButtonPos;
 
- 			buttonArea.x -= MinimizeCollisionAdjust;
 
- 			buttonArea.width += 2 * MinimizeCollisionAdjust;
 
- 			buttonArea.y -= MinimizeCollisionAdjust;
 
- 			buttonArea.height += 2 * MinimizeCollisionAdjust;
 
- 			if ( m_parentWindow.CameraDrawInfo.CurrentEventType == EventType.Repaint )
 
- 				GUI.Label( m_minimizeButtonPos, string.Empty, UIUtils.GetCustomStyle( m_isMaximized ? CustomStyle.MinimizeButton : CustomStyle.MaximizeButton ) );
 
- 			if( m_parentWindow.CameraDrawInfo.CurrentEventType == EventType.MouseDown && buttonArea.Contains( m_parentWindow.CameraDrawInfo.MousePosition ) )
 
- 			//if ( GUI.Button( buttonArea, string.Empty, m_empty ) )
 
- 			{
 
- 				m_isMaximized = !m_isMaximized;
 
- 				m_resizeDelta = 0;
 
- 			}
 
- 			if ( m_resizable && m_isMaximized )
 
- 			{
 
- 				EditorGUIUtility.AddCursorRect( m_resizeArea, MouseCursor.ResizeHorizontal );
 
- 				if ( !m_isResizing && GUI.RepeatButton( m_resizeArea, string.Empty, m_resizeAreaStyle ) )
 
- 				{
 
- 					m_isResizing = true;
 
- 				}
 
- 				else
 
- 				{
 
- 					if ( m_isResizing )
 
- 					{
 
- 						if ( ParentWindow.CurrentEvent.isMouse && ParentWindow.CurrentEvent.type != EventType.MouseDrag )
 
- 						{
 
- 							m_isResizing = false;
 
- 						}
 
- 					}
 
- 				}
 
- 				if ( m_realWidth < buttonArea.width )
 
- 				{
 
- 					// Auto-minimize
 
- 					m_isMaximized = false;
 
- 					m_resizeDelta = 0;
 
- 					m_isResizing = false;
 
- 				}
 
- 				else
 
- 				{
 
- 					float halfSizeWindow = 0.5f * ParentWindow.position.width;
 
- 					if ( m_realWidth > halfSizeWindow )
 
- 					{
 
- 						m_realWidth = 0.5f * ParentWindow.position.width;
 
- 						if ( m_resizeDelta > 0 )
 
- 						{
 
- 							m_resizeDelta = m_realWidth - m_maximizedArea.width;
 
- 						}
 
- 						else
 
- 						{
 
- 							m_resizeDelta = m_maximizedArea.width - m_realWidth;
 
- 						}
 
- 					}
 
- 				}
 
- 			}
 
- 			GUI.enabled = guiEnabledBuffer;
 
- 			GUI.color = colorBuffer;
 
- 			
 
- 		}
 
- 		public void OnLostFocus()
 
- 		{
 
- 			if ( m_isResizing )
 
- 			{
 
- 				m_isResizing = false;
 
- 			}
 
- 		}
 
- 		virtual public void Destroy()
 
- 		{
 
- 			m_empty = null;
 
- 			m_resizeAreaStyle = null;
 
- 		}
 
- 		public float InitialX
 
- 		{
 
- 			get { return m_maximizedArea.x; }
 
- 			set { m_maximizedArea.x = value; }
 
- 		}
 
- 		public float Width
 
- 		{
 
- 			get { return m_maximizedArea.width; }
 
- 			set { m_maximizedArea.width = value; }
 
- 		}
 
- 		public float RealWidth
 
- 		{
 
- 			get { return m_realWidth; }
 
- 		}
 
- 		public float Height
 
- 		{
 
- 			get { return m_maximizedArea.height; }
 
- 			set { m_maximizedArea.height = value; }
 
- 		}
 
- 		public Rect Size
 
- 		{
 
- 			get { return m_maximizedArea; }
 
- 		}
 
- 		public virtual bool IsInside( Vector2 position )
 
- 		{
 
- 			if ( !m_isActive )
 
- 				return false;
 
- 			return m_transformedArea.Contains( position );
 
- 		}
 
- 		public bool IsMaximized
 
- 		{
 
- 			get { return m_isMaximized; }
 
- 			set { m_isMaximized = value; }
 
- 		}
 
- 		public Rect TransformedArea
 
- 		{
 
- 			get { return m_transformedArea; }
 
- 		}
 
- 		public bool Resizable { set { m_resizable = value; } }
 
- 		public bool IsResizing { get { return m_isResizing; } }
 
- 		public bool LockOnMinimize
 
- 		{
 
- 			set
 
- 			{
 
- 				if ( m_lockOnMinimize == value )
 
- 					return;
 
- 				m_lockOnMinimize = value;
 
- 				if ( value )
 
- 				{
 
- 					m_preLockState = m_isMaximized;
 
- 					m_isMaximized = false;
 
- 				}
 
- 				else
 
- 				{
 
- 					m_isMaximized = m_preLockState;
 
- 				}
 
- 			}
 
- 		}
 
- 		public bool IsActive
 
- 		{
 
- 			get { return m_isActive; }
 
- 		}
 
- 		public AmplifyShaderEditorWindow ParentWindow { get { return m_parentWindow; } set { m_parentWindow = value; } }
 
- 	}
 
- }
 
 
  |