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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
// 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.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; } }
}
}
|