No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UIUtilities.cs 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEngine;
  6. using UnityEngine.UIElements;
  7. namespace UnityEditor.Graphing.Util
  8. {
  9. static class UIUtilities
  10. {
  11. public static bool ItemsReferenceEquals<T>(this IList<T> first, IList<T> second)
  12. {
  13. if (first.Count != second.Count)
  14. {
  15. return false;
  16. }
  17. for (int i = 0; i < first.Count; i++)
  18. {
  19. if (!ReferenceEquals(first[i], second[i]))
  20. {
  21. return false;
  22. }
  23. }
  24. return true;
  25. }
  26. public static int GetHashCode(params object[] objects)
  27. {
  28. return GetHashCode(objects.AsEnumerable());
  29. }
  30. public static int GetHashCode<T>(IEnumerable<T> objects)
  31. {
  32. var hashCode = 17;
  33. foreach (var @object in objects)
  34. {
  35. hashCode = hashCode * 31 + (@object == null ? 79 : @object.GetHashCode());
  36. }
  37. return hashCode;
  38. }
  39. public static IEnumerable<T> ToEnumerable<T>(this T item)
  40. {
  41. yield return item;
  42. }
  43. public static void Add<T>(this VisualElement visualElement, T elementToAdd, Action<T> action)
  44. where T : VisualElement
  45. {
  46. visualElement.Add(elementToAdd);
  47. action(elementToAdd);
  48. }
  49. public static IEnumerable<Type> GetTypesOrNothing(this Assembly assembly)
  50. {
  51. try
  52. {
  53. return assembly.GetTypes();
  54. }
  55. catch
  56. {
  57. return Enumerable.Empty<Type>();
  58. }
  59. }
  60. public static Vector2 CalculateCentroid(IEnumerable<Vector2> nodePositions)
  61. {
  62. Vector2 centroid = Vector2.zero;
  63. int count = 1;
  64. foreach (var position in nodePositions)
  65. {
  66. centroid = centroid + (position - centroid) / count;
  67. ++count;
  68. }
  69. return centroid;
  70. }
  71. }
  72. }