UIElementHelper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. #if UNITY_2019_1_OR_NEWER
  5. using UnityEngine.UIElements;
  6. #else
  7. using UnityEngine.Experimental.UIElements;
  8. #endif
  9. namespace UnityEditor.Recorder
  10. {
  11. static class UIElementHelper
  12. {
  13. internal static void SetFocusable(VisualElement element)
  14. {
  15. #if UNITY_2019_1_OR_NEWER
  16. element.focusable = true;
  17. #else
  18. element.focusIndex = 0;
  19. #endif
  20. }
  21. internal static void ResetStylePosition(IStyle style)
  22. {
  23. #if UNITY_2019_1_OR_NEWER
  24. style.left = 0;
  25. style.right = 0;
  26. #else
  27. style.positionLeft = 0;
  28. style.positionRight = 0;
  29. #endif
  30. }
  31. internal static void SetDirty(VisualElement element)
  32. {
  33. #if !UNITY_2018_3_OR_NEWER
  34. element.Dirty(ChangeType.Layout | ChangeType.Styles);
  35. #endif
  36. }
  37. internal static void RegisterTrickleDownCallback<T>(VisualElement element, EventCallback<T> callback) where T : EventBase<T>, new()
  38. {
  39. #if UNITY_2018_3_OR_NEWER
  40. element.RegisterCallback(callback, TrickleDown.TrickleDown);
  41. #else
  42. element.RegisterCallback(callback, Capture.Capture);
  43. #endif
  44. }
  45. internal static void SetFlex(VisualElement element, float value)
  46. {
  47. #if UNITY_2019_1_OR_NEWER
  48. element.style.flexGrow = value;
  49. #elif UNITY_2018_3_OR_NEWER
  50. element.style.flex = new Flex(value);
  51. #else
  52. element.style.flex = value;
  53. #endif
  54. }
  55. internal static bool GetToggleValue(Toggle toggle)
  56. {
  57. #if UNITY_2018_2_OR_NEWER
  58. return toggle.value;
  59. #else
  60. return toggle.on;
  61. #endif
  62. }
  63. internal static void SetToggleValue(Toggle toggle, bool value)
  64. {
  65. #if UNITY_2018_2_OR_NEWER
  66. toggle.value = value;
  67. #else
  68. toggle.on = value;
  69. #endif
  70. }
  71. internal static bool MultiIntField(GUIContent label, GUIContent[] subLabels, int[] values)
  72. {
  73. var r = EditorGUILayout.GetControlRect();
  74. var rLabel = r;
  75. rLabel.width = EditorGUIUtility.labelWidth;
  76. EditorGUI.LabelField(rLabel, label);
  77. var rContent = r;
  78. rContent.xMin = rLabel.xMax;
  79. var width = subLabels.Select(l => GUI.skin.label.CalcSize(l).x).Max();
  80. EditorGUI.BeginChangeCheck();
  81. MultiIntField(rContent, subLabels, values, width);
  82. return EditorGUI.EndChangeCheck();
  83. }
  84. internal static bool MultiFloatField(GUIContent label, GUIContent[] subLabels, float[] values)
  85. {
  86. var r = EditorGUILayout.GetControlRect();
  87. var rLabel = r;
  88. rLabel.width = EditorGUIUtility.labelWidth;
  89. EditorGUI.LabelField(rLabel, label);
  90. var rContent = r;
  91. rContent.xMin = rLabel.xMax;
  92. var width = subLabels.Select(l => GUI.skin.label.CalcSize(l).x).Max();
  93. EditorGUI.BeginChangeCheck();
  94. MultiFloatField(rContent, subLabels, values, width);
  95. return EditorGUI.EndChangeCheck();
  96. }
  97. static void MultiIntField(Rect position, IList<GUIContent> subLabels, IList<int> values, float labelWidth)
  98. {
  99. var length = values.Count;
  100. var num = (position.width - (float) (length - 1) * 2f) / (float) length;
  101. var position1 = new Rect(position)
  102. {
  103. width = num
  104. };
  105. var labelWidth1 = EditorGUIUtility.labelWidth;
  106. var indentLevel = EditorGUI.indentLevel;
  107. EditorGUIUtility.labelWidth = labelWidth;
  108. EditorGUI.indentLevel = 0;
  109. for (int index = 0; index < values.Count; ++index)
  110. {
  111. values[index] = EditorGUI.IntField(position1, subLabels[index], values[index]);
  112. position1.x += num + 2f;
  113. }
  114. EditorGUIUtility.labelWidth = labelWidth1;
  115. EditorGUI.indentLevel = indentLevel;
  116. }
  117. static void MultiFloatField(Rect position, IList<GUIContent> subLabels, IList<float> values, float labelWidth)
  118. {
  119. var length = values.Count;
  120. var num = (position.width - (float) (length - 1) * 2f) / (float) length;
  121. var position1 = new Rect(position)
  122. {
  123. width = num
  124. };
  125. var labelWidth1 = EditorGUIUtility.labelWidth;
  126. var indentLevel = EditorGUI.indentLevel;
  127. EditorGUIUtility.labelWidth = labelWidth;
  128. EditorGUI.indentLevel = 0;
  129. for (int index = 0; index < values.Count; ++index)
  130. {
  131. values[index] = EditorGUI.FloatField(position1, subLabels[index], values[index]);
  132. position1.x += num + 2f;
  133. }
  134. EditorGUIUtility.labelWidth = labelWidth1;
  135. EditorGUI.indentLevel = indentLevel;
  136. }
  137. }
  138. }