A game about forced loneliness, made by TACStudios
at master 235 lines 7.8 kB view raw
1using UnityEngine; 2using UnityEditor; 3using System.Collections; 4 5 6namespace TMPro.EditorUtilities 7{ 8 [CustomEditor(typeof(TextContainer)), CanEditMultipleObjects] 9 public class TMPro_TextContainerEditor : Editor 10 { 11 12 // Serialized Properties 13 private SerializedProperty anchorPosition_prop; 14 private SerializedProperty pivot_prop; 15 private SerializedProperty rectangle_prop; 16 private SerializedProperty margins_prop; 17 18 19 private TextContainer m_textContainer; 20 //private Transform m_transform; 21 //private Vector3[] m_Rect_handlePoints = new Vector3[4]; 22 //private Vector3[] m_Margin_handlePoints = new Vector3[4]; 23 24 //private Vector2 m_anchorPosition; 25 26 //private Vector3 m_mousePreviousPOS; 27 //private Vector2 m_previousStartPOS; 28 //private int m_mouseDragFlag = 0; 29 30 //private static Transform m_visualHelper; 31 32 33 void OnEnable() 34 { 35 36 // Serialized Properties 37 anchorPosition_prop = serializedObject.FindProperty("m_anchorPosition"); 38 pivot_prop = serializedObject.FindProperty("m_pivot"); 39 rectangle_prop = serializedObject.FindProperty("m_rect"); 40 margins_prop = serializedObject.FindProperty("m_margins"); 41 42 m_textContainer = (TextContainer)target; 43 //m_transform = m_textContainer.transform; 44 45 46 /* 47 if (m_visualHelper == null) 48 { 49 m_visualHelper = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform; 50 m_visualHelper.localScale = new Vector3(0.25f, 0.25f, 0.25f); 51 } 52 */ 53 } 54 55 void OnDisable() 56 { 57 /* 58 if (m_visualHelper != null) 59 DestroyImmediate (m_visualHelper.gameObject); 60 */ 61 } 62 63 64 65 66 public override void OnInspectorGUI() 67 { 68 serializedObject.Update(); 69 70 EditorGUI.BeginChangeCheck(); 71 EditorGUILayout.PropertyField(anchorPosition_prop); 72 if (anchorPosition_prop.enumValueIndex == 9) 73 { 74 EditorGUI.indentLevel += 1; 75 EditorGUILayout.PropertyField(pivot_prop, new GUIContent("Pivot Position")); 76 EditorGUI.indentLevel -= 1; 77 } 78 79 80 DrawDimensionProperty(rectangle_prop, "Dimensions"); 81 DrawMaginProperty(margins_prop, "Margins"); 82 if (EditorGUI.EndChangeCheck()) 83 { 84 // Re-compute pivot position when changes are made. 85 if (anchorPosition_prop.enumValueIndex != 9) 86 pivot_prop.vector2Value = GetAnchorPosition(anchorPosition_prop.enumValueIndex); 87 88 m_textContainer.hasChanged = true; 89 } 90 91 serializedObject.ApplyModifiedProperties(); 92 93 EditorGUILayout.Space(); 94 } 95 96 97 private void DrawDimensionProperty(SerializedProperty property, string label) 98 { 99 float old_LabelWidth = EditorGUIUtility.labelWidth; 100 float old_FieldWidth = EditorGUIUtility.fieldWidth; 101 102 Rect rect = EditorGUILayout.GetControlRect(false, 18); 103 Rect pos0 = new Rect(rect.x, rect.y + 2, rect.width, 18); 104 105 float width = rect.width + 3; 106 pos0.width = old_LabelWidth; 107 GUI.Label(pos0, label); 108 109 Rect rectangle = property.rectValue; 110 111 float width_B = width - old_LabelWidth; 112 float fieldWidth = width_B / 4; 113 pos0.width = fieldWidth - 5; 114 115 pos0.x = old_LabelWidth + 15; 116 GUI.Label(pos0, "Width"); 117 118 pos0.x += fieldWidth; 119 rectangle.width = EditorGUI.FloatField(pos0, GUIContent.none, rectangle.width); 120 121 pos0.x += fieldWidth; 122 GUI.Label(pos0, "Height"); 123 124 pos0.x += fieldWidth; 125 rectangle.height = EditorGUI.FloatField(pos0, GUIContent.none, rectangle.height); 126 127 property.rectValue = rectangle; 128 EditorGUIUtility.labelWidth = old_LabelWidth; 129 EditorGUIUtility.fieldWidth = old_FieldWidth; 130 } 131 132 133 private void DrawMaginProperty(SerializedProperty property, string label) 134 { 135 float old_LabelWidth = EditorGUIUtility.labelWidth; 136 float old_FieldWidth = EditorGUIUtility.fieldWidth; 137 138 Rect rect = EditorGUILayout.GetControlRect(false, 2 * 18); 139 Rect pos0 = new Rect(rect.x, rect.y + 2, rect.width, 18); 140 141 float width = rect.width + 3; 142 pos0.width = old_LabelWidth; 143 GUI.Label(pos0, label); 144 145 //Vector4 vec = property.vector4Value; 146 Vector4 vec = Vector4.zero; 147 vec.x = property.FindPropertyRelative("x").floatValue; 148 vec.y = property.FindPropertyRelative("y").floatValue; 149 vec.z = property.FindPropertyRelative("z").floatValue; 150 vec.w = property.FindPropertyRelative("w").floatValue; 151 152 153 float widthB = width - old_LabelWidth; 154 float fieldWidth = widthB / 4; 155 pos0.width = fieldWidth - 5; 156 157 // Labels 158 pos0.x = old_LabelWidth + 15; 159 GUI.Label(pos0, "Left"); 160 161 pos0.x += fieldWidth; 162 GUI.Label(pos0, "Top"); 163 164 pos0.x += fieldWidth; 165 GUI.Label(pos0, "Right"); 166 167 pos0.x += fieldWidth; 168 GUI.Label(pos0, "Bottom"); 169 170 pos0.y += 18; 171 172 pos0.x = old_LabelWidth + 15; 173 vec.x = EditorGUI.FloatField(pos0, GUIContent.none, vec.x); 174 175 pos0.x += fieldWidth; 176 vec.y = EditorGUI.FloatField(pos0, GUIContent.none, vec.y); 177 178 pos0.x += fieldWidth; 179 vec.z = EditorGUI.FloatField(pos0, GUIContent.none, vec.z); 180 181 pos0.x += fieldWidth; 182 vec.w = EditorGUI.FloatField(pos0, GUIContent.none, vec.w); 183 184 //property.vector4Value = vec; 185 property.FindPropertyRelative("x").floatValue = vec.x; 186 property.FindPropertyRelative("y").floatValue = vec.y; 187 property.FindPropertyRelative("z").floatValue = vec.z; 188 property.FindPropertyRelative("w").floatValue = vec.w; 189 190 EditorGUIUtility.labelWidth = old_LabelWidth; 191 EditorGUIUtility.fieldWidth = old_FieldWidth; 192 } 193 194 195 Vector2 GetAnchorPosition(int index) 196 { 197 Vector2 anchorPosition = Vector2.zero; 198 199 switch (index) 200 { 201 case 0: // TOP LEFT 202 anchorPosition = new Vector2(0, 1); 203 break; 204 case 1: // TOP 205 anchorPosition = new Vector2(0.5f, 1); 206 break; 207 case 2: // TOP RIGHT 208 anchorPosition = new Vector2(1, 1); 209 break; 210 case 3: // LEFT 211 anchorPosition = new Vector2(0, 0.5f); 212 break; 213 case 4: // MIDDLE 214 anchorPosition = new Vector2(0.5f, 0.5f); 215 break; 216 case 5: // RIGHT 217 anchorPosition = new Vector2(1, 0.5f); 218 break; 219 case 6: // BOTTOM LEFT 220 anchorPosition = new Vector2(0, 0); 221 break; 222 case 7: // BOTTOM 223 anchorPosition = new Vector2(0.5f, 0); 224 break; 225 case 8: // BOTTOM RIGHT 226 anchorPosition = new Vector2(1, 0); 227 break; 228 } 229 230 return anchorPosition; 231 } 232 233 234 } 235}