summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/AmplifyShaderEditor/Plugins/Editor/Utils/WindowHelper.cs
blob: 7b8972b4a350b7461e61334da2b057e685053f7a (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
250
#if UNITY_EDITOR
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;

public static class WindowHelper
{
	private class R_EditorWindow
	{
		private EditorWindow m_instance;
		private System.Type m_type;

		public R_EditorWindow( EditorWindow instance )
		{
			m_instance = instance;
			m_type = instance.GetType();
		}

		public object Parent
		{
			get
			{
				var field = m_type.GetField( "m_Parent", BindingFlags.Instance | BindingFlags.NonPublic );
				return field.GetValue( m_instance );
			}
		}

		public object Docked
		{
			get
			{
				var property = m_type.GetProperty( "docked", BindingFlags.Instance | BindingFlags.NonPublic );
				return property.GetValue( m_instance, null );
			}
		}
	}

	private class R_DockArea
	{
		private object m_instance;
		private System.Type m_type;

		public R_DockArea( object instance )
		{
			m_instance = instance;
			m_type = instance.GetType();
		}

		public object Window
		{
			get
			{
				var property = m_type.GetProperty( "window", BindingFlags.Instance | BindingFlags.Public );
				return property.GetValue( m_instance, null );
			}
		}

		public object ActualView
		{
			get
			{
				var field = m_type.GetField( "m_ActualView", BindingFlags.Instance | BindingFlags.NonPublic );
				return field.GetValue( m_instance );
			}
		}

		public object OriginalDragSource
		{
			set
			{
				var field = m_type.GetField( "s_OriginalDragSource", BindingFlags.Static | BindingFlags.NonPublic );
				field.SetValue( null, value );
			}
		}


		public void AddTab( EditorWindow pane )
		{
#if UNITY_2018_3_OR_NEWER
			var method = m_type.GetMethod( "AddTab", BindingFlags.Instance | BindingFlags.Public, null, new System.Type[] { typeof( EditorWindow ), typeof( bool ) }, null );
			method.Invoke( m_instance, new object[] { pane, true } );
#else
			var method = m_type.GetMethod( "AddTab", BindingFlags.Instance | BindingFlags.Public, null, new System.Type[] { typeof( EditorWindow ) }, null );
			method.Invoke( m_instance, new object[] { pane } );
#endif
		}

		public void RemoveTab( EditorWindow pane )
		{
			var method = m_type.GetMethod( "RemoveTab", BindingFlags.Instance | BindingFlags.Public, null, new System.Type[] { typeof( EditorWindow ) }, null );
			method.Invoke( m_instance, new object[] { pane } );
		}
	}

	private class R_ContainerWindow
	{
		private object m_instance;
		private System.Type m_type;

		public R_ContainerWindow( object instance )
		{
			m_instance = instance;
			m_type = instance.GetType();
		}

		public object RootSplitView
		{
			get
			{
				var property = m_type.GetProperty( "rootSplitView", BindingFlags.Instance | BindingFlags.Public );
				return property.GetValue( m_instance, null );
			}
		}

		public object RootView
		{
			get
			{
				var property = m_type.GetProperty( "rootView", BindingFlags.Instance | BindingFlags.Public );
				return property.GetValue( m_instance, null );
			}
		}

		public object WindowPtr
		{
			get
			{
				var all = m_type.GetNestedTypes();
				foreach( var item in all )
				{
					Debug.Log( item.Name );
				}
				var property = m_type.GetField( "m_WindowPtr", BindingFlags.Instance | BindingFlags.NonPublic );
				return property.GetValue( m_instance );
			}
		}
	}

	private class R_SplitView
	{
		private object m_instance;
		private System.Type m_type;

		public R_SplitView( object instance )
		{
			m_instance = instance;
			m_type = instance.GetType();
		}

		public object DragOver( EditorWindow child, Vector2 screenPoint )
		{
			var method = m_type.GetMethod( "DragOver", BindingFlags.Instance | BindingFlags.Public );
			return method.Invoke( m_instance, new object[] { child, screenPoint } );
		}

		public void PerformDrop( EditorWindow child, object dropInfo, Vector2 screenPoint )
		{
			var method = m_type.GetMethod( "PerformDrop", BindingFlags.Instance | BindingFlags.Public );
			method.Invoke( m_instance, new object[] { child, dropInfo, screenPoint } );
		}
	}

	public enum DockPosition
	{
		Left,
		Top,
		Right,
		Bottom
	}

	public static bool IsDocked( this EditorWindow wnd )
	{
		var parent = new R_EditorWindow( wnd );
		return (bool)parent.Docked;
	}

	public static void Undock( this EditorWindow wnd )
	{
		var parent = new R_EditorWindow( wnd );
		var dockArea = new R_DockArea( parent.Parent );
		dockArea.RemoveTab( wnd );
		wnd.Show( true );
	}

	public static void RemoveTab( this EditorWindow wnd )
	{
		var parent = new R_EditorWindow( wnd );
		var dockArea = new R_DockArea( parent.Parent );
		dockArea.RemoveTab( wnd );
	}

	/// <summary>
	/// Docks the second window to the first window at the given position
	/// </summary>
	public static void Dock( this EditorWindow wnd, EditorWindow other, DockPosition position )
	{
		var mousePosition = GetFakeMousePosition( wnd, position );

		var parent = new R_EditorWindow( wnd );
		var child = new R_EditorWindow( other );
		var dockArea = new R_DockArea( parent.Parent );
		var containerWindow = new R_ContainerWindow( dockArea.Window );
		var splitView = new R_SplitView( containerWindow.RootSplitView );
		var dropInfo = splitView.DragOver( other, mousePosition );
		dockArea.OriginalDragSource = child.Parent;
		splitView.PerformDrop( other, dropInfo, mousePosition );
	}


	/// <summary>
	/// Adds the the second window as a tab at the end of the first window tab list
	/// </summary>
	/// <param name="existingWindow"></param>
	/// <param name="newWindow"></param>
	public static void AddTab( this EditorWindow existingWindow, EditorWindow newWindow )
	{
		var parent = new R_EditorWindow( existingWindow );
		var child = new R_EditorWindow( newWindow );
		var dockArea = new R_DockArea( parent.Parent );
		dockArea.OriginalDragSource = child.Parent;
		dockArea.AddTab( newWindow );
	}

	private static Vector2 GetFakeMousePosition( EditorWindow wnd, DockPosition position )
	{
		Vector2 mousePosition = Vector2.zero;

		// The 20 is required to make the docking work.
		// Smaller values might not work when faking the mouse position.
		switch ( position )
		{
			case DockPosition.Left:
			mousePosition = new Vector2( 20, wnd.position.size.y / 2 );
			break;
			case DockPosition.Top:
			mousePosition = new Vector2( wnd.position.size.x / 2, 20 );
			break;
			case DockPosition.Right:
			mousePosition = new Vector2( wnd.position.size.x - 20, wnd.position.size.y / 2 );
			break;
			case DockPosition.Bottom:
			mousePosition = new Vector2( wnd.position.size.x / 2, wnd.position.size.y - 20 );
			break;
		}

		return GUIUtility.GUIToScreenPoint( mousePosition );
	}
}
#endif