| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Reflection; |
| | 5 | | using UnityEditor; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace ReorderableList.Editor |
| | 9 | | { |
| | 10 | |
|
| | 11 | | public class ReorderableList |
| | 12 | | { |
| | 13 | |
|
| | 14 | | private const float ELEMENT_EDGE_TOP = 1; |
| | 15 | | private const float ELEMENT_EDGE_BOT = 3; |
| | 16 | | private const float ELEMENT_HEIGHT_OFFSET = ELEMENT_EDGE_TOP + ELEMENT_EDGE_BOT; |
| | 17 | |
|
| 0 | 18 | | private static int selectionHash = "ReorderableListSelection".GetHashCode(); |
| 0 | 19 | | private static int dragAndDropHash = "ReorderableListDragAndDrop".GetHashCode(); |
| | 20 | |
|
| | 21 | | private const string EMPTY_LABEL = "List is Empty"; |
| | 22 | | private const string ARRAY_ERROR = "{0} is not an Array!"; |
| | 23 | |
|
| | 24 | | public enum ElementDisplayType |
| | 25 | | { |
| | 26 | | Auto, |
| | 27 | | Expandable, |
| | 28 | | SingleLine |
| | 29 | | } |
| | 30 | |
|
| | 31 | | public delegate void DrawHeaderDelegate(Rect rect, GUIContent label); |
| | 32 | | public delegate void DrawFooterDelegate(Rect rect); |
| | 33 | | public delegate void DrawElementDelegate(Rect rect, SerializedProperty element, GUIContent label, bool selected, |
| | 34 | | public delegate void ActionDelegate(ReorderableList list); |
| | 35 | | public delegate bool ActionBoolDelegate(ReorderableList list); |
| | 36 | | public delegate void AddDropdownDelegate(Rect buttonRect, ReorderableList list); |
| | 37 | | public delegate Object DragDropReferenceDelegate(Object[] references, ReorderableList list); |
| | 38 | | public delegate void DragDropAppendDelegate(Object reference, ReorderableList list); |
| | 39 | | public delegate float GetElementHeightDelegate(SerializedProperty element); |
| | 40 | | public delegate float GetElementsHeightDelegate(ReorderableList list); |
| | 41 | | public delegate string GetElementNameDelegate(SerializedProperty element); |
| | 42 | | public delegate GUIContent GetElementLabelDelegate(SerializedProperty element); |
| | 43 | | public delegate void SurrogateCallback(SerializedProperty element, Object objectReference, ReorderableList list) |
| | 44 | |
|
| | 45 | | public event DrawHeaderDelegate drawHeaderCallback; |
| | 46 | | public event DrawFooterDelegate drawFooterCallback; |
| | 47 | | public event DrawElementDelegate drawElementCallback; |
| | 48 | | public event DrawElementDelegate drawElementBackgroundCallback; |
| | 49 | | public event GetElementHeightDelegate getElementHeightCallback; |
| | 50 | | public event GetElementsHeightDelegate getElementsHeightCallback; |
| | 51 | | public event GetElementNameDelegate getElementNameCallback; |
| | 52 | | public event GetElementLabelDelegate getElementLabelCallback; |
| | 53 | | public event DragDropReferenceDelegate onValidateDragAndDropCallback; |
| | 54 | | public event DragDropAppendDelegate onAppendDragDropCallback; |
| | 55 | | public event ActionDelegate onReorderCallback; |
| | 56 | | public event ActionDelegate onSelectCallback; |
| | 57 | | public event ActionDelegate onAddCallback; |
| | 58 | | public event AddDropdownDelegate onAddDropdownCallback; |
| | 59 | | public event ActionDelegate onRemoveCallback; |
| | 60 | | public event ActionDelegate onMouseUpCallback; |
| | 61 | | public event ActionBoolDelegate onCanRemoveCallback; |
| | 62 | | public event ActionDelegate onChangedCallback; |
| | 63 | |
|
| | 64 | | public bool canAdd; |
| | 65 | | public bool canRemove; |
| | 66 | | public bool draggable; |
| | 67 | | public bool sortable; |
| | 68 | | public bool expandable; |
| | 69 | | public bool multipleSelection; |
| | 70 | | public GUIContent label; |
| | 71 | | public float headerHeight; |
| | 72 | | public float footerHeight; |
| | 73 | | public float slideEasing; |
| | 74 | | public float verticalSpacing; |
| | 75 | | public bool showDefaultBackground; |
| | 76 | | public ElementDisplayType elementDisplayType; |
| | 77 | | public string elementNameProperty; |
| | 78 | | public string elementNameOverride; |
| | 79 | | public bool elementLabels; |
| | 80 | | public Texture elementIcon; |
| | 81 | | public Surrogate surrogate; |
| | 82 | |
|
| 0 | 83 | | public bool paginate { get { return pagination.enabled; } set { pagination.enabled = value; } } |
| | 84 | |
|
| 0 | 85 | | public int pageSize { get { return pagination.fixedPageSize; } set { pagination.fixedPageSize = value; } } |
| | 86 | |
|
| | 87 | | internal readonly int id; |
| | 88 | |
|
| | 89 | | private SerializedProperty list; |
| 0 | 90 | | private int controlID = -1; |
| | 91 | | private Rect[] elementRects; |
| | 92 | | private GUIContent elementLabel; |
| | 93 | | private GUIContent pageInfoContent; |
| | 94 | | private GUIContent pageSizeContent; |
| | 95 | | private ListSelection selection; |
| | 96 | | private SlideGroup slideGroup; |
| | 97 | | private int pressIndex; |
| | 98 | |
|
| 0 | 99 | | private bool doPagination { get { return pagination.enabled && !list.serializedObject.isEditingMultipleObjects; |
| | 100 | |
|
| 0 | 101 | | private float elementSpacing { get { return Mathf.Max(0, verticalSpacing - 2); } } |
| | 102 | |
|
| | 103 | | private bool dragging; |
| | 104 | | private float pressPosition; |
| | 105 | | private float dragPosition; |
| | 106 | | private int dragDirection; |
| | 107 | | private DragList dragList; |
| | 108 | | private ListSelection beforeDragSelection; |
| | 109 | | private Pagination pagination; |
| | 110 | |
|
| 0 | 111 | | private int dragDropControlID = -1; |
| | 112 | |
|
| | 113 | | public ReorderableList(SerializedProperty list) |
| 0 | 114 | | : this(list, true, true, true) { } |
| | 115 | |
|
| | 116 | | public ReorderableList(SerializedProperty list, bool canAdd, bool canRemove, bool draggable) |
| 0 | 117 | | : this(list, canAdd, canRemove, draggable, ElementDisplayType.Auto, null, null, null) { } |
| | 118 | |
|
| | 119 | | public ReorderableList(SerializedProperty list, bool canAdd, bool canRemove, bool draggable, ElementDisplayType |
| 0 | 120 | | : this(list, canAdd, canRemove, draggable, elementDisplayType, elementNameProperty, null, elementIcon) { } |
| | 121 | |
|
| 0 | 122 | | public ReorderableList(SerializedProperty list, bool canAdd, bool canRemove, bool draggable, ElementDisplayType |
| | 123 | | { |
| | 124 | |
|
| 0 | 125 | | if (list == null) |
| | 126 | | { |
| | 127 | |
|
| 0 | 128 | | throw new MissingListExeption(); |
| | 129 | | } |
| 0 | 130 | | else if (!list.isArray) |
| | 131 | | { |
| | 132 | |
|
| | 133 | | //check if user passed in a ReorderableArray, if so, that becomes the list object |
| | 134 | |
|
| 0 | 135 | | SerializedProperty array = list.FindPropertyRelative("array"); |
| | 136 | |
|
| 0 | 137 | | if (array == null || !array.isArray) |
| | 138 | | { |
| | 139 | |
|
| 0 | 140 | | throw new InvalidListException(); |
| | 141 | | } |
| | 142 | |
|
| 0 | 143 | | this.list = array; |
| 0 | 144 | | } |
| | 145 | | else |
| | 146 | | { |
| | 147 | |
|
| 0 | 148 | | this.list = list; |
| | 149 | | } |
| | 150 | |
|
| 0 | 151 | | this.canAdd = canAdd; |
| 0 | 152 | | this.canRemove = canRemove; |
| 0 | 153 | | this.draggable = draggable; |
| 0 | 154 | | this.elementDisplayType = elementDisplayType; |
| 0 | 155 | | this.elementNameProperty = elementNameProperty; |
| 0 | 156 | | this.elementNameOverride = elementNameOverride; |
| 0 | 157 | | this.elementIcon = elementIcon; |
| | 158 | |
|
| 0 | 159 | | id = GetHashCode(); |
| 0 | 160 | | list.isExpanded = true; |
| 0 | 161 | | label = new GUIContent(list.displayName); |
| 0 | 162 | | pageInfoContent = new GUIContent(); |
| 0 | 163 | | pageSizeContent = new GUIContent(); |
| | 164 | |
|
| | 165 | | #if UNITY_5_6_OR_NEWER |
| 0 | 166 | | verticalSpacing = EditorGUIUtility.standardVerticalSpacing; |
| | 167 | | #else |
| | 168 | | verticalSpacing = 2f; |
| | 169 | | #endif |
| 0 | 170 | | headerHeight = 18f; |
| 0 | 171 | | footerHeight = 13f; |
| 0 | 172 | | slideEasing = 0.15f; |
| 0 | 173 | | expandable = true; |
| 0 | 174 | | elementLabels = true; |
| 0 | 175 | | showDefaultBackground = true; |
| 0 | 176 | | multipleSelection = true; |
| 0 | 177 | | pagination = new Pagination(); |
| 0 | 178 | | elementLabel = new GUIContent(); |
| | 179 | |
|
| 0 | 180 | | dragList = new DragList(0); |
| 0 | 181 | | selection = new ListSelection(); |
| 0 | 182 | | slideGroup = new SlideGroup(); |
| 0 | 183 | | elementRects = new Rect[0]; |
| 0 | 184 | | } |
| | 185 | |
|
| | 186 | | // |
| | 187 | | // -- PROPERTIES -- |
| | 188 | | // |
| | 189 | |
|
| 0 | 190 | | public SerializedProperty List { get { return list; } internal set { list = value; } } |
| | 191 | |
|
| 0 | 192 | | public bool HasList { get { return list != null && list.isArray; } } |
| | 193 | |
|
| | 194 | | public int Length |
| | 195 | | { |
| | 196 | | get |
| | 197 | | { |
| | 198 | |
|
| 0 | 199 | | if (!HasList) |
| | 200 | | { |
| | 201 | |
|
| 0 | 202 | | return 0; |
| | 203 | | } |
| 0 | 204 | | else if (!list.hasMultipleDifferentValues) |
| | 205 | | { |
| | 206 | |
|
| 0 | 207 | | return list.arraySize; |
| | 208 | | } |
| | 209 | |
|
| | 210 | | //When multiple objects are selected, because of a Unity bug, list.arraySize is never guranteed to actua |
| | 211 | | //array size. So we have to find it. Not that great since we're creating SerializedObjects here. There h |
| | 212 | |
|
| 0 | 213 | | int smallerArraySize = list.arraySize; |
| | 214 | |
|
| 0 | 215 | | foreach (Object targetObject in list.serializedObject.targetObjects) |
| | 216 | | { |
| | 217 | |
|
| 0 | 218 | | SerializedObject serializedObject = new SerializedObject(targetObject); |
| 0 | 219 | | SerializedProperty property = serializedObject.FindProperty(list.propertyPath); |
| | 220 | |
|
| 0 | 221 | | smallerArraySize = Mathf.Min(property.arraySize, smallerArraySize); |
| | 222 | | } |
| | 223 | |
|
| 0 | 224 | | return smallerArraySize; |
| | 225 | | } |
| | 226 | | } |
| | 227 | |
|
| 0 | 228 | | public int VisibleLength { get { return pagination.GetVisibleLength(Length); } } |
| | 229 | |
|
| 0 | 230 | | public int[] Selected { get { return selection.ToArray(); } set { selection = new ListSelection(value); } } |
| | 231 | |
|
| 0 | 232 | | public int Index { get { return selection.First; } set { selection.Select(value); } } |
| | 233 | |
|
| 0 | 234 | | public bool IsDragging { get { return dragging; } } |
| | 235 | |
|
| | 236 | | // |
| | 237 | | // -- PUBLIC -- |
| | 238 | | // |
| | 239 | |
|
| | 240 | | public float GetHeight() |
| | 241 | | { |
| | 242 | |
|
| 0 | 243 | | if (HasList) |
| | 244 | | { |
| | 245 | |
|
| 0 | 246 | | float topHeight = doPagination ? headerHeight * 2 : headerHeight; |
| | 247 | |
|
| 0 | 248 | | return list.isExpanded ? topHeight + GetElementsHeight() + footerHeight : headerHeight; |
| | 249 | | } |
| | 250 | | else |
| | 251 | | { |
| | 252 | |
|
| 0 | 253 | | return EditorGUIUtility.singleLineHeight; |
| | 254 | | } |
| | 255 | | } |
| | 256 | |
|
| | 257 | | public void DoLayoutList() |
| | 258 | | { |
| | 259 | |
|
| 0 | 260 | | Rect position = EditorGUILayout.GetControlRect(false, GetHeight(), EditorStyles.largeLabel); |
| | 261 | |
|
| 0 | 262 | | DoList(EditorGUI.IndentedRect(position), label); |
| 0 | 263 | | } |
| | 264 | |
|
| | 265 | | public void DoList(Rect rect, GUIContent label) |
| | 266 | | { |
| | 267 | |
|
| 0 | 268 | | int indent = EditorGUI.indentLevel; |
| 0 | 269 | | EditorGUI.indentLevel = 0; |
| | 270 | |
|
| 0 | 271 | | Rect headerRect = rect; |
| 0 | 272 | | headerRect.height = headerHeight; |
| | 273 | |
|
| 0 | 274 | | if (!HasList) |
| | 275 | | { |
| | 276 | |
|
| 0 | 277 | | DrawEmpty(headerRect, string.Format(ARRAY_ERROR, label.text), GUIStyle.none, EditorStyles.helpBox); |
| 0 | 278 | | } |
| | 279 | | else |
| | 280 | | { |
| | 281 | |
|
| 0 | 282 | | controlID = GUIUtility.GetControlID(selectionHash, FocusType.Keyboard, rect); |
| 0 | 283 | | dragDropControlID = GUIUtility.GetControlID(dragAndDropHash, FocusType.Passive, rect); |
| | 284 | |
|
| 0 | 285 | | DrawHeader(headerRect, label); |
| | 286 | |
|
| 0 | 287 | | if (list.isExpanded) |
| | 288 | | { |
| | 289 | |
|
| 0 | 290 | | if (doPagination) |
| | 291 | | { |
| | 292 | |
|
| 0 | 293 | | Rect paginateHeaderRect = headerRect; |
| 0 | 294 | | paginateHeaderRect.y += headerRect.height; |
| | 295 | |
|
| 0 | 296 | | DrawPaginationHeader(paginateHeaderRect); |
| | 297 | |
|
| 0 | 298 | | headerRect.yMax = paginateHeaderRect.yMax - 1; |
| | 299 | | } |
| | 300 | |
|
| 0 | 301 | | Rect elementBackgroundRect = rect; |
| 0 | 302 | | elementBackgroundRect.yMin = headerRect.yMax; |
| 0 | 303 | | elementBackgroundRect.yMax = rect.yMax - footerHeight; |
| | 304 | |
|
| 0 | 305 | | Event evt = Event.current; |
| | 306 | |
|
| 0 | 307 | | if (selection.Length > 1) |
| | 308 | | { |
| | 309 | |
|
| 0 | 310 | | if (evt.type == EventType.ContextClick && CanSelect(evt.mousePosition)) |
| | 311 | | { |
| | 312 | |
|
| 0 | 313 | | HandleMultipleContextClick(evt); |
| | 314 | | } |
| | 315 | | } |
| | 316 | |
|
| 0 | 317 | | if (Length > 0) |
| | 318 | | { |
| | 319 | |
|
| | 320 | | //update element rects if not dragging. Dragging caches draw rects so no need to update |
| | 321 | |
|
| 0 | 322 | | if (!dragging) |
| | 323 | | { |
| | 324 | |
|
| 0 | 325 | | UpdateElementRects(elementBackgroundRect, evt); |
| | 326 | | } |
| | 327 | |
|
| 0 | 328 | | if (elementRects.Length > 0) |
| | 329 | | { |
| | 330 | |
|
| | 331 | | int start, end; |
| | 332 | |
|
| 0 | 333 | | pagination.GetVisibleRange(elementRects.Length, out start, out end); |
| | 334 | |
|
| 0 | 335 | | Rect selectableRect = elementBackgroundRect; |
| 0 | 336 | | selectableRect.yMin = elementRects[start].yMin; |
| 0 | 337 | | selectableRect.yMax = elementRects[end - 1].yMax; |
| | 338 | |
|
| 0 | 339 | | HandlePreSelection(selectableRect, evt); |
| 0 | 340 | | DrawElements(elementBackgroundRect, evt); |
| 0 | 341 | | HandlePostSelection(selectableRect, evt); |
| | 342 | | } |
| 0 | 343 | | } |
| | 344 | | else |
| | 345 | | { |
| | 346 | |
|
| 0 | 347 | | DrawEmpty(elementBackgroundRect, EMPTY_LABEL, Style.boxBackground, Style.verticalLabel); |
| | 348 | | } |
| | 349 | |
|
| 0 | 350 | | Rect footerRect = rect; |
| 0 | 351 | | footerRect.yMin = elementBackgroundRect.yMax; |
| 0 | 352 | | footerRect.xMin = rect.xMax - 58; |
| | 353 | |
|
| 0 | 354 | | DrawFooter(footerRect); |
| | 355 | | } |
| | 356 | | } |
| | 357 | |
|
| 0 | 358 | | EditorGUI.indentLevel = indent; |
| 0 | 359 | | } |
| | 360 | |
|
| | 361 | | public SerializedProperty AddItem<T>(T item) where T : Object |
| | 362 | | { |
| | 363 | |
|
| 0 | 364 | | SerializedProperty property = AddItem(); |
| | 365 | |
|
| 0 | 366 | | if (property != null) |
| | 367 | | { |
| | 368 | |
|
| 0 | 369 | | property.objectReferenceValue = item; |
| | 370 | | } |
| | 371 | |
|
| 0 | 372 | | return property; |
| | 373 | | } |
| | 374 | |
|
| | 375 | | public SerializedProperty AddItem() |
| | 376 | | { |
| | 377 | |
|
| 0 | 378 | | if (HasList) |
| | 379 | | { |
| | 380 | |
|
| | 381 | | //TODO Validate add on multiple selected objects |
| | 382 | |
|
| 0 | 383 | | list.arraySize++; |
| 0 | 384 | | selection.Select(list.arraySize - 1); |
| | 385 | |
|
| 0 | 386 | | SetPageByIndex(list.arraySize - 1); |
| 0 | 387 | | DispatchChange(); |
| | 388 | |
|
| 0 | 389 | | return list.GetArrayElementAtIndex(selection.Last); |
| | 390 | | } |
| | 391 | | else |
| | 392 | | { |
| | 393 | |
|
| 0 | 394 | | throw new InvalidListException(); |
| | 395 | | } |
| | 396 | | } |
| | 397 | |
|
| | 398 | | public void Remove(int[] selection) |
| | 399 | | { |
| | 400 | |
|
| 0 | 401 | | System.Array.Sort(selection); |
| | 402 | |
|
| 0 | 403 | | int i = selection.Length; |
| | 404 | |
|
| 0 | 405 | | while (--i > -1) |
| | 406 | | { |
| | 407 | |
|
| 0 | 408 | | RemoveItem(selection[i]); |
| | 409 | | } |
| 0 | 410 | | } |
| | 411 | |
|
| | 412 | | public void RemoveItem(int index) |
| | 413 | | { |
| | 414 | |
|
| 0 | 415 | | if (index >= 0 && index < Length) |
| | 416 | | { |
| | 417 | |
|
| 0 | 418 | | SerializedProperty property = list.GetArrayElementAtIndex(index); |
| | 419 | |
|
| 0 | 420 | | if (property.propertyType == SerializedPropertyType.ObjectReference && property.objectReferenceValue) |
| | 421 | | { |
| | 422 | |
|
| 0 | 423 | | property.objectReferenceValue = null; |
| | 424 | | } |
| | 425 | |
|
| 0 | 426 | | list.DeleteArrayElementAtIndex(index); |
| 0 | 427 | | selection.Remove(index); |
| | 428 | |
|
| | 429 | | //TODO Validate removal on multiple selected objects |
| | 430 | |
|
| 0 | 431 | | if (Length > 0) |
| | 432 | | { |
| | 433 | |
|
| 0 | 434 | | selection.Select(Mathf.Max(0, index - 1)); |
| | 435 | | } |
| | 436 | |
|
| 0 | 437 | | DispatchChange(); |
| | 438 | | } |
| 0 | 439 | | } |
| | 440 | |
|
| | 441 | | public SerializedProperty GetItem(int index) |
| | 442 | | { |
| | 443 | |
|
| 0 | 444 | | if (index >= 0 && index < Length) |
| | 445 | | { |
| | 446 | |
|
| 0 | 447 | | return list.GetArrayElementAtIndex(index); |
| | 448 | | } |
| | 449 | | else |
| | 450 | | { |
| | 451 | |
|
| 0 | 452 | | return null; |
| | 453 | | } |
| | 454 | | } |
| | 455 | |
|
| | 456 | | public int IndexOf(SerializedProperty element) |
| | 457 | | { |
| | 458 | |
|
| 0 | 459 | | if (element != null) |
| | 460 | | { |
| | 461 | |
|
| 0 | 462 | | int i = Length; |
| | 463 | |
|
| 0 | 464 | | while (--i > -1) |
| | 465 | | { |
| | 466 | |
|
| 0 | 467 | | if (SerializedProperty.EqualContents(element, list.GetArrayElementAtIndex(i))) |
| | 468 | | { |
| | 469 | |
|
| 0 | 470 | | return i; |
| | 471 | | } |
| | 472 | | } |
| | 473 | | } |
| | 474 | |
|
| 0 | 475 | | return -1; |
| | 476 | | } |
| | 477 | |
|
| 0 | 478 | | public void GrabKeyboardFocus() { GUIUtility.keyboardControl = id; } |
| | 479 | |
|
| 0 | 480 | | public bool HasKeyboardControl() { return GUIUtility.keyboardControl == id; } |
| | 481 | |
|
| | 482 | | public void ReleaseKeyboardFocus() |
| | 483 | | { |
| | 484 | |
|
| 0 | 485 | | if (GUIUtility.keyboardControl == id) |
| | 486 | | { |
| | 487 | |
|
| 0 | 488 | | GUIUtility.keyboardControl = 0; |
| | 489 | | } |
| 0 | 490 | | } |
| | 491 | |
|
| | 492 | | public void SetPage(int page) |
| | 493 | | { |
| | 494 | |
|
| 0 | 495 | | if (doPagination) |
| | 496 | | { |
| | 497 | |
|
| 0 | 498 | | pagination.page = page; |
| | 499 | | } |
| 0 | 500 | | } |
| | 501 | |
|
| | 502 | | public void SetPageByIndex(int index) |
| | 503 | | { |
| | 504 | |
|
| 0 | 505 | | if (doPagination) |
| | 506 | | { |
| | 507 | |
|
| 0 | 508 | | pagination.page = pagination.GetPageForIndex(index); |
| | 509 | | } |
| 0 | 510 | | } |
| | 511 | |
|
| 0 | 512 | | public int GetPage(int index) { return doPagination ? pagination.page : 0; } |
| | 513 | |
|
| 0 | 514 | | public int GetPageByIndex(int index) { return doPagination ? pagination.GetPageForIndex(index) : 0; } |
| | 515 | |
|
| | 516 | | // |
| | 517 | | // -- PRIVATE -- |
| | 518 | | // |
| | 519 | |
|
| | 520 | | private float GetElementsHeight() |
| | 521 | | { |
| | 522 | |
|
| 0 | 523 | | if (getElementsHeightCallback != null) |
| | 524 | | { |
| | 525 | |
|
| 0 | 526 | | return getElementsHeightCallback(this); |
| | 527 | | } |
| | 528 | |
|
| 0 | 529 | | int i, len = Length; |
| | 530 | |
|
| 0 | 531 | | if (len == 0) |
| | 532 | | { |
| | 533 | |
|
| 0 | 534 | | return 28; |
| | 535 | | } |
| | 536 | |
|
| 0 | 537 | | float totalHeight = 0; |
| 0 | 538 | | float spacing = elementSpacing; |
| | 539 | |
|
| | 540 | | int start, end; |
| | 541 | |
|
| 0 | 542 | | pagination.GetVisibleRange(len, out start, out end); |
| | 543 | |
|
| 0 | 544 | | for (i = start; i < end; i++) |
| | 545 | | { |
| | 546 | |
|
| 0 | 547 | | totalHeight += GetElementHeight(list.GetArrayElementAtIndex(i)) + spacing; |
| | 548 | | } |
| | 549 | |
|
| 0 | 550 | | return totalHeight + 7 - spacing; |
| | 551 | | } |
| | 552 | |
|
| | 553 | | private float GetElementHeight(SerializedProperty element) |
| | 554 | | { |
| | 555 | |
|
| 0 | 556 | | if (getElementHeightCallback != null) |
| | 557 | | { |
| | 558 | |
|
| 0 | 559 | | return getElementHeightCallback(element) + ELEMENT_HEIGHT_OFFSET; |
| | 560 | | } |
| | 561 | | else |
| | 562 | | { |
| | 563 | |
|
| 0 | 564 | | return EditorGUI.GetPropertyHeight(element, GetElementLabel(element, elementLabels), IsElementExpandable |
| | 565 | | } |
| | 566 | | } |
| | 567 | |
|
| | 568 | | private Rect GetElementDrawRect(int index, Rect desiredRect) |
| | 569 | | { |
| | 570 | |
|
| 0 | 571 | | if (slideEasing <= 0) |
| | 572 | | { |
| | 573 | |
|
| 0 | 574 | | return desiredRect; |
| | 575 | | } |
| | 576 | | else |
| | 577 | | { |
| | 578 | |
|
| | 579 | | //lerp the drag easing toward slide easing, this creates a stronger easing at the start then slower at t |
| | 580 | | //when dealing with large lists, we can |
| | 581 | |
|
| 0 | 582 | | return dragging ? slideGroup.GetRect(dragList[index].startIndex, desiredRect, slideEasing) : slideGroup. |
| | 583 | | } |
| | 584 | | } |
| | 585 | |
|
| | 586 | | /* |
| | 587 | | private Rect GetElementHeaderRect(SerializedProperty element, Rect elementRect) { |
| | 588 | |
|
| | 589 | | Rect rect = elementRect; |
| | 590 | | rect.height = EditorGUIUtility.singleLineHeight + verticalSpacing; |
| | 591 | |
|
| | 592 | | return rect; |
| | 593 | | } |
| | 594 | | */ |
| | 595 | |
|
| | 596 | | private Rect GetElementRenderRect(SerializedProperty element, Rect elementRect) |
| | 597 | | { |
| | 598 | |
|
| 0 | 599 | | float offset = draggable ? 20 : 5; |
| | 600 | |
|
| 0 | 601 | | Rect rect = elementRect; |
| 0 | 602 | | rect.xMin += IsElementExpandable(element) ? offset + 10 : offset; |
| 0 | 603 | | rect.xMax -= 5; |
| 0 | 604 | | rect.yMin += ELEMENT_EDGE_TOP; |
| 0 | 605 | | rect.yMax -= ELEMENT_EDGE_BOT; |
| | 606 | |
|
| 0 | 607 | | return rect; |
| | 608 | | } |
| | 609 | |
|
| | 610 | | private void DrawHeader(Rect rect, GUIContent label) |
| | 611 | | { |
| | 612 | |
|
| 0 | 613 | | if (showDefaultBackground && Event.current.type == EventType.Repaint) |
| | 614 | | { |
| | 615 | |
|
| 0 | 616 | | Style.headerBackground.Draw(rect, false, false, false, false); |
| | 617 | | } |
| | 618 | |
|
| 0 | 619 | | HandleDragAndDrop(rect, Event.current); |
| | 620 | |
|
| 0 | 621 | | bool multiline = elementDisplayType != ElementDisplayType.SingleLine; |
| | 622 | |
|
| 0 | 623 | | Rect titleRect = rect; |
| 0 | 624 | | titleRect.xMin += 6f; |
| 0 | 625 | | titleRect.xMax -= multiline ? 95f : 55f; |
| 0 | 626 | | titleRect.height -= 2f; |
| 0 | 627 | | titleRect.y++; |
| | 628 | |
|
| 0 | 629 | | label = EditorGUI.BeginProperty(titleRect, label, list); |
| | 630 | |
|
| 0 | 631 | | if (drawHeaderCallback != null) |
| | 632 | | { |
| | 633 | |
|
| 0 | 634 | | drawHeaderCallback(titleRect, label); |
| 0 | 635 | | } |
| 0 | 636 | | else if (expandable) |
| | 637 | | { |
| | 638 | |
|
| 0 | 639 | | titleRect.xMin += 10; |
| | 640 | |
|
| 0 | 641 | | EditorGUI.BeginChangeCheck(); |
| | 642 | |
|
| 0 | 643 | | bool isExpanded = EditorGUI.Foldout(titleRect, list.isExpanded, label, true); |
| | 644 | |
|
| 0 | 645 | | if (EditorGUI.EndChangeCheck()) |
| | 646 | | { |
| | 647 | |
|
| 0 | 648 | | list.isExpanded = isExpanded; |
| | 649 | | } |
| 0 | 650 | | } |
| | 651 | | else |
| | 652 | | { |
| | 653 | |
|
| 0 | 654 | | GUI.Label(titleRect, label, EditorStyles.label); |
| | 655 | | } |
| | 656 | |
|
| 0 | 657 | | EditorGUI.EndProperty(); |
| | 658 | |
|
| 0 | 659 | | if (multiline) |
| | 660 | | { |
| | 661 | |
|
| 0 | 662 | | Rect bRect1 = rect; |
| 0 | 663 | | bRect1.xMin = rect.xMax - 25; |
| 0 | 664 | | bRect1.xMax = rect.xMax - 5; |
| | 665 | |
|
| 0 | 666 | | if (GUI.Button(bRect1, Style.expandButton, Style.preButton)) |
| | 667 | | { |
| | 668 | |
|
| 0 | 669 | | ExpandElements(true); |
| | 670 | | } |
| | 671 | |
|
| 0 | 672 | | Rect bRect2 = rect; |
| 0 | 673 | | bRect2.xMin = bRect1.xMin - 20; |
| 0 | 674 | | bRect2.xMax = bRect1.xMin; |
| | 675 | |
|
| 0 | 676 | | if (GUI.Button(bRect2, Style.collapseButton, Style.preButton)) |
| | 677 | | { |
| | 678 | |
|
| 0 | 679 | | ExpandElements(false); |
| | 680 | | } |
| | 681 | |
|
| 0 | 682 | | rect.xMax = bRect2.xMin + 5; |
| | 683 | | } |
| | 684 | |
|
| | 685 | | //draw sorting options |
| | 686 | |
|
| 0 | 687 | | if (sortable) |
| | 688 | | { |
| | 689 | |
|
| 0 | 690 | | Rect sortRect1 = rect; |
| 0 | 691 | | sortRect1.xMin = rect.xMax - 25; |
| 0 | 692 | | sortRect1.xMax = rect.xMax; |
| | 693 | |
|
| 0 | 694 | | Rect sortRect2 = rect; |
| 0 | 695 | | sortRect2.xMin = sortRect1.xMin - 20; |
| 0 | 696 | | sortRect2.xMax = sortRect1.xMin; |
| | 697 | |
|
| 0 | 698 | | if (EditorGUI.DropdownButton(sortRect1, Style.sortAscending, FocusType.Passive, Style.preButton)) |
| | 699 | | { |
| | 700 | |
|
| 0 | 701 | | SortElements(sortRect1, false); |
| | 702 | | } |
| | 703 | |
|
| 0 | 704 | | if (EditorGUI.DropdownButton(sortRect2, Style.sortDescending, FocusType.Passive, Style.preButton)) |
| | 705 | | { |
| | 706 | |
|
| 0 | 707 | | SortElements(sortRect2, true); |
| | 708 | | } |
| | 709 | | } |
| 0 | 710 | | } |
| | 711 | |
|
| | 712 | | private void ExpandElements(bool expand) |
| | 713 | | { |
| | 714 | |
|
| 0 | 715 | | if (!list.isExpanded && expand) |
| | 716 | | { |
| | 717 | |
|
| 0 | 718 | | list.isExpanded = true; |
| | 719 | | } |
| | 720 | |
|
| 0 | 721 | | int i, len = Length; |
| | 722 | |
|
| 0 | 723 | | for (i = 0; i < len; i++) |
| | 724 | | { |
| | 725 | |
|
| 0 | 726 | | list.GetArrayElementAtIndex(i).isExpanded = expand; |
| | 727 | | } |
| 0 | 728 | | } |
| | 729 | |
|
| | 730 | | private void SortElements(Rect rect, bool descending) |
| | 731 | | { |
| | 732 | |
|
| 0 | 733 | | int total = Length; |
| | 734 | |
|
| | 735 | | //no point in sorting a list with 1 element! |
| | 736 | |
|
| 0 | 737 | | if (total <= 1) |
| | 738 | | { |
| | 739 | |
|
| 0 | 740 | | return; |
| | 741 | | } |
| | 742 | |
|
| | 743 | | //the first property tells us what type of items are in the list |
| | 744 | | //if generic, then we give the user a list of properties to sort on |
| | 745 | |
|
| 0 | 746 | | SerializedProperty prop = list.GetArrayElementAtIndex(0); |
| | 747 | |
|
| 0 | 748 | | if (prop.propertyType == SerializedPropertyType.Generic) |
| | 749 | | { |
| | 750 | |
|
| 0 | 751 | | GenericMenu menu = new GenericMenu(); |
| | 752 | |
|
| 0 | 753 | | SerializedProperty property = prop.Copy(); |
| 0 | 754 | | SerializedProperty end = property.GetEndProperty(); |
| | 755 | |
|
| 0 | 756 | | bool enterChildren = true; |
| | 757 | |
|
| 0 | 758 | | while (property.NextVisible(enterChildren) && !SerializedProperty.EqualContents(property, end)) |
| | 759 | | { |
| | 760 | |
|
| 0 | 761 | | menu.AddItem(new GUIContent(property.name), false, userData => |
| | 762 | | { |
| | 763 | |
|
| | 764 | | //sort based on the property selected then apply the changes |
| | 765 | |
|
| 0 | 766 | | ListSort.SortOnProperty(list, total, descending, (string)userData); |
| | 767 | |
|
| 0 | 768 | | ApplyReorder(); |
| | 769 | |
|
| 0 | 770 | | HandleUtility.Repaint(); |
| | 771 | |
|
| 0 | 772 | | }, property.name); |
| | 773 | |
|
| 0 | 774 | | enterChildren = false; |
| | 775 | | } |
| | 776 | |
|
| 0 | 777 | | menu.DropDown(rect); |
| 0 | 778 | | } |
| | 779 | | else |
| | 780 | | { |
| | 781 | |
|
| | 782 | | //list is not generic, so we just sort directly on the type then apply the changes |
| | 783 | |
|
| 0 | 784 | | ListSort.SortOnType(list, total, descending, prop.propertyType); |
| | 785 | |
|
| 0 | 786 | | ApplyReorder(); |
| | 787 | | } |
| 0 | 788 | | } |
| | 789 | |
|
| | 790 | | private void DrawEmpty(Rect rect, string label, GUIStyle backgroundStyle, GUIStyle labelStyle) |
| | 791 | | { |
| | 792 | |
|
| 0 | 793 | | if (showDefaultBackground && Event.current.type == EventType.Repaint) |
| | 794 | | { |
| | 795 | |
|
| 0 | 796 | | backgroundStyle.Draw(rect, false, false, false, false); |
| | 797 | | } |
| | 798 | |
|
| 0 | 799 | | EditorGUI.LabelField(rect, label, labelStyle); |
| 0 | 800 | | } |
| | 801 | |
|
| | 802 | | private void UpdateElementRects(Rect rect, Event evt) |
| | 803 | | { |
| | 804 | |
|
| | 805 | | //resize array if elements changed |
| | 806 | |
|
| 0 | 807 | | int i, len = Length; |
| | 808 | |
|
| 0 | 809 | | if (len != elementRects.Length) |
| | 810 | | { |
| | 811 | |
|
| 0 | 812 | | System.Array.Resize(ref elementRects, len); |
| | 813 | | } |
| | 814 | |
|
| 0 | 815 | | if (evt.type == EventType.Repaint) |
| | 816 | | { |
| | 817 | |
|
| | 818 | | //start rect |
| | 819 | |
|
| 0 | 820 | | Rect elementRect = rect; |
| 0 | 821 | | elementRect.yMin = elementRect.yMax = rect.yMin + 2; |
| | 822 | |
|
| 0 | 823 | | float spacing = elementSpacing; |
| | 824 | |
|
| | 825 | | int start, end; |
| | 826 | |
|
| 0 | 827 | | pagination.GetVisibleRange(len, out start, out end); |
| | 828 | |
|
| 0 | 829 | | for (i = start; i < end; i++) |
| | 830 | | { |
| | 831 | |
|
| 0 | 832 | | SerializedProperty element = list.GetArrayElementAtIndex(i); |
| | 833 | |
|
| | 834 | | //update the elementRects value for this object. Grab the last elementRect for startPosition |
| | 835 | |
|
| 0 | 836 | | elementRect.y = elementRect.yMax; |
| 0 | 837 | | elementRect.height = GetElementHeight(element); |
| 0 | 838 | | elementRects[i] = elementRect; |
| | 839 | |
|
| 0 | 840 | | elementRect.yMax += spacing; |
| | 841 | | } |
| | 842 | | } |
| 0 | 843 | | } |
| | 844 | |
|
| | 845 | | private void DrawElements(Rect rect, Event evt) |
| | 846 | | { |
| | 847 | |
|
| | 848 | | //draw list background |
| | 849 | |
|
| 0 | 850 | | if (showDefaultBackground && evt.type == EventType.Repaint) |
| | 851 | | { |
| | 852 | |
|
| 0 | 853 | | Style.boxBackground.Draw(rect, false, false, false, false); |
| | 854 | | } |
| | 855 | |
|
| | 856 | | //if not dragging, draw elements as usual |
| | 857 | |
|
| 0 | 858 | | if (!dragging) |
| | 859 | | { |
| | 860 | |
|
| | 861 | | int start, end; |
| | 862 | |
|
| 0 | 863 | | pagination.GetVisibleRange(Length, out start, out end); |
| | 864 | |
|
| 0 | 865 | | for (int i = start; i < end; i++) |
| | 866 | | { |
| | 867 | |
|
| 0 | 868 | | bool selected = selection.Contains(i); |
| | 869 | |
|
| 0 | 870 | | DrawElement(list.GetArrayElementAtIndex(i), GetElementDrawRect(i, elementRects[i]), selected, select |
| | 871 | | } |
| 0 | 872 | | } |
| 0 | 873 | | else if (evt.type == EventType.Repaint) |
| | 874 | | { |
| | 875 | |
|
| | 876 | | //draw dragging elements only when repainting |
| | 877 | |
|
| 0 | 878 | | int i, s, len = dragList.Length; |
| 0 | 879 | | int sLen = selection.Length; |
| | 880 | |
|
| | 881 | | //first, find the rects of the selected elements, we need to use them for overlap queries |
| | 882 | |
|
| 0 | 883 | | for (i = 0; i < sLen; i++) |
| | 884 | | { |
| | 885 | |
|
| 0 | 886 | | DragElement element = dragList[i]; |
| | 887 | |
|
| | 888 | | //update the element desiredRect if selected. Selected elements appear first in the dragList, so oth |
| | 889 | |
|
| 0 | 890 | | element.desiredRect.y = dragPosition - element.dragOffset; |
| 0 | 891 | | dragList[i] = element; |
| | 892 | | } |
| | 893 | |
|
| | 894 | | //draw elements, start from the bottom of the list as first elements are the ones selected, so should be |
| | 895 | |
|
| 0 | 896 | | i = len; |
| | 897 | |
|
| 0 | 898 | | while (--i > -1) |
| | 899 | | { |
| | 900 | |
|
| 0 | 901 | | DragElement element = dragList[i]; |
| | 902 | |
|
| | 903 | | //draw dragging elements last as the loop is backwards |
| | 904 | |
|
| 0 | 905 | | if (element.selected) |
| | 906 | | { |
| | 907 | |
|
| 0 | 908 | | DrawElement(element.property, element.desiredRect, true, true); |
| 0 | 909 | | continue; |
| | 910 | | } |
| | 911 | |
|
| | 912 | | //loop over selection and see what overlaps |
| | 913 | | //if dragging down we start from the bottom of the selection |
| | 914 | | //otherwise we start from the top. This helps to cover multiple selected objects |
| | 915 | |
|
| 0 | 916 | | Rect elementRect = element.rect; |
| 0 | 917 | | int elementIndex = element.startIndex; |
| | 918 | |
|
| 0 | 919 | | int start = dragDirection > 0 ? sLen - 1 : 0; |
| 0 | 920 | | int end = dragDirection > 0 ? -1 : sLen; |
| | 921 | |
|
| 0 | 922 | | for (s = start; s != end; s -= dragDirection) |
| | 923 | | { |
| | 924 | |
|
| 0 | 925 | | DragElement selected = dragList[s]; |
| | 926 | |
|
| 0 | 927 | | if (selected.Overlaps(elementRect, elementIndex, dragDirection)) |
| | 928 | | { |
| | 929 | |
|
| 0 | 930 | | elementRect.y -= selected.rect.height * dragDirection; |
| 0 | 931 | | elementIndex += dragDirection; |
| | 932 | | } |
| | 933 | | } |
| | 934 | |
|
| | 935 | | //draw the element with the new rect |
| | 936 | |
|
| 0 | 937 | | DrawElement(element.property, GetElementDrawRect(i, elementRect), false, false); |
| | 938 | |
|
| | 939 | | //reassign the element back into the dragList |
| | 940 | |
|
| 0 | 941 | | element.desiredRect = elementRect; |
| 0 | 942 | | dragList[i] = element; |
| | 943 | | } |
| | 944 | | } |
| 0 | 945 | | } |
| | 946 | |
|
| | 947 | | private void DrawElement(SerializedProperty element, Rect rect, bool selected, bool focused) |
| | 948 | | { |
| | 949 | |
|
| 0 | 950 | | Event evt = Event.current; |
| | 951 | |
|
| 0 | 952 | | if (drawElementBackgroundCallback != null) |
| | 953 | | { |
| | 954 | |
|
| 0 | 955 | | drawElementBackgroundCallback(rect, element, null, selected, focused); |
| 0 | 956 | | } |
| 0 | 957 | | else if (evt.type == EventType.Repaint) |
| | 958 | | { |
| | 959 | |
|
| 0 | 960 | | Style.elementBackground.Draw(rect, false, selected, selected, focused); |
| | 961 | | } |
| | 962 | |
|
| 0 | 963 | | if (evt.type == EventType.Repaint && draggable) |
| | 964 | | { |
| | 965 | |
|
| 0 | 966 | | Style.draggingHandle.Draw(new Rect(rect.x + 5, rect.y + 6, 10, rect.height - (rect.height - 6)), false, |
| | 967 | | } |
| | 968 | |
|
| 0 | 969 | | GUIContent label = GetElementLabel(element, elementLabels); |
| | 970 | |
|
| 0 | 971 | | Rect renderRect = GetElementRenderRect(element, rect); |
| | 972 | |
|
| 0 | 973 | | if (drawElementCallback != null) |
| | 974 | | { |
| | 975 | |
|
| 0 | 976 | | drawElementCallback(renderRect, element, label, selected, focused); |
| 0 | 977 | | } |
| | 978 | | else |
| | 979 | | { |
| | 980 | |
|
| 0 | 981 | | EditorGUI.PropertyField(renderRect, element, label, true); |
| | 982 | | } |
| | 983 | |
|
| | 984 | | //handle context click |
| | 985 | |
|
| 0 | 986 | | int controlId = GUIUtility.GetControlID(label, FocusType.Passive, rect); |
| | 987 | |
|
| 0 | 988 | | switch (evt.GetTypeForControl(controlId)) |
| | 989 | | { |
| | 990 | |
|
| | 991 | | case EventType.ContextClick: |
| | 992 | |
|
| 0 | 993 | | if (rect.Contains(evt.mousePosition)) |
| | 994 | | { |
| | 995 | |
|
| 0 | 996 | | HandleSingleContextClick(evt, element); |
| | 997 | | } |
| | 998 | |
|
| | 999 | | break; |
| | 1000 | | } |
| 0 | 1001 | | } |
| | 1002 | |
|
| | 1003 | | private GUIContent GetElementLabel(SerializedProperty element, bool allowElementLabel) |
| | 1004 | | { |
| | 1005 | |
|
| 0 | 1006 | | if (!allowElementLabel) |
| | 1007 | | { |
| | 1008 | |
|
| 0 | 1009 | | return GUIContent.none; |
| | 1010 | | } |
| 0 | 1011 | | else if (getElementLabelCallback != null) |
| | 1012 | | { |
| | 1013 | |
|
| 0 | 1014 | | return getElementLabelCallback(element); |
| | 1015 | | } |
| | 1016 | |
|
| | 1017 | | string name; |
| | 1018 | |
|
| 0 | 1019 | | if (getElementNameCallback != null) |
| | 1020 | | { |
| | 1021 | |
|
| 0 | 1022 | | name = getElementNameCallback(element); |
| 0 | 1023 | | } |
| | 1024 | | else |
| | 1025 | | { |
| | 1026 | |
|
| 0 | 1027 | | name = GetElementName(element, elementNameProperty, elementNameOverride); |
| | 1028 | | } |
| | 1029 | |
|
| 0 | 1030 | | elementLabel.text = !string.IsNullOrEmpty(name) ? name : element.displayName; |
| 0 | 1031 | | elementLabel.tooltip = element.tooltip; |
| 0 | 1032 | | elementLabel.image = elementIcon; |
| | 1033 | |
|
| 0 | 1034 | | return elementLabel; |
| | 1035 | | } |
| | 1036 | |
|
| | 1037 | | private static string GetElementName(SerializedProperty element, string nameProperty, string nameOverride) |
| | 1038 | | { |
| | 1039 | |
|
| 0 | 1040 | | if (!string.IsNullOrEmpty(nameOverride)) |
| | 1041 | | { |
| | 1042 | |
|
| 0 | 1043 | | string path = element.propertyPath; |
| | 1044 | |
|
| | 1045 | | const string arrayEndDelimeter = "]"; |
| | 1046 | | const char arrayStartDelimeter = '['; |
| | 1047 | |
|
| 0 | 1048 | | if (path.EndsWith(arrayEndDelimeter)) |
| | 1049 | | { |
| | 1050 | |
|
| 0 | 1051 | | int startIndex = path.LastIndexOf(arrayStartDelimeter) + 1; |
| | 1052 | |
|
| 0 | 1053 | | return string.Format("{0} {1}", nameOverride, path.Substring(startIndex, path.Length - startIndex - |
| | 1054 | | } |
| | 1055 | |
|
| 0 | 1056 | | return nameOverride; |
| | 1057 | | } |
| 0 | 1058 | | else if (string.IsNullOrEmpty(nameProperty)) |
| | 1059 | | { |
| | 1060 | |
|
| 0 | 1061 | | return null; |
| | 1062 | | } |
| 0 | 1063 | | else if (element.propertyType == SerializedPropertyType.ObjectReference && nameProperty == "name") |
| | 1064 | | { |
| | 1065 | |
|
| 0 | 1066 | | return element.objectReferenceValue ? element.objectReferenceValue.name : null; |
| | 1067 | | } |
| | 1068 | |
|
| 0 | 1069 | | SerializedProperty prop = element.FindPropertyRelative(nameProperty); |
| | 1070 | |
|
| 0 | 1071 | | if (prop != null) |
| | 1072 | | { |
| | 1073 | |
|
| 0 | 1074 | | switch (prop.propertyType) |
| | 1075 | | { |
| | 1076 | |
|
| | 1077 | | case SerializedPropertyType.ObjectReference: |
| | 1078 | |
|
| 0 | 1079 | | return prop.objectReferenceValue ? prop.objectReferenceValue.name : null; |
| | 1080 | |
|
| | 1081 | | case SerializedPropertyType.Enum: |
| | 1082 | |
|
| 0 | 1083 | | return prop.enumDisplayNames[prop.enumValueIndex]; |
| | 1084 | |
|
| | 1085 | | case SerializedPropertyType.Integer: |
| | 1086 | | case SerializedPropertyType.Character: |
| | 1087 | |
|
| 0 | 1088 | | return prop.intValue.ToString(); |
| | 1089 | |
|
| | 1090 | | case SerializedPropertyType.LayerMask: |
| | 1091 | |
|
| 0 | 1092 | | return GetLayerMaskName(prop.intValue); |
| | 1093 | |
|
| | 1094 | | case SerializedPropertyType.String: |
| | 1095 | |
|
| 0 | 1096 | | return prop.stringValue; |
| | 1097 | |
|
| | 1098 | | case SerializedPropertyType.Float: |
| | 1099 | |
|
| 0 | 1100 | | return prop.floatValue.ToString(); |
| | 1101 | | } |
| | 1102 | |
|
| 0 | 1103 | | return prop.displayName; |
| | 1104 | | } |
| | 1105 | |
|
| 0 | 1106 | | return null; |
| | 1107 | | } |
| | 1108 | |
|
| | 1109 | | private static string GetLayerMaskName(int mask) |
| | 1110 | | { |
| | 1111 | |
|
| 0 | 1112 | | if (mask == 0) |
| | 1113 | | { |
| | 1114 | |
|
| 0 | 1115 | | return "Nothing"; |
| | 1116 | | } |
| 0 | 1117 | | else if (mask < 0) |
| | 1118 | | { |
| | 1119 | |
|
| 0 | 1120 | | return "Everything"; |
| | 1121 | | } |
| | 1122 | |
|
| 0 | 1123 | | string name = string.Empty; |
| 0 | 1124 | | int n = 0; |
| | 1125 | |
|
| 0 | 1126 | | for (int i = 0; i < 32; i++) |
| | 1127 | | { |
| | 1128 | |
|
| 0 | 1129 | | if (((1 << i) & mask) != 0) |
| | 1130 | | { |
| | 1131 | |
|
| 0 | 1132 | | if (n == 4) |
| | 1133 | | { |
| | 1134 | |
|
| 0 | 1135 | | return "Mixed ..."; |
| | 1136 | | } |
| | 1137 | |
|
| 0 | 1138 | | name += (n > 0 ? ", " : string.Empty) + LayerMask.LayerToName(i); |
| 0 | 1139 | | n++; |
| | 1140 | | } |
| | 1141 | | } |
| | 1142 | |
|
| 0 | 1143 | | return name; |
| | 1144 | | } |
| | 1145 | |
|
| | 1146 | | private void DrawFooter(Rect rect) |
| | 1147 | | { |
| | 1148 | |
|
| 0 | 1149 | | if (drawFooterCallback != null) |
| | 1150 | | { |
| | 1151 | |
|
| 0 | 1152 | | drawFooterCallback(rect); |
| 0 | 1153 | | return; |
| | 1154 | | } |
| | 1155 | |
|
| 0 | 1156 | | if (Event.current.type == EventType.Repaint) |
| | 1157 | | { |
| | 1158 | |
|
| 0 | 1159 | | Style.footerBackground.Draw(rect, false, false, false, false); |
| | 1160 | | } |
| | 1161 | |
|
| 0 | 1162 | | Rect addRect = new Rect(rect.xMin + 4f, rect.y - 3f, 25f, 13f); |
| 0 | 1163 | | Rect subRect = new Rect(rect.xMax - 29f, rect.y - 3f, 25f, 13f); |
| | 1164 | |
|
| 0 | 1165 | | EditorGUI.BeginDisabledGroup(!canAdd); |
| | 1166 | |
|
| 0 | 1167 | | if (GUI.Button(addRect, onAddDropdownCallback != null ? Style.iconToolbarPlusMore : Style.iconToolbarPlus, S |
| | 1168 | | { |
| | 1169 | |
|
| 0 | 1170 | | if (onAddDropdownCallback != null) |
| | 1171 | | { |
| | 1172 | |
|
| 0 | 1173 | | onAddDropdownCallback(addRect, this); |
| 0 | 1174 | | } |
| 0 | 1175 | | else if (onAddCallback != null) |
| | 1176 | | { |
| | 1177 | |
|
| 0 | 1178 | | onAddCallback(this); |
| 0 | 1179 | | } |
| | 1180 | | else |
| | 1181 | | { |
| | 1182 | |
|
| 0 | 1183 | | AddItem(); |
| | 1184 | | } |
| | 1185 | | } |
| | 1186 | |
|
| 0 | 1187 | | EditorGUI.EndDisabledGroup(); |
| | 1188 | |
|
| 0 | 1189 | | EditorGUI.BeginDisabledGroup(!CanSelect(selection) || !canRemove || (onCanRemoveCallback != null && !onCanRe |
| | 1190 | |
|
| 0 | 1191 | | if (GUI.Button(subRect, Style.iconToolbarMinus, Style.preButton)) |
| | 1192 | | { |
| | 1193 | |
|
| 0 | 1194 | | if (onRemoveCallback != null) |
| | 1195 | | { |
| | 1196 | |
|
| 0 | 1197 | | onRemoveCallback(this); |
| 0 | 1198 | | } |
| | 1199 | | else |
| | 1200 | | { |
| | 1201 | |
|
| 0 | 1202 | | Remove(selection.ToArray()); |
| | 1203 | | } |
| | 1204 | | } |
| | 1205 | |
|
| 0 | 1206 | | EditorGUI.EndDisabledGroup(); |
| 0 | 1207 | | } |
| | 1208 | |
|
| | 1209 | | private void DrawPaginationHeader(Rect rect) |
| | 1210 | | { |
| | 1211 | |
|
| 0 | 1212 | | int total = Length; |
| 0 | 1213 | | int pages = pagination.GetPageCount(total); |
| 0 | 1214 | | int page = Mathf.Clamp(pagination.page, 0, pages - 1); |
| | 1215 | |
|
| | 1216 | | //some actions may have reduced the page count, so we need to check the current page against the clamped one |
| | 1217 | | //if different, we need to change and repaint |
| | 1218 | |
|
| 0 | 1219 | | if (page != pagination.page) |
| | 1220 | | { |
| | 1221 | |
|
| 0 | 1222 | | pagination.page = page; |
| | 1223 | |
|
| 0 | 1224 | | HandleUtility.Repaint(); |
| | 1225 | | } |
| | 1226 | |
|
| 0 | 1227 | | Rect prevRect = new Rect(rect.xMin + 4f, rect.y - 1f, 17f, 14f); |
| 0 | 1228 | | Rect popupRect = new Rect(prevRect.xMax, rect.y - 1f, 14f, 14f); |
| 0 | 1229 | | Rect nextRect = new Rect(popupRect.xMax, rect.y - 1f, 17f, 14f); |
| | 1230 | |
|
| 0 | 1231 | | if (Event.current.type == EventType.Repaint) |
| | 1232 | | { |
| | 1233 | |
|
| 0 | 1234 | | Style.paginationHeader.Draw(rect, false, true, true, false); |
| | 1235 | | } |
| | 1236 | |
|
| 0 | 1237 | | pageInfoContent.text = string.Format(Style.PAGE_INFO_FORMAT, pagination.page + 1, pages); |
| | 1238 | |
|
| 0 | 1239 | | Rect pageInfoRect = rect; |
| 0 | 1240 | | pageInfoRect.width = Style.paginationText.CalcSize(pageInfoContent).x; |
| 0 | 1241 | | pageInfoRect.x = rect.xMax - pageInfoRect.width - 7; |
| 0 | 1242 | | pageInfoRect.y += 2; |
| | 1243 | |
|
| | 1244 | | //draw page info |
| | 1245 | |
|
| 0 | 1246 | | GUI.Label(pageInfoRect, pageInfoContent, Style.paginationText); |
| | 1247 | |
|
| | 1248 | | //draw page buttons and page popup |
| | 1249 | |
|
| 0 | 1250 | | if (GUI.Button(prevRect, Style.iconPagePrev, Style.preButton)) |
| | 1251 | | { |
| | 1252 | |
|
| 0 | 1253 | | pagination.page = Mathf.Max(0, pagination.page - 1); |
| | 1254 | | } |
| | 1255 | |
|
| 0 | 1256 | | if (EditorGUI.DropdownButton(popupRect, Style.iconPagePopup, FocusType.Passive, Style.preButton)) |
| | 1257 | | { |
| | 1258 | |
|
| 0 | 1259 | | GenericMenu menu = new GenericMenu(); |
| | 1260 | |
|
| 0 | 1261 | | for (int i = 0; i < pages; i++) |
| | 1262 | | { |
| | 1263 | |
|
| 0 | 1264 | | int pageIndex = i; |
| | 1265 | |
|
| 0 | 1266 | | menu.AddItem(new GUIContent(string.Format("Page {0}", i + 1)), i == pagination.page, OnPageDropDownS |
| | 1267 | | } |
| | 1268 | |
|
| 0 | 1269 | | menu.DropDown(popupRect); |
| | 1270 | | } |
| | 1271 | |
|
| 0 | 1272 | | if (GUI.Button(nextRect, Style.iconPageNext, Style.preButton)) |
| | 1273 | | { |
| | 1274 | |
|
| 0 | 1275 | | pagination.page = Mathf.Min(pages - 1, pagination.page + 1); |
| | 1276 | | } |
| | 1277 | |
|
| | 1278 | | //if we're allowed to control the page size manually, show an editor |
| | 1279 | |
|
| 0 | 1280 | | bool useFixedPageSize = pagination.fixedPageSize > 0; |
| | 1281 | |
|
| 0 | 1282 | | EditorGUI.BeginDisabledGroup(useFixedPageSize); |
| | 1283 | |
|
| 0 | 1284 | | pageSizeContent.text = total.ToString(); |
| | 1285 | |
|
| 0 | 1286 | | GUIStyle style = Style.pageSizeTextField; |
| 0 | 1287 | | Texture icon = Style.listIcon.image; |
| | 1288 | |
|
| 0 | 1289 | | float min = nextRect.xMax + 5; |
| 0 | 1290 | | float max = pageInfoRect.xMin - 5; |
| 0 | 1291 | | float space = max - min; |
| 0 | 1292 | | float labelWidth = icon.width + 2; |
| 0 | 1293 | | float width = style.CalcSize(pageSizeContent).x + 50 + labelWidth; |
| | 1294 | |
|
| 0 | 1295 | | Rect pageSizeRect = rect; |
| 0 | 1296 | | pageSizeRect.y--; |
| 0 | 1297 | | pageSizeRect.x = min + (space - width) / 2; |
| 0 | 1298 | | pageSizeRect.width = width - labelWidth; |
| | 1299 | |
|
| 0 | 1300 | | EditorGUI.BeginChangeCheck(); |
| | 1301 | |
|
| 0 | 1302 | | EditorGUIUtility.labelWidth = labelWidth; |
| 0 | 1303 | | EditorGUIUtility.SetIconSize(new Vector2(icon.width, icon.height)); |
| | 1304 | |
|
| 0 | 1305 | | int newPageSize = EditorGUI.DelayedIntField(pageSizeRect, Style.listIcon, useFixedPageSize ? pagination.fixe |
| | 1306 | |
|
| 0 | 1307 | | EditorGUIUtility.labelWidth = 0; |
| 0 | 1308 | | EditorGUIUtility.SetIconSize(Vector2.zero); |
| | 1309 | |
|
| 0 | 1310 | | if (EditorGUI.EndChangeCheck()) |
| | 1311 | | { |
| | 1312 | |
|
| 0 | 1313 | | pagination.customPageSize = Mathf.Clamp(newPageSize, 0, total); |
| 0 | 1314 | | pagination.page = Mathf.Min(pagination.GetPageCount(total) - 1, pagination.page); |
| | 1315 | | } |
| | 1316 | |
|
| 0 | 1317 | | EditorGUI.EndDisabledGroup(); |
| 0 | 1318 | | } |
| | 1319 | |
|
| 0 | 1320 | | private void OnPageDropDownSelect(object userData) { pagination.page = (int)userData; } |
| | 1321 | |
|
| | 1322 | | private void DispatchChange() |
| | 1323 | | { |
| | 1324 | |
|
| 0 | 1325 | | if (onChangedCallback != null) |
| | 1326 | | { |
| | 1327 | |
|
| 0 | 1328 | | onChangedCallback(this); |
| | 1329 | | } |
| 0 | 1330 | | } |
| | 1331 | |
|
| | 1332 | | private void HandleSingleContextClick(Event evt, SerializedProperty element) |
| | 1333 | | { |
| | 1334 | |
|
| 0 | 1335 | | selection.Select(IndexOf(element)); |
| | 1336 | |
|
| 0 | 1337 | | GenericMenu menu = new GenericMenu(); |
| | 1338 | |
|
| 0 | 1339 | | if (element.isInstantiatedPrefab) |
| | 1340 | | { |
| | 1341 | |
|
| 0 | 1342 | | menu.AddItem(new GUIContent("Revert " + GetElementLabel(element, true).text + " to Prefab"), false, sele |
| 0 | 1343 | | menu.AddSeparator(string.Empty); |
| | 1344 | | } |
| | 1345 | |
|
| 0 | 1346 | | HandleSharedContextClick(evt, menu, "Duplicate Array Element", "Delete Array Element", "Move Array Element") |
| 0 | 1347 | | } |
| | 1348 | |
|
| | 1349 | | private void HandleMultipleContextClick(Event evt) |
| | 1350 | | { |
| | 1351 | |
|
| 0 | 1352 | | GenericMenu menu = new GenericMenu(); |
| | 1353 | |
|
| 0 | 1354 | | if (selection.CanRevert(list)) |
| | 1355 | | { |
| | 1356 | |
|
| 0 | 1357 | | menu.AddItem(new GUIContent("Revert Values to Prefab"), false, selection.RevertValues, list); |
| 0 | 1358 | | menu.AddSeparator(string.Empty); |
| | 1359 | | } |
| | 1360 | |
|
| 0 | 1361 | | HandleSharedContextClick(evt, menu, "Duplicate Array Elements", "Delete Array Elements", "Move Array Element |
| 0 | 1362 | | } |
| | 1363 | |
|
| | 1364 | | private void HandleSharedContextClick(Event evt, GenericMenu menu, string duplicateLabel, string deleteLabel, st |
| | 1365 | | { |
| | 1366 | |
|
| 0 | 1367 | | menu.AddItem(new GUIContent(duplicateLabel), false, HandleDuplicate, list); |
| 0 | 1368 | | menu.AddItem(new GUIContent(deleteLabel), false, HandleDelete, list); |
| | 1369 | |
|
| 0 | 1370 | | if (doPagination) |
| | 1371 | | { |
| | 1372 | |
|
| 0 | 1373 | | int pages = pagination.GetPageCount(Length); |
| | 1374 | |
|
| 0 | 1375 | | if (pages > 1) |
| | 1376 | | { |
| | 1377 | |
|
| 0 | 1378 | | for (int i = 0; i < pages; i++) |
| | 1379 | | { |
| | 1380 | |
|
| 0 | 1381 | | string label = string.Format("{0}/Page {1}", moveLabel, i + 1); |
| | 1382 | |
|
| 0 | 1383 | | menu.AddItem(new GUIContent(label), i == pagination.page, HandleMoveElement, i); |
| | 1384 | | } |
| | 1385 | | } |
| | 1386 | | } |
| | 1387 | |
|
| 0 | 1388 | | menu.ShowAsContext(); |
| | 1389 | |
|
| 0 | 1390 | | evt.Use(); |
| 0 | 1391 | | } |
| | 1392 | |
|
| | 1393 | | private void HandleMoveElement(object userData) |
| | 1394 | | { |
| | 1395 | |
|
| 0 | 1396 | | int toPage = (int)userData; |
| 0 | 1397 | | int fromPage = pagination.page; |
| 0 | 1398 | | int size = pagination.pageSize; |
| 0 | 1399 | | int offset = (toPage * size) - (fromPage * size); |
| 0 | 1400 | | int direction = offset > 0 ? 1 : -1; |
| 0 | 1401 | | int total = Length; |
| | 1402 | |
|
| | 1403 | | //We need to find the actually positions things will move to and not clamp the index |
| | 1404 | | //because sometimes something wants to move to a negative index, or beyond the length |
| | 1405 | | //we need to find this overlow and adjust the move offsets based on that |
| | 1406 | |
|
| 0 | 1407 | | int overflow = 0; |
| | 1408 | |
|
| 0 | 1409 | | for (int i = 0; i < selection.Length; i++) |
| | 1410 | | { |
| | 1411 | |
|
| 0 | 1412 | | int desiredIndex = selection[i] + offset; |
| | 1413 | |
|
| 0 | 1414 | | overflow = direction < 0 ? Mathf.Min(overflow, desiredIndex) : Mathf.Max(overflow, desiredIndex - total) |
| | 1415 | | } |
| | 1416 | |
|
| 0 | 1417 | | offset -= overflow; |
| | 1418 | |
|
| | 1419 | | //copy the current list to prepare for moving |
| | 1420 | |
|
| 0 | 1421 | | UpdateDragList(0, 0, total); |
| | 1422 | |
|
| | 1423 | | //create a list that will act as our new order |
| | 1424 | |
|
| 0 | 1425 | | List<DragElement> orderedList = new List<DragElement>(dragList.Elements.Where(t => !selection.Contains(t.sta |
| | 1426 | |
|
| | 1427 | | //go through the selection and insert them into the new order based on the page offset |
| | 1428 | |
|
| 0 | 1429 | | selection.Sort(); |
| | 1430 | |
|
| 0 | 1431 | | for (int i = 0; i < selection.Length; i++) |
| | 1432 | | { |
| | 1433 | |
|
| 0 | 1434 | | int selIndex = selection[i]; |
| 0 | 1435 | | int oldIndex = dragList.GetIndexFromSelection(selIndex); |
| 0 | 1436 | | int newIndex = Mathf.Clamp(selIndex + offset, 0, orderedList.Count); |
| | 1437 | |
|
| 0 | 1438 | | orderedList.Insert(newIndex, dragList[oldIndex]); |
| | 1439 | | } |
| | 1440 | |
|
| | 1441 | | //finally, perform the re-order |
| | 1442 | |
|
| 0 | 1443 | | dragList.Elements = orderedList.ToArray(); |
| | 1444 | |
|
| 0 | 1445 | | ReorderDraggedElements(direction, 0, null); |
| | 1446 | |
|
| | 1447 | | //assume we still want to view these items |
| | 1448 | |
|
| 0 | 1449 | | pagination.page = toPage; |
| | 1450 | |
|
| 0 | 1451 | | HandleUtility.Repaint(); |
| 0 | 1452 | | } |
| | 1453 | |
|
| | 1454 | | private void HandleDelete(object userData) |
| | 1455 | | { |
| | 1456 | |
|
| 0 | 1457 | | selection.Delete(userData as SerializedProperty); |
| | 1458 | |
|
| 0 | 1459 | | DispatchChange(); |
| 0 | 1460 | | } |
| | 1461 | |
|
| | 1462 | | private void HandleDuplicate(object userData) |
| | 1463 | | { |
| | 1464 | |
|
| 0 | 1465 | | selection.Duplicate(userData as SerializedProperty); |
| | 1466 | |
|
| 0 | 1467 | | DispatchChange(); |
| 0 | 1468 | | } |
| | 1469 | |
|
| | 1470 | | private void HandleDragAndDrop(Rect rect, Event evt) |
| | 1471 | | { |
| | 1472 | |
|
| 0 | 1473 | | switch (evt.GetTypeForControl(dragDropControlID)) |
| | 1474 | | { |
| | 1475 | |
|
| | 1476 | | case EventType.DragUpdated: |
| | 1477 | | case EventType.DragPerform: |
| | 1478 | |
|
| 0 | 1479 | | if (GUI.enabled && rect.Contains(evt.mousePosition)) |
| | 1480 | | { |
| | 1481 | |
|
| 0 | 1482 | | Object[] objectReferences = DragAndDrop.objectReferences; |
| 0 | 1483 | | Object[] references = new Object[1]; |
| | 1484 | |
|
| 0 | 1485 | | bool acceptDrag = false; |
| | 1486 | |
|
| 0 | 1487 | | foreach (Object object1 in objectReferences) |
| | 1488 | | { |
| | 1489 | |
|
| 0 | 1490 | | references[0] = object1; |
| 0 | 1491 | | Object object2 = ValidateObjectDragAndDrop(references); |
| | 1492 | |
|
| 0 | 1493 | | if (object2 != null) |
| | 1494 | | { |
| | 1495 | |
|
| 0 | 1496 | | DragAndDrop.visualMode = DragAndDropVisualMode.Copy; |
| | 1497 | |
|
| 0 | 1498 | | if (evt.type == EventType.DragPerform) |
| | 1499 | | { |
| | 1500 | |
|
| 0 | 1501 | | AppendDragAndDropValue(object2); |
| | 1502 | |
|
| 0 | 1503 | | acceptDrag = true; |
| 0 | 1504 | | DragAndDrop.activeControlID = 0; |
| 0 | 1505 | | } |
| | 1506 | | else |
| | 1507 | | { |
| | 1508 | |
|
| 0 | 1509 | | DragAndDrop.activeControlID = dragDropControlID; |
| | 1510 | | } |
| | 1511 | | } |
| | 1512 | | } |
| | 1513 | |
|
| 0 | 1514 | | if (acceptDrag) |
| | 1515 | | { |
| | 1516 | |
|
| 0 | 1517 | | GUI.changed = true; |
| 0 | 1518 | | DragAndDrop.AcceptDrag(); |
| | 1519 | | } |
| | 1520 | | } |
| | 1521 | |
|
| 0 | 1522 | | break; |
| | 1523 | |
|
| | 1524 | | case EventType.DragExited: |
| | 1525 | |
|
| 0 | 1526 | | if (GUI.enabled) |
| | 1527 | | { |
| | 1528 | |
|
| 0 | 1529 | | HandleUtility.Repaint(); |
| | 1530 | | } |
| | 1531 | |
|
| | 1532 | | break; |
| | 1533 | | } |
| 0 | 1534 | | } |
| | 1535 | |
|
| | 1536 | | private Object ValidateObjectDragAndDrop(Object[] references) |
| | 1537 | | { |
| | 1538 | |
|
| 0 | 1539 | | if (onValidateDragAndDropCallback != null) |
| | 1540 | | { |
| | 1541 | |
|
| 0 | 1542 | | return onValidateDragAndDropCallback(references, this); |
| | 1543 | | } |
| 0 | 1544 | | else if (surrogate.HasType) |
| | 1545 | | { |
| | 1546 | |
|
| | 1547 | | //if we have a surrogate type, then validate using the surrogate type rather than the list |
| | 1548 | |
|
| 0 | 1549 | | return Internals.ValidateObjectDragAndDrop(references, null, surrogate.type, surrogate.exactType); |
| | 1550 | | } |
| | 1551 | |
|
| 0 | 1552 | | return Internals.ValidateObjectDragAndDrop(references, list, null, false); |
| | 1553 | | } |
| | 1554 | |
|
| | 1555 | | private void AppendDragAndDropValue(Object obj) |
| | 1556 | | { |
| | 1557 | |
|
| 0 | 1558 | | if (onAppendDragDropCallback != null) |
| | 1559 | | { |
| | 1560 | |
|
| 0 | 1561 | | onAppendDragDropCallback(obj, this); |
| 0 | 1562 | | } |
| | 1563 | | else |
| | 1564 | | { |
| | 1565 | |
|
| | 1566 | | //check if we have a surrogate type. If so use that for appending |
| | 1567 | |
|
| 0 | 1568 | | if (surrogate.HasType) |
| | 1569 | | { |
| | 1570 | |
|
| 0 | 1571 | | surrogate.Invoke(AddItem(), obj, this); |
| 0 | 1572 | | } |
| | 1573 | | else |
| | 1574 | | { |
| | 1575 | |
|
| 0 | 1576 | | Internals.AppendDragAndDropValue(obj, list); |
| | 1577 | | } |
| | 1578 | | } |
| | 1579 | |
|
| 0 | 1580 | | DispatchChange(); |
| 0 | 1581 | | } |
| | 1582 | |
|
| | 1583 | | private void HandlePreSelection(Rect rect, Event evt) |
| | 1584 | | { |
| | 1585 | |
|
| 0 | 1586 | | if (evt.type == EventType.MouseDrag && draggable && GUIUtility.hotControl == controlID) |
| | 1587 | | { |
| | 1588 | |
|
| 0 | 1589 | | if (selection.Length > 0 && UpdateDragPosition(evt.mousePosition, rect, dragList)) |
| | 1590 | | { |
| | 1591 | |
|
| 0 | 1592 | | GUIUtility.keyboardControl = controlID; |
| 0 | 1593 | | dragging = true; |
| | 1594 | | } |
| | 1595 | |
|
| 0 | 1596 | | evt.Use(); |
| | 1597 | | } |
| | 1598 | |
|
| | 1599 | | /* TODO This is buggy. The reason for this is to allow selection and dragging of an element using the header |
| | 1600 | | * The main issue here is determining whether the element has an "expandable" drop down arrow, which if it d |
| | 1601 | | * Because of property drawers and certain property types, it's impossible to know this automatically (witho |
| | 1602 | | * So if the below code is active and we determine that the property is expandable but isn't actually. Then |
| | 1603 | | * So for now, in order to drag or select a row, the user must select empty space on the row. Not a huge dea |
| | 1604 | | * What needs to happen is the drag event needs to occur independent of the event type. But that's messy too |
| | 1605 | | if (evt.type == EventType.MouseDown) { |
| | 1606 | |
|
| | 1607 | | //check if we contain the mouse press |
| | 1608 | | //we also need to check what has current focus. If nothing we can assume control |
| | 1609 | | //if there's something, check if the header has been pressed if the element is expandable |
| | 1610 | | //if we did press the header, then override the control |
| | 1611 | |
|
| | 1612 | | if (rect.Contains(evt.mousePosition) && IsSelectionButton(evt)) { |
| | 1613 | |
|
| | 1614 | | int index = GetSelectionIndex(evt.mousePosition); |
| | 1615 | |
|
| | 1616 | | if (CanSelect(index)) { |
| | 1617 | |
|
| | 1618 | | SerializedProperty element = list.GetArrayElementAtIndex(index); |
| | 1619 | |
|
| | 1620 | | if (IsElementExpandable(element)) { |
| | 1621 | |
|
| | 1622 | | Rect elementHeaderRect = GetElementHeaderRect(element, elementRects[index]); |
| | 1623 | | Rect elementRenderRect = GetElementRenderRect(element, elementRects[index]); |
| | 1624 | |
|
| | 1625 | | Rect elementExpandRect = elementHeaderRect; |
| | 1626 | | elementExpandRect.xMin = elementRenderRect.xMin - 10; |
| | 1627 | | elementExpandRect.xMax = elementRenderRect.xMin; |
| | 1628 | |
|
| | 1629 | | if (elementHeaderRect.Contains(evt.mousePosition) && !elementExpandRect.Contains(evt.mousePo |
| | 1630 | |
|
| | 1631 | | DoSelection(index, true, evt); |
| | 1632 | | HandleUtility.Repaint(); |
| | 1633 | | } |
| | 1634 | | } |
| | 1635 | | } |
| | 1636 | | } |
| | 1637 | | } |
| | 1638 | | */ |
| 0 | 1639 | | } |
| | 1640 | |
|
| | 1641 | | private void HandlePostSelection(Rect rect, Event evt) |
| | 1642 | | { |
| | 1643 | |
|
| 0 | 1644 | | switch (evt.GetTypeForControl(controlID)) |
| | 1645 | | { |
| | 1646 | |
|
| | 1647 | | case EventType.MouseDown: |
| | 1648 | |
|
| 0 | 1649 | | if (rect.Contains(evt.mousePosition) && IsSelectionButton(evt)) |
| | 1650 | | { |
| | 1651 | |
|
| 0 | 1652 | | int index = GetSelectionIndex(evt.mousePosition); |
| | 1653 | |
|
| 0 | 1654 | | if (CanSelect(index)) |
| | 1655 | | { |
| | 1656 | |
|
| 0 | 1657 | | DoSelection(index, GUIUtility.keyboardControl == 0 || GUIUtility.keyboardControl == controlI |
| 0 | 1658 | | } |
| | 1659 | | else |
| | 1660 | | { |
| | 1661 | |
|
| 0 | 1662 | | selection.Clear(); |
| | 1663 | | } |
| | 1664 | |
|
| 0 | 1665 | | HandleUtility.Repaint(); |
| | 1666 | | } |
| | 1667 | |
|
| 0 | 1668 | | break; |
| | 1669 | |
|
| | 1670 | | case EventType.MouseUp: |
| | 1671 | |
|
| 0 | 1672 | | if (!draggable) |
| | 1673 | | { |
| | 1674 | |
|
| | 1675 | | //select the single object if no selection modifier is being performed |
| | 1676 | |
|
| 0 | 1677 | | selection.SelectWhenNoAction(pressIndex, evt); |
| | 1678 | |
|
| 0 | 1679 | | if (onMouseUpCallback != null && IsPositionWithinElement(evt.mousePosition, selection.Last)) |
| | 1680 | | { |
| | 1681 | |
|
| 0 | 1682 | | onMouseUpCallback(this); |
| | 1683 | | } |
| 0 | 1684 | | } |
| 0 | 1685 | | else if (GUIUtility.hotControl == controlID) |
| | 1686 | | { |
| | 1687 | |
|
| 0 | 1688 | | evt.Use(); |
| | 1689 | |
|
| 0 | 1690 | | if (dragging) |
| | 1691 | | { |
| | 1692 | |
|
| 0 | 1693 | | dragging = false; |
| | 1694 | |
|
| | 1695 | | //move elements in list |
| | 1696 | |
|
| 0 | 1697 | | ReorderDraggedElements(dragDirection, dragList.StartIndex, () => dragList.SortByPosition()); |
| 0 | 1698 | | } |
| | 1699 | | else |
| | 1700 | | { |
| | 1701 | |
|
| | 1702 | | //if we didn't drag, then select the original pressed object |
| | 1703 | |
|
| 0 | 1704 | | selection.SelectWhenNoAction(pressIndex, evt); |
| | 1705 | |
|
| 0 | 1706 | | if (onMouseUpCallback != null) |
| | 1707 | | { |
| | 1708 | |
|
| 0 | 1709 | | onMouseUpCallback(this); |
| | 1710 | | } |
| | 1711 | | } |
| | 1712 | |
|
| 0 | 1713 | | GUIUtility.hotControl = 0; |
| | 1714 | | } |
| | 1715 | |
|
| 0 | 1716 | | HandleUtility.Repaint(); |
| | 1717 | |
|
| 0 | 1718 | | break; |
| | 1719 | |
|
| | 1720 | | case EventType.KeyDown: |
| | 1721 | |
|
| 0 | 1722 | | if (GUIUtility.keyboardControl == controlID) |
| | 1723 | | { |
| | 1724 | |
|
| 0 | 1725 | | if (evt.keyCode == KeyCode.DownArrow && !dragging) |
| | 1726 | | { |
| | 1727 | |
|
| 0 | 1728 | | selection.Select(Mathf.Min(selection.Last + 1, Length - 1)); |
| 0 | 1729 | | evt.Use(); |
| 0 | 1730 | | } |
| 0 | 1731 | | else if (evt.keyCode == KeyCode.UpArrow && !dragging) |
| | 1732 | | { |
| | 1733 | |
|
| 0 | 1734 | | selection.Select(Mathf.Max(selection.Last - 1, 0)); |
| 0 | 1735 | | evt.Use(); |
| 0 | 1736 | | } |
| 0 | 1737 | | else if (evt.keyCode == KeyCode.Escape && GUIUtility.hotControl == controlID) |
| | 1738 | | { |
| | 1739 | |
|
| 0 | 1740 | | GUIUtility.hotControl = 0; |
| | 1741 | |
|
| 0 | 1742 | | if (dragging) |
| | 1743 | | { |
| | 1744 | |
|
| 0 | 1745 | | dragging = false; |
| 0 | 1746 | | selection = beforeDragSelection; |
| | 1747 | | } |
| | 1748 | |
|
| 0 | 1749 | | evt.Use(); |
| | 1750 | | } |
| | 1751 | | } |
| | 1752 | |
|
| | 1753 | | break; |
| | 1754 | | } |
| 0 | 1755 | | } |
| | 1756 | |
|
| 0 | 1757 | | private bool IsSelectionButton(Event evt) { return evt.button == 0 || evt.button == 2; } |
| | 1758 | |
|
| | 1759 | | private void DoSelection(int index, bool setKeyboardControl, Event evt) |
| | 1760 | | { |
| | 1761 | |
|
| | 1762 | | //append selections based on action, this may be a additive (ctrl) or range (shift) selection |
| | 1763 | |
|
| 0 | 1764 | | if (multipleSelection) |
| | 1765 | | { |
| | 1766 | |
|
| 0 | 1767 | | selection.AppendWithAction(pressIndex = index, evt); |
| 0 | 1768 | | } |
| | 1769 | | else |
| | 1770 | | { |
| | 1771 | |
|
| 0 | 1772 | | selection.Select(pressIndex = index); |
| | 1773 | | } |
| | 1774 | |
|
| 0 | 1775 | | if (onSelectCallback != null) |
| | 1776 | | { |
| | 1777 | |
|
| 0 | 1778 | | onSelectCallback(this); |
| | 1779 | | } |
| | 1780 | |
|
| 0 | 1781 | | if (draggable) |
| | 1782 | | { |
| | 1783 | |
|
| 0 | 1784 | | dragging = false; |
| 0 | 1785 | | dragPosition = pressPosition = evt.mousePosition.y; |
| | 1786 | |
|
| | 1787 | | int start, end; |
| | 1788 | |
|
| 0 | 1789 | | pagination.GetVisibleRange(Length, out start, out end); |
| | 1790 | |
|
| 0 | 1791 | | UpdateDragList(dragPosition, start, end); |
| | 1792 | |
|
| 0 | 1793 | | selection.Trim(start, end); |
| | 1794 | |
|
| 0 | 1795 | | beforeDragSelection = selection.Clone(); |
| | 1796 | |
|
| 0 | 1797 | | GUIUtility.hotControl = controlID; |
| | 1798 | | } |
| | 1799 | |
|
| 0 | 1800 | | if (setKeyboardControl) |
| | 1801 | | { |
| | 1802 | |
|
| 0 | 1803 | | GUIUtility.keyboardControl = controlID; |
| | 1804 | | } |
| | 1805 | |
|
| 0 | 1806 | | evt.Use(); |
| 0 | 1807 | | } |
| | 1808 | |
|
| | 1809 | | private void UpdateDragList(float dragPosition, int start, int end) |
| | 1810 | | { |
| | 1811 | |
|
| 0 | 1812 | | dragList.Resize(start, end - start); |
| | 1813 | |
|
| 0 | 1814 | | for (int i = start; i < end; i++) |
| | 1815 | | { |
| | 1816 | |
|
| 0 | 1817 | | SerializedProperty property = list.GetArrayElementAtIndex(i); |
| 0 | 1818 | | Rect elementRect = elementRects[i]; |
| | 1819 | |
|
| 0 | 1820 | | DragElement dragElement = new DragElement() |
| | 1821 | | { |
| | 1822 | | property = property, |
| | 1823 | | dragOffset = dragPosition - elementRect.y, |
| | 1824 | | rect = elementRect, |
| | 1825 | | desiredRect = elementRect, |
| | 1826 | | selected = selection.Contains(i), |
| | 1827 | | startIndex = i |
| | 1828 | | }; |
| | 1829 | |
|
| 0 | 1830 | | dragList[i - start] = dragElement; |
| | 1831 | | } |
| | 1832 | |
|
| | 1833 | | //finally, sort the dragList by selection, selected objects appear first in the list |
| | 1834 | | //selection order is preserved as well |
| | 1835 | |
|
| 0 | 1836 | | dragList.SortByIndex(); |
| 0 | 1837 | | } |
| | 1838 | |
|
| | 1839 | | private bool UpdateDragPosition(Vector2 position, Rect bounds, DragList dragList) |
| | 1840 | | { |
| | 1841 | |
|
| | 1842 | | //find new drag position |
| | 1843 | |
|
| 0 | 1844 | | int startIndex = 0; |
| 0 | 1845 | | int endIndex = selection.Length - 1; |
| | 1846 | |
|
| 0 | 1847 | | float minOffset = dragList[startIndex].dragOffset; |
| 0 | 1848 | | float maxOffset = dragList[endIndex].rect.height - dragList[endIndex].dragOffset; |
| | 1849 | |
|
| 0 | 1850 | | dragPosition = Mathf.Clamp(position.y, bounds.yMin + minOffset, bounds.yMax - maxOffset); |
| | 1851 | |
|
| 0 | 1852 | | if (Mathf.Abs(dragPosition - pressPosition) > 1) |
| | 1853 | | { |
| | 1854 | |
|
| 0 | 1855 | | dragDirection = (int)Mathf.Sign(dragPosition - pressPosition); |
| 0 | 1856 | | return true; |
| | 1857 | | } |
| | 1858 | |
|
| 0 | 1859 | | return false; |
| | 1860 | | } |
| | 1861 | |
|
| | 1862 | | private void ReorderDraggedElements(int direction, int offset, System.Action sortList) |
| | 1863 | | { |
| | 1864 | |
|
| | 1865 | | //save the current expanded states on all elements. I don't see any other way to do this |
| | 1866 | | //MoveArrayElement does not move the foldout states, so... fun. |
| | 1867 | |
|
| 0 | 1868 | | dragList.RecordState(); |
| | 1869 | |
|
| 0 | 1870 | | if (sortList != null) |
| | 1871 | | { |
| | 1872 | |
|
| 0 | 1873 | | sortList(); |
| | 1874 | | } |
| | 1875 | |
|
| 0 | 1876 | | selection.Sort((a, b) => |
| | 1877 | | { |
| | 1878 | |
|
| 0 | 1879 | | int d1 = dragList.GetIndexFromSelection(a); |
| 0 | 1880 | | int d2 = dragList.GetIndexFromSelection(b); |
| | 1881 | |
|
| 0 | 1882 | | return direction > 0 ? d1.CompareTo(d2) : d2.CompareTo(d1); |
| | 1883 | | }); |
| | 1884 | |
|
| | 1885 | | //swap the selected elements in the List |
| | 1886 | |
|
| 0 | 1887 | | int s = selection.Length; |
| | 1888 | |
|
| 0 | 1889 | | while (--s > -1) |
| | 1890 | | { |
| | 1891 | |
|
| 0 | 1892 | | int newIndex = dragList.GetIndexFromSelection(selection[s]); |
| 0 | 1893 | | int listIndex = newIndex + offset; |
| | 1894 | |
|
| 0 | 1895 | | selection[s] = listIndex; |
| | 1896 | |
|
| 0 | 1897 | | list.MoveArrayElement(dragList[newIndex].startIndex, listIndex); |
| | 1898 | | } |
| | 1899 | |
|
| | 1900 | | //restore expanded states on items |
| | 1901 | |
|
| 0 | 1902 | | dragList.RestoreState(list); |
| | 1903 | |
|
| | 1904 | | //apply and update |
| | 1905 | |
|
| 0 | 1906 | | ApplyReorder(); |
| 0 | 1907 | | } |
| | 1908 | |
|
| | 1909 | | private void ApplyReorder() |
| | 1910 | | { |
| | 1911 | |
|
| 0 | 1912 | | list.serializedObject.ApplyModifiedProperties(); |
| 0 | 1913 | | list.serializedObject.Update(); |
| | 1914 | |
|
| 0 | 1915 | | if (onReorderCallback != null) |
| | 1916 | | { |
| | 1917 | |
|
| 0 | 1918 | | onReorderCallback(this); |
| | 1919 | | } |
| | 1920 | |
|
| 0 | 1921 | | DispatchChange(); |
| 0 | 1922 | | } |
| | 1923 | |
|
| | 1924 | | private int GetSelectionIndex(Vector2 position) |
| | 1925 | | { |
| | 1926 | |
|
| | 1927 | | int start, end; |
| | 1928 | |
|
| 0 | 1929 | | pagination.GetVisibleRange(elementRects.Length, out start, out end); |
| | 1930 | |
|
| 0 | 1931 | | for (int i = start; i < end; i++) |
| | 1932 | | { |
| | 1933 | |
|
| 0 | 1934 | | Rect rect = elementRects[i]; |
| | 1935 | |
|
| 0 | 1936 | | if (rect.Contains(position) || (i == 0 && position.y <= rect.yMin) || (i == end - 1 && position.y >= rec |
| | 1937 | | { |
| | 1938 | |
|
| 0 | 1939 | | return i; |
| | 1940 | | } |
| | 1941 | | } |
| | 1942 | |
|
| 0 | 1943 | | return -1; |
| | 1944 | | } |
| | 1945 | |
|
| 0 | 1946 | | private bool CanSelect(ListSelection selection) { return selection.Length > 0 ? selection.All(s => CanSelect(s)) |
| | 1947 | |
|
| 0 | 1948 | | private bool CanSelect(int index) { return index >= 0 && index < Length; } |
| | 1949 | |
|
| 0 | 1950 | | private bool CanSelect(Vector2 position) { return selection.Length > 0 ? selection.Any(s => IsPositionWithinElem |
| | 1951 | |
|
| 0 | 1952 | | private bool IsPositionWithinElement(Vector2 position, int index) { return CanSelect(index) ? elementRects[index |
| | 1953 | |
|
| | 1954 | | private bool IsElementExpandable(SerializedProperty element) |
| | 1955 | | { |
| | 1956 | |
|
| 0 | 1957 | | switch (elementDisplayType) |
| | 1958 | | { |
| | 1959 | |
|
| | 1960 | | case ElementDisplayType.Auto: |
| | 1961 | |
|
| 0 | 1962 | | return element.hasVisibleChildren && IsTypeExpandable(element.propertyType); |
| | 1963 | |
|
| | 1964 | | case ElementDisplayType.Expandable: |
| 0 | 1965 | | return true; |
| | 1966 | | case ElementDisplayType.SingleLine: |
| 0 | 1967 | | return false; |
| | 1968 | | } |
| | 1969 | |
|
| 0 | 1970 | | return false; |
| | 1971 | | } |
| | 1972 | |
|
| | 1973 | | private bool IsTypeExpandable(SerializedPropertyType type) |
| | 1974 | | { |
| | 1975 | |
|
| | 1976 | | switch (type) |
| | 1977 | | { |
| | 1978 | |
|
| | 1979 | | case SerializedPropertyType.Generic: |
| | 1980 | | case SerializedPropertyType.Vector4: |
| | 1981 | | case SerializedPropertyType.Quaternion: |
| | 1982 | | case SerializedPropertyType.ArraySize: |
| | 1983 | |
|
| 0 | 1984 | | return true; |
| | 1985 | |
|
| | 1986 | | default: |
| | 1987 | |
|
| 0 | 1988 | | return false; |
| | 1989 | | } |
| | 1990 | | } |
| | 1991 | |
|
| | 1992 | | // |
| | 1993 | | // -- LIST STYLE -- |
| | 1994 | | // |
| | 1995 | |
|
| | 1996 | | static class Style |
| | 1997 | | { |
| | 1998 | |
|
| | 1999 | | internal const string PAGE_INFO_FORMAT = "{0} / {1}"; |
| | 2000 | |
|
| | 2001 | | internal static GUIContent iconToolbarPlus; |
| | 2002 | | internal static GUIContent iconToolbarPlusMore; |
| | 2003 | | internal static GUIContent iconToolbarMinus; |
| | 2004 | | internal static GUIContent iconPagePrev; |
| | 2005 | | internal static GUIContent iconPageNext; |
| | 2006 | | internal static GUIContent iconPagePopup; |
| | 2007 | |
|
| | 2008 | | internal static GUIStyle paginationText; |
| | 2009 | | internal static GUIStyle pageSizeTextField; |
| | 2010 | | internal static GUIStyle draggingHandle; |
| | 2011 | | internal static GUIStyle headerBackground; |
| | 2012 | | internal static GUIStyle footerBackground; |
| | 2013 | | internal static GUIStyle paginationHeader; |
| | 2014 | | internal static GUIStyle boxBackground; |
| | 2015 | | internal static GUIStyle preButton; |
| | 2016 | | internal static GUIStyle elementBackground; |
| | 2017 | | internal static GUIStyle verticalLabel; |
| | 2018 | | internal static GUIContent expandButton; |
| | 2019 | | internal static GUIContent collapseButton; |
| | 2020 | | internal static GUIContent sortAscending; |
| | 2021 | | internal static GUIContent sortDescending; |
| | 2022 | |
|
| | 2023 | | internal static GUIContent listIcon; |
| | 2024 | |
|
| | 2025 | | static Style() |
| | 2026 | | { |
| | 2027 | |
|
| 0 | 2028 | | iconToolbarPlus = EditorGUIUtility.IconContent("Toolbar Plus", "Add to list"); |
| 0 | 2029 | | iconToolbarPlusMore = EditorGUIUtility.IconContent("Toolbar Plus More", "Choose to add to list"); |
| 0 | 2030 | | iconToolbarMinus = EditorGUIUtility.IconContent("Toolbar Minus", "Remove selection from list"); |
| 0 | 2031 | | iconPagePrev = EditorGUIUtility.IconContent("Animation.PrevKey", "Previous page"); |
| 0 | 2032 | | iconPageNext = EditorGUIUtility.IconContent("Animation.NextKey", "Next page"); |
| | 2033 | |
|
| | 2034 | | #if UNITY_2018_3_OR_NEWER |
| 0 | 2035 | | iconPagePopup = EditorGUIUtility.IconContent("ShurikenPopup", "Select page"); |
| | 2036 | | #else |
| | 2037 | | iconPagePopup = EditorGUIUtility.IconContent("MiniPopupNoBg", "Select page"); |
| | 2038 | | #endif |
| 0 | 2039 | | paginationText = new GUIStyle(); |
| 0 | 2040 | | paginationText.margin = new RectOffset(2, 2, 0, 0); |
| 0 | 2041 | | paginationText.fontSize = EditorStyles.miniTextField.fontSize; |
| 0 | 2042 | | paginationText.font = EditorStyles.miniFont; |
| 0 | 2043 | | paginationText.normal.textColor = EditorStyles.miniTextField.normal.textColor; |
| 0 | 2044 | | paginationText.alignment = TextAnchor.UpperLeft; |
| 0 | 2045 | | paginationText.clipping = TextClipping.Clip; |
| | 2046 | |
|
| 0 | 2047 | | pageSizeTextField = new GUIStyle("RL Footer"); |
| 0 | 2048 | | pageSizeTextField.alignment = TextAnchor.MiddleLeft; |
| 0 | 2049 | | pageSizeTextField.clipping = TextClipping.Clip; |
| 0 | 2050 | | pageSizeTextField.fixedHeight = 0; |
| 0 | 2051 | | pageSizeTextField.padding = new RectOffset(3, 0, 0, 0); |
| 0 | 2052 | | pageSizeTextField.overflow = new RectOffset(0, 0, -2, -3); |
| 0 | 2053 | | pageSizeTextField.contentOffset = new Vector2(0, -1); |
| 0 | 2054 | | pageSizeTextField.font = EditorStyles.miniFont; |
| 0 | 2055 | | pageSizeTextField.fontSize = EditorStyles.miniTextField.fontSize; |
| 0 | 2056 | | pageSizeTextField.fontStyle = FontStyle.Normal; |
| 0 | 2057 | | pageSizeTextField.wordWrap = false; |
| | 2058 | |
|
| 0 | 2059 | | draggingHandle = new GUIStyle("RL DragHandle"); |
| 0 | 2060 | | headerBackground = new GUIStyle("RL Header"); |
| 0 | 2061 | | footerBackground = new GUIStyle("RL Footer"); |
| | 2062 | | //paginationHeader = new GUIStyle("RectangleToolHBar"); |
| 0 | 2063 | | paginationHeader = new GUIStyle("RL Element"); |
| 0 | 2064 | | paginationHeader.border = new RectOffset(2, 3, 2, 3); |
| 0 | 2065 | | elementBackground = new GUIStyle("RL Element"); |
| 0 | 2066 | | elementBackground.border = new RectOffset(2, 3, 2, 3); |
| 0 | 2067 | | verticalLabel = new GUIStyle(EditorStyles.label); |
| 0 | 2068 | | verticalLabel.alignment = TextAnchor.UpperLeft; |
| 0 | 2069 | | verticalLabel.contentOffset = new Vector2(10, 3); |
| 0 | 2070 | | boxBackground = new GUIStyle("RL Background"); |
| 0 | 2071 | | boxBackground.border = new RectOffset(6, 3, 3, 6); |
| 0 | 2072 | | preButton = new GUIStyle("RL FooterButton"); |
| | 2073 | |
|
| 0 | 2074 | | expandButton = EditorGUIUtility.IconContent("winbtn_win_max"); |
| 0 | 2075 | | expandButton.tooltip = "Expand All Elements"; |
| | 2076 | |
|
| 0 | 2077 | | collapseButton = EditorGUIUtility.IconContent("winbtn_win_min"); |
| 0 | 2078 | | collapseButton.tooltip = "Collapse All Elements"; |
| | 2079 | |
|
| 0 | 2080 | | sortAscending = EditorGUIUtility.IconContent("align_vertically_bottom"); |
| 0 | 2081 | | sortAscending.tooltip = "Sort Ascending"; |
| | 2082 | |
|
| 0 | 2083 | | sortDescending = EditorGUIUtility.IconContent("align_vertically_top"); |
| 0 | 2084 | | sortDescending.tooltip = "Sort Descending"; |
| | 2085 | |
|
| 0 | 2086 | | listIcon = EditorGUIUtility.IconContent("align_horizontally_right"); |
| 0 | 2087 | | } |
| | 2088 | | } |
| | 2089 | |
|
| | 2090 | | // |
| | 2091 | | // -- DRAG LIST -- |
| | 2092 | | // |
| | 2093 | |
|
| | 2094 | | struct DragList |
| | 2095 | | { |
| | 2096 | |
|
| | 2097 | | private int startIndex; |
| | 2098 | | private DragElement[] elements; |
| | 2099 | | private int length; |
| | 2100 | |
|
| | 2101 | | internal DragList(int length) |
| | 2102 | | { |
| | 2103 | |
|
| 0 | 2104 | | this.length = length; |
| | 2105 | |
|
| 0 | 2106 | | startIndex = 0; |
| 0 | 2107 | | elements = new DragElement[length]; |
| 0 | 2108 | | } |
| | 2109 | |
|
| 0 | 2110 | | internal int StartIndex { get { return startIndex; } } |
| | 2111 | |
|
| 0 | 2112 | | internal int Length { get { return length; } } |
| | 2113 | |
|
| 0 | 2114 | | internal DragElement[] Elements { get { return elements; } set { elements = value; } } |
| | 2115 | |
|
| 0 | 2116 | | internal DragElement this[int index] { get { return elements[index]; } set { elements[index] = value; } } |
| | 2117 | |
|
| | 2118 | | internal void Resize(int start, int length) |
| | 2119 | | { |
| | 2120 | |
|
| 0 | 2121 | | startIndex = start; |
| | 2122 | |
|
| 0 | 2123 | | this.length = length; |
| | 2124 | |
|
| 0 | 2125 | | if (elements.Length != length) |
| | 2126 | | { |
| | 2127 | |
|
| 0 | 2128 | | System.Array.Resize(ref elements, length); |
| | 2129 | | } |
| 0 | 2130 | | } |
| | 2131 | |
|
| | 2132 | | internal void SortByIndex() |
| | 2133 | | { |
| 0 | 2134 | | System.Array.Sort(elements, (a, b) => |
| | 2135 | | { |
| | 2136 | |
|
| 0 | 2137 | | if (b.selected) |
| | 2138 | | { |
| | 2139 | |
|
| 0 | 2140 | | return a.selected ? a.startIndex.CompareTo(b.startIndex) : 1; |
| | 2141 | | } |
| 0 | 2142 | | else if (a.selected) |
| | 2143 | | { |
| | 2144 | |
|
| 0 | 2145 | | return b.selected ? b.startIndex.CompareTo(a.startIndex) : -1; |
| | 2146 | | } |
| | 2147 | |
|
| 0 | 2148 | | return a.startIndex.CompareTo(b.startIndex); |
| | 2149 | | }); |
| 0 | 2150 | | } |
| | 2151 | |
|
| | 2152 | | internal void RecordState() |
| | 2153 | | { |
| | 2154 | |
|
| 0 | 2155 | | for (int i = 0; i < length; i++) |
| | 2156 | | { |
| | 2157 | |
|
| 0 | 2158 | | elements[i].RecordState(); |
| | 2159 | | } |
| 0 | 2160 | | } |
| | 2161 | |
|
| | 2162 | | internal void RestoreState(SerializedProperty list) |
| | 2163 | | { |
| | 2164 | |
|
| 0 | 2165 | | for (int i = 0; i < length; i++) |
| | 2166 | | { |
| | 2167 | |
|
| 0 | 2168 | | elements[i].RestoreState(list.GetArrayElementAtIndex(i + startIndex)); |
| | 2169 | | } |
| 0 | 2170 | | } |
| | 2171 | |
|
| 0 | 2172 | | internal void SortByPosition() { System.Array.Sort(elements, (a, b) => a.desiredRect.center.y.CompareTo(b.de |
| | 2173 | |
|
| 0 | 2174 | | internal int GetIndexFromSelection(int index) { return System.Array.FindIndex(elements, t => t.startIndex == |
| | 2175 | | } |
| | 2176 | |
|
| | 2177 | | // |
| | 2178 | | // -- DRAG ELEMENT -- |
| | 2179 | | // |
| | 2180 | |
|
| | 2181 | | struct DragElement |
| | 2182 | | { |
| | 2183 | |
|
| | 2184 | | internal SerializedProperty property; |
| | 2185 | | internal int startIndex; |
| | 2186 | | internal float dragOffset; |
| | 2187 | | internal bool selected; |
| | 2188 | | internal Rect rect; |
| | 2189 | | internal Rect desiredRect; |
| | 2190 | |
|
| | 2191 | | private bool isExpanded; |
| | 2192 | | private Dictionary<int, bool> states; |
| | 2193 | |
|
| | 2194 | | internal bool Overlaps(Rect value, int index, int direction) |
| | 2195 | | { |
| | 2196 | |
|
| 0 | 2197 | | if (direction < 0 && index < startIndex) |
| | 2198 | | { |
| | 2199 | |
|
| 0 | 2200 | | return desiredRect.yMin < value.center.y; |
| | 2201 | | } |
| 0 | 2202 | | else if (direction > 0 && index > startIndex) |
| | 2203 | | { |
| | 2204 | |
|
| 0 | 2205 | | return desiredRect.yMax > value.center.y; |
| | 2206 | | } |
| | 2207 | |
|
| 0 | 2208 | | return false; |
| | 2209 | | } |
| | 2210 | |
|
| | 2211 | | internal void RecordState() |
| | 2212 | | { |
| | 2213 | |
|
| 0 | 2214 | | states = new Dictionary<int, bool>(); |
| 0 | 2215 | | isExpanded = property.isExpanded; |
| | 2216 | |
|
| 0 | 2217 | | Iterate(this, property, (DragElement e, SerializedProperty p, int index) => |
| | 2218 | | { |
| | 2219 | |
|
| 0 | 2220 | | e.states[index] = p.isExpanded; |
| 0 | 2221 | | }); |
| 0 | 2222 | | } |
| | 2223 | |
|
| | 2224 | | internal void RestoreState(SerializedProperty property) |
| | 2225 | | { |
| | 2226 | |
|
| 0 | 2227 | | property.isExpanded = isExpanded; |
| | 2228 | |
|
| 0 | 2229 | | Iterate(this, property, (DragElement e, SerializedProperty p, int index) => |
| | 2230 | | { |
| | 2231 | |
|
| 0 | 2232 | | p.isExpanded = e.states[index]; |
| 0 | 2233 | | }); |
| 0 | 2234 | | } |
| | 2235 | |
|
| | 2236 | | private static void Iterate(DragElement element, SerializedProperty property, System.Action<DragElement, Ser |
| | 2237 | | { |
| | 2238 | |
|
| 0 | 2239 | | SerializedProperty copy = property.Copy(); |
| 0 | 2240 | | SerializedProperty end = copy.GetEndProperty(); |
| | 2241 | |
|
| 0 | 2242 | | int index = 0; |
| | 2243 | |
|
| 0 | 2244 | | while (copy.NextVisible(true) && !SerializedProperty.EqualContents(copy, end)) |
| | 2245 | | { |
| | 2246 | |
|
| 0 | 2247 | | if (copy.hasVisibleChildren) |
| | 2248 | | { |
| | 2249 | |
|
| 0 | 2250 | | action(element, copy, index); |
| 0 | 2251 | | index++; |
| | 2252 | | } |
| | 2253 | | } |
| 0 | 2254 | | } |
| | 2255 | | } |
| | 2256 | |
|
| | 2257 | | // |
| | 2258 | | // -- SLIDE GROUP -- |
| | 2259 | | // |
| | 2260 | |
|
| | 2261 | | class SlideGroup |
| | 2262 | | { |
| | 2263 | |
|
| | 2264 | | private Dictionary<int, Rect> animIDs; |
| | 2265 | |
|
| 0 | 2266 | | public SlideGroup() { animIDs = new Dictionary<int, Rect>(); } |
| | 2267 | |
|
| | 2268 | | public Rect GetRect(int id, Rect r, float easing) |
| | 2269 | | { |
| | 2270 | |
|
| 0 | 2271 | | if (Event.current.type != EventType.Repaint) |
| | 2272 | | { |
| | 2273 | |
|
| 0 | 2274 | | return r; |
| | 2275 | | } |
| | 2276 | |
|
| 0 | 2277 | | if (!animIDs.ContainsKey(id)) |
| | 2278 | | { |
| | 2279 | |
|
| 0 | 2280 | | animIDs.Add(id, r); |
| 0 | 2281 | | return r; |
| | 2282 | | } |
| | 2283 | | else |
| | 2284 | | { |
| | 2285 | |
|
| 0 | 2286 | | Rect rect = animIDs[id]; |
| | 2287 | |
|
| 0 | 2288 | | if (rect.y != r.y) |
| | 2289 | | { |
| | 2290 | |
|
| 0 | 2291 | | float delta = r.y - rect.y; |
| 0 | 2292 | | float absDelta = Mathf.Abs(delta); |
| | 2293 | |
|
| | 2294 | | //if the distance between current rect and target is too large, then move the element towards th |
| | 2295 | |
|
| 0 | 2296 | | if (absDelta > (rect.height * 2)) |
| | 2297 | | { |
| | 2298 | |
|
| 0 | 2299 | | r.y = delta > 0 ? r.y - rect.height : r.y + rect.height; |
| 0 | 2300 | | } |
| 0 | 2301 | | else if (absDelta > 0.5) |
| | 2302 | | { |
| | 2303 | |
|
| 0 | 2304 | | r.y = Mathf.Lerp(rect.y, r.y, easing); |
| | 2305 | | } |
| | 2306 | |
|
| 0 | 2307 | | animIDs[id] = r; |
| 0 | 2308 | | HandleUtility.Repaint(); |
| | 2309 | | } |
| | 2310 | |
|
| 0 | 2311 | | return r; |
| | 2312 | | } |
| | 2313 | | } |
| | 2314 | |
|
| | 2315 | | public Rect SetRect(int id, Rect rect) |
| | 2316 | | { |
| | 2317 | |
|
| 0 | 2318 | | if (animIDs.ContainsKey(id)) |
| | 2319 | | { |
| | 2320 | |
|
| 0 | 2321 | | animIDs[id] = rect; |
| 0 | 2322 | | } |
| | 2323 | | else |
| | 2324 | | { |
| | 2325 | |
|
| 0 | 2326 | | animIDs.Add(id, rect); |
| | 2327 | | } |
| | 2328 | |
|
| 0 | 2329 | | return rect; |
| | 2330 | | } |
| | 2331 | | } |
| | 2332 | |
|
| | 2333 | | // |
| | 2334 | | // -- PAGINATION -- |
| | 2335 | | // |
| | 2336 | |
|
| | 2337 | | struct Pagination |
| | 2338 | | { |
| | 2339 | |
|
| | 2340 | | internal bool enabled; |
| | 2341 | | internal int fixedPageSize; |
| | 2342 | | internal int customPageSize; |
| | 2343 | | internal int page; |
| | 2344 | |
|
| 0 | 2345 | | internal bool usePagination { get { return enabled && pageSize > 0; } } |
| | 2346 | |
|
| 0 | 2347 | | internal int pageSize { get { return fixedPageSize > 0 ? fixedPageSize : customPageSize; } } |
| | 2348 | |
|
| | 2349 | | internal int GetVisibleLength(int total) |
| | 2350 | | { |
| | 2351 | |
|
| | 2352 | | int start, end; |
| | 2353 | |
|
| 0 | 2354 | | if (GetVisibleRange(total, out start, out end)) |
| | 2355 | | { |
| | 2356 | |
|
| 0 | 2357 | | return end - start; |
| | 2358 | | } |
| | 2359 | |
|
| 0 | 2360 | | return total; |
| | 2361 | | } |
| | 2362 | |
|
| 0 | 2363 | | internal int GetPageForIndex(int index) { return usePagination ? Mathf.FloorToInt(index / (float)pageSize) : |
| | 2364 | |
|
| 0 | 2365 | | internal int GetPageCount(int total) { return usePagination ? Mathf.CeilToInt(total / (float)pageSize) : 1; |
| | 2366 | |
|
| | 2367 | | internal bool GetVisibleRange(int total, out int start, out int end) |
| | 2368 | | { |
| | 2369 | |
|
| 0 | 2370 | | if (usePagination) |
| | 2371 | | { |
| | 2372 | |
|
| 0 | 2373 | | int size = pageSize; |
| | 2374 | |
|
| 0 | 2375 | | start = Mathf.Clamp(page * size, 0, total - 1); |
| 0 | 2376 | | end = Mathf.Min(start + size, total); |
| 0 | 2377 | | return true; |
| | 2378 | | } |
| | 2379 | |
|
| 0 | 2380 | | start = 0; |
| 0 | 2381 | | end = total; |
| 0 | 2382 | | return false; |
| | 2383 | | } |
| | 2384 | | } |
| | 2385 | |
|
| | 2386 | | // |
| | 2387 | | // -- SELECTION -- |
| | 2388 | | // |
| | 2389 | |
|
| | 2390 | | class ListSelection : IEnumerable<int> |
| | 2391 | | { |
| | 2392 | |
|
| | 2393 | | private List<int> indexes; |
| | 2394 | |
|
| | 2395 | | internal int? firstSelected; |
| | 2396 | |
|
| 0 | 2397 | | public ListSelection() { indexes = new List<int>(); } |
| | 2398 | |
|
| 0 | 2399 | | public ListSelection(int[] indexes) { this.indexes = new List<int>(indexes); } |
| | 2400 | |
|
| 0 | 2401 | | public int First { get { return indexes.Count > 0 ? indexes[0] : -1; } } |
| | 2402 | |
|
| 0 | 2403 | | public int Last { get { return indexes.Count > 0 ? indexes[indexes.Count - 1] : -1; } } |
| | 2404 | |
|
| 0 | 2405 | | public int Length { get { return indexes.Count; } } |
| | 2406 | |
|
| | 2407 | | public int this[int index] |
| | 2408 | | { |
| 0 | 2409 | | get { return indexes[index]; } |
| | 2410 | | set |
| | 2411 | | { |
| | 2412 | |
|
| 0 | 2413 | | int oldIndex = indexes[index]; |
| | 2414 | |
|
| 0 | 2415 | | indexes[index] = value; |
| | 2416 | |
|
| 0 | 2417 | | if (oldIndex == firstSelected) |
| | 2418 | | { |
| | 2419 | |
|
| 0 | 2420 | | firstSelected = value; |
| | 2421 | | } |
| 0 | 2422 | | } |
| | 2423 | | } |
| | 2424 | |
|
| 0 | 2425 | | public bool Contains(int index) { return indexes.Contains(index); } |
| | 2426 | |
|
| | 2427 | | public void Clear() |
| | 2428 | | { |
| | 2429 | |
|
| 0 | 2430 | | indexes.Clear(); |
| 0 | 2431 | | firstSelected = null; |
| 0 | 2432 | | } |
| | 2433 | |
|
| | 2434 | | public void SelectWhenNoAction(int index, Event evt) |
| | 2435 | | { |
| | 2436 | |
|
| 0 | 2437 | | if (!EditorGUI.actionKey && !evt.shift) |
| | 2438 | | { |
| | 2439 | |
|
| 0 | 2440 | | Select(index); |
| | 2441 | | } |
| 0 | 2442 | | } |
| | 2443 | |
|
| | 2444 | | public void Select(int index) |
| | 2445 | | { |
| | 2446 | |
|
| 0 | 2447 | | indexes.Clear(); |
| 0 | 2448 | | indexes.Add(index); |
| | 2449 | |
|
| 0 | 2450 | | firstSelected = index; |
| 0 | 2451 | | } |
| | 2452 | |
|
| | 2453 | | public void Remove(int index) |
| | 2454 | | { |
| | 2455 | |
|
| 0 | 2456 | | if (indexes.Contains(index)) |
| | 2457 | | { |
| | 2458 | |
|
| 0 | 2459 | | indexes.Remove(index); |
| | 2460 | | } |
| 0 | 2461 | | } |
| | 2462 | |
|
| | 2463 | | public void AppendWithAction(int index, Event evt) |
| | 2464 | | { |
| | 2465 | |
|
| 0 | 2466 | | if (EditorGUI.actionKey) |
| | 2467 | | { |
| | 2468 | |
|
| 0 | 2469 | | if (Contains(index)) |
| | 2470 | | { |
| | 2471 | |
|
| 0 | 2472 | | Remove(index); |
| 0 | 2473 | | } |
| | 2474 | | else |
| | 2475 | | { |
| | 2476 | |
|
| 0 | 2477 | | Append(index); |
| 0 | 2478 | | firstSelected = index; |
| | 2479 | | } |
| 0 | 2480 | | } |
| 0 | 2481 | | else if (evt.shift && indexes.Count > 0 && firstSelected.HasValue) |
| | 2482 | | { |
| | 2483 | |
|
| 0 | 2484 | | indexes.Clear(); |
| | 2485 | |
|
| 0 | 2486 | | AppendRange(firstSelected.Value, index); |
| 0 | 2487 | | } |
| 0 | 2488 | | else if (!Contains(index)) |
| | 2489 | | { |
| | 2490 | |
|
| 0 | 2491 | | Select(index); |
| | 2492 | | } |
| 0 | 2493 | | } |
| | 2494 | |
|
| | 2495 | | public void Sort() |
| | 2496 | | { |
| | 2497 | |
|
| 0 | 2498 | | if (indexes.Count > 0) |
| | 2499 | | { |
| | 2500 | |
|
| 0 | 2501 | | indexes.Sort(); |
| | 2502 | | } |
| 0 | 2503 | | } |
| | 2504 | |
|
| | 2505 | | public void Sort(System.Comparison<int> comparison) |
| | 2506 | | { |
| | 2507 | |
|
| 0 | 2508 | | if (indexes.Count > 0) |
| | 2509 | | { |
| | 2510 | |
|
| 0 | 2511 | | indexes.Sort(comparison); |
| | 2512 | | } |
| 0 | 2513 | | } |
| | 2514 | |
|
| 0 | 2515 | | public int[] ToArray() { return indexes.ToArray(); } |
| | 2516 | |
|
| | 2517 | | public ListSelection Clone() |
| | 2518 | | { |
| | 2519 | |
|
| 0 | 2520 | | ListSelection clone = new ListSelection(ToArray()); |
| 0 | 2521 | | clone.firstSelected = firstSelected; |
| | 2522 | |
|
| 0 | 2523 | | return clone; |
| | 2524 | | } |
| | 2525 | |
|
| | 2526 | | internal void Trim(int min, int max) |
| | 2527 | | { |
| | 2528 | |
|
| 0 | 2529 | | int i = indexes.Count; |
| | 2530 | |
|
| 0 | 2531 | | while (--i > -1) |
| | 2532 | | { |
| | 2533 | |
|
| 0 | 2534 | | int index = indexes[i]; |
| | 2535 | |
|
| 0 | 2536 | | if (index < min || index >= max) |
| | 2537 | | { |
| | 2538 | |
|
| 0 | 2539 | | if (index == firstSelected && i > 0) |
| | 2540 | | { |
| | 2541 | |
|
| 0 | 2542 | | firstSelected = indexes[i - 1]; |
| | 2543 | | } |
| | 2544 | |
|
| 0 | 2545 | | indexes.RemoveAt(i); |
| | 2546 | | } |
| | 2547 | | } |
| 0 | 2548 | | } |
| | 2549 | |
|
| | 2550 | | internal bool CanRevert(SerializedProperty list) |
| | 2551 | | { |
| | 2552 | |
|
| 0 | 2553 | | if (list.serializedObject.targetObjects.Length == 1) |
| | 2554 | | { |
| | 2555 | |
|
| 0 | 2556 | | for (int i = 0; i < Length; i++) |
| | 2557 | | { |
| | 2558 | |
|
| 0 | 2559 | | if (list.GetArrayElementAtIndex(this[i]).isInstantiatedPrefab) |
| | 2560 | | { |
| | 2561 | |
|
| 0 | 2562 | | return true; |
| | 2563 | | } |
| | 2564 | | } |
| | 2565 | | } |
| | 2566 | |
|
| 0 | 2567 | | return false; |
| | 2568 | | } |
| | 2569 | |
|
| | 2570 | | internal void RevertValues(object userData) |
| | 2571 | | { |
| | 2572 | |
|
| 0 | 2573 | | SerializedProperty list = userData as SerializedProperty; |
| | 2574 | |
|
| 0 | 2575 | | for (int i = 0; i < Length; i++) |
| | 2576 | | { |
| | 2577 | |
|
| 0 | 2578 | | SerializedProperty property = list.GetArrayElementAtIndex(this[i]); |
| | 2579 | |
|
| 0 | 2580 | | if (property.isInstantiatedPrefab) |
| | 2581 | | { |
| | 2582 | |
|
| 0 | 2583 | | property.prefabOverride = false; |
| | 2584 | | } |
| | 2585 | | } |
| | 2586 | |
|
| 0 | 2587 | | list.serializedObject.ApplyModifiedProperties(); |
| 0 | 2588 | | list.serializedObject.Update(); |
| | 2589 | |
|
| 0 | 2590 | | HandleUtility.Repaint(); |
| 0 | 2591 | | } |
| | 2592 | |
|
| | 2593 | | internal void Duplicate(SerializedProperty list) |
| | 2594 | | { |
| | 2595 | |
|
| 0 | 2596 | | int offset = 0; |
| | 2597 | |
|
| 0 | 2598 | | for (int i = 0; i < Length; i++) |
| | 2599 | | { |
| | 2600 | |
|
| 0 | 2601 | | this[i] += offset; |
| | 2602 | |
|
| 0 | 2603 | | list.GetArrayElementAtIndex(this[i]).DuplicateCommand(); |
| 0 | 2604 | | list.serializedObject.ApplyModifiedProperties(); |
| 0 | 2605 | | list.serializedObject.Update(); |
| | 2606 | |
|
| 0 | 2607 | | offset++; |
| | 2608 | | } |
| | 2609 | |
|
| 0 | 2610 | | HandleUtility.Repaint(); |
| 0 | 2611 | | } |
| | 2612 | |
|
| | 2613 | | internal void Delete(SerializedProperty list) |
| | 2614 | | { |
| | 2615 | |
|
| 0 | 2616 | | Sort(); |
| | 2617 | |
|
| 0 | 2618 | | int i = Length; |
| | 2619 | |
|
| 0 | 2620 | | while (--i > -1) |
| | 2621 | | { |
| | 2622 | |
|
| 0 | 2623 | | list.GetArrayElementAtIndex(this[i]).DeleteCommand(); |
| | 2624 | | } |
| | 2625 | |
|
| 0 | 2626 | | Clear(); |
| | 2627 | |
|
| 0 | 2628 | | list.serializedObject.ApplyModifiedProperties(); |
| 0 | 2629 | | list.serializedObject.Update(); |
| | 2630 | |
|
| 0 | 2631 | | HandleUtility.Repaint(); |
| 0 | 2632 | | } |
| | 2633 | |
|
| | 2634 | | private void Append(int index) |
| | 2635 | | { |
| | 2636 | |
|
| 0 | 2637 | | if (index >= 0 && !indexes.Contains(index)) |
| | 2638 | | { |
| | 2639 | |
|
| 0 | 2640 | | indexes.Add(index); |
| | 2641 | | } |
| 0 | 2642 | | } |
| | 2643 | |
|
| | 2644 | | private void AppendRange(int from, int to) |
| | 2645 | | { |
| | 2646 | |
|
| 0 | 2647 | | int dir = (int)Mathf.Sign(to - from); |
| | 2648 | |
|
| 0 | 2649 | | if (dir != 0) |
| | 2650 | | { |
| | 2651 | |
|
| 0 | 2652 | | for (int i = from; i != to; i += dir) |
| | 2653 | | { |
| | 2654 | |
|
| 0 | 2655 | | Append(i); |
| | 2656 | | } |
| | 2657 | | } |
| | 2658 | |
|
| 0 | 2659 | | Append(to); |
| 0 | 2660 | | } |
| | 2661 | |
|
| 0 | 2662 | | public IEnumerator<int> GetEnumerator() { return ((IEnumerable<int>)indexes).GetEnumerator(); } |
| | 2663 | |
|
| 0 | 2664 | | IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable<int>)indexes).GetEnumerator(); } |
| | 2665 | | } |
| | 2666 | |
|
| | 2667 | | // |
| | 2668 | | // -- SORTING -- |
| | 2669 | | // |
| | 2670 | |
|
| | 2671 | | static class ListSort |
| | 2672 | | { |
| | 2673 | |
|
| | 2674 | | private delegate int SortComparision(SerializedProperty p1, SerializedProperty p2); |
| | 2675 | |
|
| | 2676 | | internal static void SortOnProperty(SerializedProperty list, int length, bool descending, string propertyNam |
| | 2677 | | { |
| 0 | 2678 | | BubbleSort(list, length, (p1, p2) => |
| | 2679 | | { |
| | 2680 | |
|
| 0 | 2681 | | SerializedProperty a = p1.FindPropertyRelative(propertyName); |
| 0 | 2682 | | SerializedProperty b = p2.FindPropertyRelative(propertyName); |
| | 2683 | |
|
| 0 | 2684 | | if (a != null && b != null && a.propertyType == b.propertyType) |
| | 2685 | | { |
| | 2686 | |
|
| 0 | 2687 | | int comparison = Compare(a, b, descending, a.propertyType); |
| | 2688 | |
|
| 0 | 2689 | | return descending ? -comparison : comparison; |
| | 2690 | | } |
| | 2691 | |
|
| 0 | 2692 | | return 0; |
| | 2693 | | }); |
| 0 | 2694 | | } |
| | 2695 | |
|
| | 2696 | | internal static void SortOnType(SerializedProperty list, int length, bool descending, SerializedPropertyType |
| | 2697 | | { |
| 0 | 2698 | | BubbleSort(list, length, (p1, p2) => |
| | 2699 | | { |
| | 2700 | |
|
| 0 | 2701 | | int comparision = Compare(p1, p2, descending, type); |
| | 2702 | |
|
| 0 | 2703 | | return descending ? -comparision : comparision; |
| | 2704 | | }); |
| 0 | 2705 | | } |
| | 2706 | |
|
| | 2707 | | // |
| | 2708 | | // -- PRIVATE -- |
| | 2709 | | // |
| | 2710 | |
|
| | 2711 | | private static void BubbleSort(SerializedProperty list, int length, SortComparision comparision) |
| | 2712 | | { |
| | 2713 | |
|
| 0 | 2714 | | for (int i = 0; i < length; i++) |
| | 2715 | | { |
| | 2716 | |
|
| 0 | 2717 | | SerializedProperty p1 = list.GetArrayElementAtIndex(i); |
| | 2718 | |
|
| 0 | 2719 | | for (int j = i + 1; j < length; j++) |
| | 2720 | | { |
| | 2721 | |
|
| 0 | 2722 | | SerializedProperty p2 = list.GetArrayElementAtIndex(j); |
| | 2723 | |
|
| 0 | 2724 | | if (comparision(p1, p2) > 0) |
| | 2725 | | { |
| | 2726 | |
|
| 0 | 2727 | | list.MoveArrayElement(j, i); |
| | 2728 | | } |
| | 2729 | | } |
| | 2730 | | } |
| 0 | 2731 | | } |
| | 2732 | |
|
| | 2733 | | private static int Compare(SerializedProperty p1, SerializedProperty p2, bool descending, SerializedProperty |
| | 2734 | | { |
| | 2735 | |
|
| 0 | 2736 | | if (p1 == null || p2 == null) |
| | 2737 | | { |
| | 2738 | |
|
| 0 | 2739 | | return 0; |
| | 2740 | | } |
| | 2741 | |
|
| | 2742 | | switch (type) |
| | 2743 | | { |
| | 2744 | |
|
| | 2745 | | case SerializedPropertyType.Boolean: |
| | 2746 | |
|
| 0 | 2747 | | return p1.boolValue.CompareTo(p2.boolValue); |
| | 2748 | |
|
| | 2749 | | case SerializedPropertyType.Character: |
| | 2750 | | case SerializedPropertyType.Enum: |
| | 2751 | | case SerializedPropertyType.Integer: |
| | 2752 | | case SerializedPropertyType.LayerMask: |
| | 2753 | |
|
| 0 | 2754 | | return p1.longValue.CompareTo(p2.longValue); |
| | 2755 | |
|
| | 2756 | | case SerializedPropertyType.Color: |
| | 2757 | |
|
| 0 | 2758 | | return p1.colorValue.grayscale.CompareTo(p2.colorValue.grayscale); |
| | 2759 | |
|
| | 2760 | | case SerializedPropertyType.ExposedReference: |
| | 2761 | |
|
| 0 | 2762 | | return CompareObjects(p1.exposedReferenceValue, p2.exposedReferenceValue, descending); |
| | 2763 | |
|
| | 2764 | | case SerializedPropertyType.Float: |
| | 2765 | |
|
| 0 | 2766 | | return p1.doubleValue.CompareTo(p2.doubleValue); |
| | 2767 | |
|
| | 2768 | | case SerializedPropertyType.ObjectReference: |
| | 2769 | |
|
| 0 | 2770 | | return CompareObjects(p1.objectReferenceValue, p2.objectReferenceValue, descending); |
| | 2771 | |
|
| | 2772 | | case SerializedPropertyType.String: |
| | 2773 | |
|
| 0 | 2774 | | return p1.stringValue.CompareTo(p2.stringValue); |
| | 2775 | |
|
| | 2776 | | default: |
| | 2777 | |
|
| 0 | 2778 | | return 0; |
| | 2779 | | } |
| | 2780 | | } |
| | 2781 | |
|
| | 2782 | | private static int CompareObjects(Object obj1, Object obj2, bool descending) |
| | 2783 | | { |
| | 2784 | |
|
| 0 | 2785 | | if (obj1 && obj2) |
| | 2786 | | { |
| | 2787 | |
|
| 0 | 2788 | | return obj1.name.CompareTo(obj2.name); |
| | 2789 | | } |
| 0 | 2790 | | else if (obj1) |
| | 2791 | | { |
| | 2792 | |
|
| 0 | 2793 | | return descending ? 1 : -1; |
| | 2794 | | } |
| | 2795 | |
|
| 0 | 2796 | | return descending ? -1 : 1; |
| | 2797 | | } |
| | 2798 | | } |
| | 2799 | |
|
| | 2800 | | // |
| | 2801 | | // -- SURROGATE -- |
| | 2802 | | // |
| | 2803 | |
|
| | 2804 | | public struct Surrogate |
| | 2805 | | { |
| | 2806 | |
|
| | 2807 | | public System.Type type; |
| | 2808 | | public bool exactType; |
| | 2809 | | public SurrogateCallback callback; |
| | 2810 | |
|
| | 2811 | | internal bool enabled; |
| | 2812 | |
|
| 0 | 2813 | | public bool HasType { get { return enabled && type != null; } } |
| | 2814 | |
|
| | 2815 | | public Surrogate(System.Type type) |
| 0 | 2816 | | : this(type, null) { } |
| | 2817 | |
|
| | 2818 | | public Surrogate(System.Type type, SurrogateCallback callback) |
| | 2819 | | { |
| | 2820 | |
|
| 0 | 2821 | | this.type = type; |
| 0 | 2822 | | this.callback = callback; |
| | 2823 | |
|
| 0 | 2824 | | enabled = true; |
| 0 | 2825 | | exactType = false; |
| 0 | 2826 | | } |
| | 2827 | |
|
| | 2828 | | public void Invoke(SerializedProperty element, Object objectReference, ReorderableList list) |
| | 2829 | | { |
| | 2830 | |
|
| 0 | 2831 | | if (element != null && callback != null) |
| | 2832 | | { |
| | 2833 | |
|
| 0 | 2834 | | callback.Invoke(element, objectReference, list); |
| | 2835 | | } |
| 0 | 2836 | | } |
| | 2837 | | } |
| | 2838 | |
|
| | 2839 | | // |
| | 2840 | | // -- EXCEPTIONS -- |
| | 2841 | | // |
| | 2842 | |
|
| | 2843 | | class InvalidListException : System.InvalidOperationException |
| | 2844 | | { |
| | 2845 | |
|
| 0 | 2846 | | public InvalidListException() : base("ReorderableList serializedProperty must be an array") { } |
| | 2847 | | } |
| | 2848 | |
|
| | 2849 | | class MissingListExeption : System.ArgumentNullException |
| | 2850 | | { |
| | 2851 | |
|
| 0 | 2852 | | public MissingListExeption() : base("ReorderableList serializedProperty is null") { } |
| | 2853 | | } |
| | 2854 | |
|
| | 2855 | | // |
| | 2856 | | // -- INTERNAL -- |
| | 2857 | | // |
| | 2858 | |
|
| | 2859 | | static class Internals |
| | 2860 | | { |
| | 2861 | |
|
| | 2862 | | private static MethodInfo dragDropValidation; |
| | 2863 | | private static object[] dragDropValidationParams; |
| | 2864 | | private static MethodInfo appendDragDrop; |
| | 2865 | | private static object[] appendDragDropParams; |
| | 2866 | |
|
| | 2867 | | static Internals() |
| | 2868 | | { |
| | 2869 | |
|
| 0 | 2870 | | dragDropValidation = System.Type.GetType("UnityEditor.EditorGUI, UnityEditor").GetMethod("ValidateObject |
| 0 | 2871 | | appendDragDrop = typeof(SerializedProperty).GetMethod("AppendFoldoutPPtrValue", BindingFlags.NonPublic | |
| 0 | 2872 | | } |
| | 2873 | |
|
| | 2874 | | internal static Object ValidateObjectDragAndDrop(Object[] references, SerializedProperty property, System.Ty |
| | 2875 | | { |
| | 2876 | |
|
| | 2877 | | #if UNITY_2017_1_OR_NEWER |
| 0 | 2878 | | dragDropValidationParams = GetParams(ref dragDropValidationParams, 4); |
| 0 | 2879 | | dragDropValidationParams[0] = references; |
| 0 | 2880 | | dragDropValidationParams[1] = type; |
| 0 | 2881 | | dragDropValidationParams[2] = property; |
| 0 | 2882 | | dragDropValidationParams[3] = exactType ? 1 : 0; |
| | 2883 | | #else |
| | 2884 | | dragDropValidationParams = GetParams(ref dragDropValidationParams, 3); |
| | 2885 | | dragDropValidationParams[0] = references; |
| | 2886 | | dragDropValidationParams[1] = type; |
| | 2887 | | dragDropValidationParams[2] = property; |
| | 2888 | | #endif |
| 0 | 2889 | | return dragDropValidation.Invoke(null, dragDropValidationParams) as Object; |
| | 2890 | | } |
| | 2891 | |
|
| | 2892 | | internal static void AppendDragAndDropValue(Object obj, SerializedProperty list) |
| | 2893 | | { |
| | 2894 | |
|
| 0 | 2895 | | appendDragDropParams = GetParams(ref appendDragDropParams, 1); |
| 0 | 2896 | | appendDragDropParams[0] = obj; |
| 0 | 2897 | | appendDragDrop.Invoke(list, appendDragDropParams); |
| 0 | 2898 | | } |
| | 2899 | |
|
| | 2900 | | private static object[] GetParams(ref object[] parameters, int count) |
| | 2901 | | { |
| | 2902 | |
|
| 0 | 2903 | | if (parameters == null) |
| | 2904 | | { |
| | 2905 | |
|
| 0 | 2906 | | parameters = new object[count]; |
| | 2907 | | } |
| | 2908 | |
|
| 0 | 2909 | | return parameters; |
| | 2910 | | } |
| | 2911 | | } |
| | 2912 | | } |
| | 2913 | | } |