summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/AmplifyShaderEditor/Plugins/Editor/Menu/Tools/ToolsMenuButton.cs
blob: a1994638706438590416e99d7f695e5f8d4bc9e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// 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; }
		}
	}
}