A game about forced loneliness, made by TACStudios
at master 127 lines 5.6 kB view raw
1using System.Collections.Generic; 2using System.Linq; 3using UnityEngine; 4using UnityEngine.UI; 5using System.Globalization; 6 7namespace UnityEditor.Events 8{ 9 [CustomPreview(typeof(GameObject))] 10 /// <summary> 11 /// Custom preview drawing that will draw the layout properties of a given object. 12 /// </summary> 13 class LayoutPropertiesPreview : ObjectPreview 14 { 15 private const float kLabelWidth = 110; 16 private const float kValueWidth = 100; 17 18 class Styles 19 { 20 public GUIStyle labelStyle = new GUIStyle(EditorStyles.label); 21 public GUIStyle headerStyle = new GUIStyle(EditorStyles.boldLabel); 22 23 public Styles() 24 { 25 labelStyle.padding.right += 4; 26 headerStyle.padding.right += 4; 27 } 28 } 29 30 private GUIContent m_Title; 31 private Styles m_Styles; 32 33 public override void Initialize(UnityEngine.Object[] targets) 34 { 35 base.Initialize(targets); 36 } 37 38 public override GUIContent GetPreviewTitle() 39 { 40 if (m_Title == null) 41 { 42 m_Title = EditorGUIUtility.TrTextContent("Layout Properties"); 43 } 44 return m_Title; 45 } 46 47 public override bool HasPreviewGUI() 48 { 49 GameObject go = target as GameObject; 50 if (!go) 51 return false; 52 53 // Prevent allocations in the editor by using TryGetComponent 54 ILayoutElement layoutElement; 55 return go.TryGetComponent(out layoutElement); 56 } 57 58 public override void OnPreviewGUI(Rect r, GUIStyle background) 59 { 60 if (Event.current.type != EventType.Repaint) 61 return; 62 63 if (m_Styles == null) 64 m_Styles = new Styles(); 65 66 GameObject go = target as GameObject; 67 RectTransform rect = go.transform as RectTransform; 68 if (rect == null) 69 return; 70 71 // Apply padding 72 RectOffset previewPadding = new RectOffset(-5, -5, -5, -5); 73 r = previewPadding.Add(r); 74 75 // Prepare rects for columns 76 r.height = EditorGUIUtility.singleLineHeight; 77 Rect labelRect = r; 78 Rect valueRect = r; 79 Rect sourceRect = r; 80 labelRect.width = kLabelWidth; 81 valueRect.xMin += kLabelWidth; 82 valueRect.width = kValueWidth; 83 sourceRect.xMin += kLabelWidth + kValueWidth; 84 85 // Headers 86 GUI.Label(labelRect, "Property", m_Styles.headerStyle); 87 GUI.Label(valueRect, "Value", m_Styles.headerStyle); 88 GUI.Label(sourceRect, "Source", m_Styles.headerStyle); 89 labelRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; 90 valueRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; 91 sourceRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; 92 93 // Prepare reusable variable for out argument 94 ILayoutElement source = null; 95 96 // Show properties 97 98 ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Min Width", LayoutUtility.GetLayoutProperty(rect, e => e.minWidth, 0, out source).ToString(CultureInfo.InvariantCulture.NumberFormat), source); 99 ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Min Height", LayoutUtility.GetLayoutProperty(rect, e => e.minHeight, 0, out source).ToString(CultureInfo.InvariantCulture.NumberFormat), source); 100 ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Preferred Width", LayoutUtility.GetLayoutProperty(rect, e => e.preferredWidth, 0, out source).ToString(CultureInfo.InvariantCulture.NumberFormat), source); 101 ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Preferred Height", LayoutUtility.GetLayoutProperty(rect, e => e.preferredHeight, 0, out source).ToString(CultureInfo.InvariantCulture.NumberFormat), source); 102 103 float flexible = 0; 104 105 flexible = LayoutUtility.GetLayoutProperty(rect, e => e.flexibleWidth, 0, out source); 106 ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Flexible Width", flexible > 0 ? ("enabled (" + flexible.ToString(CultureInfo.InvariantCulture.NumberFormat) + ")") : "disabled", source); 107 flexible = LayoutUtility.GetLayoutProperty(rect, e => e.flexibleHeight, 0, out source); 108 ShowProp(ref labelRect, ref valueRect, ref sourceRect, "Flexible Height", flexible > 0 ? ("enabled (" + flexible.ToString(CultureInfo.InvariantCulture.NumberFormat) + ")") : "disabled", source); 109 110 if (!rect.GetComponent<LayoutElement>()) 111 { 112 Rect noteRect = new Rect(labelRect.x, labelRect.y + 10, r.width, EditorGUIUtility.singleLineHeight); 113 GUI.Label(noteRect, "Add a LayoutElement to override values.", m_Styles.labelStyle); 114 } 115 } 116 117 private void ShowProp(ref Rect labelRect, ref Rect valueRect, ref Rect sourceRect, string label, string value, ILayoutElement source) 118 { 119 GUI.Label(labelRect, label, m_Styles.labelStyle); 120 GUI.Label(valueRect, value, m_Styles.labelStyle); 121 GUI.Label(sourceRect, source == null ? "none" : source.GetType().Name, m_Styles.labelStyle); 122 labelRect.y += EditorGUIUtility.singleLineHeight; 123 valueRect.y += EditorGUIUtility.singleLineHeight; 124 sourceRect.y += EditorGUIUtility.singleLineHeight; 125 } 126 } 127}