GUI2.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using Vector2 = UnityEngine.Vector2;
  6. namespace vietlabs.fr2
  7. {
  8. public interface IDrawer
  9. {
  10. bool Draw(Rect rect);
  11. bool DrawLayout();
  12. }
  13. public static class GUI2
  14. {
  15. public static void Color(Action a, Color c, float? alpha = null)
  16. {
  17. if (a == null) return;
  18. var cColor = GUI.color;
  19. if (alpha != null) c.a = alpha.Value;
  20. GUI.color = c;
  21. a();
  22. GUI.color = cColor;
  23. }
  24. public static void ContentColor(Action a, Color c, float? alpha = null)
  25. {
  26. if (a == null) return;
  27. var cColor = GUI.contentColor;
  28. if (alpha != null) c.a = alpha.Value;
  29. GUI.contentColor = c;
  30. a();
  31. GUI.contentColor = cColor;
  32. }
  33. public static void BackgroundColor(Action a, Color c, float? alpha = null)
  34. {
  35. if (a == null) return;
  36. var cColor = GUI.backgroundColor;
  37. if (alpha != null) c.a = alpha.Value;
  38. GUI.backgroundColor = c;
  39. a();
  40. GUI.backgroundColor = cColor;
  41. }
  42. public static Color Theme(Color proColor, Color indieColor)
  43. {
  44. return EditorGUIUtility.isProSkin ? proColor : indieColor;
  45. }
  46. public static Color Alpha(Color c, float a)
  47. {
  48. c.a = a;
  49. return c;
  50. }
  51. public static void Rect(Rect r, Color c, float? alpha = null)
  52. {
  53. var cColor = GUI.color;
  54. if (alpha != null) c.a = alpha.Value;
  55. GUI.color = c;
  56. GUI.DrawTexture(r, Texture2D.whiteTexture);
  57. GUI.color = cColor;
  58. }
  59. public static UnityEngine.Object[] DropZone(string title, float w, float h)
  60. {
  61. var rect = GUILayoutUtility.GetRect(w, h);
  62. GUI.Box(rect, GUIContent.none, EditorStyles.textArea);
  63. var cx = rect.x + w/ 2f;
  64. var cy = rect.y + h/ 2f;
  65. var pz = w / 3f; // plus size
  66. var plusRect = new Rect(cx - pz/2f, (cy - pz/2f), pz, pz);
  67. GUI2.Color(() => { GUI.DrawTexture(plusRect, FR2_Icon.Plus.image, ScaleMode.ScaleToFit); }, UnityEngine.Color.white, 0.1f);
  68. GUI.Label(rect, title, EditorStyles.wordWrappedMiniLabel);
  69. EventType eventType = Event.current.type;
  70. bool isAccepted = false;
  71. if (eventType == EventType.DragUpdated || eventType == EventType.DragPerform)
  72. {
  73. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  74. if (eventType == EventType.DragPerform) {
  75. DragAndDrop.AcceptDrag();
  76. isAccepted = true;
  77. }
  78. Event.current.Use();
  79. }
  80. return isAccepted ? DragAndDrop.objectReferences : null;
  81. }
  82. // public static bool ColorIconButton(Rect r, Texture icon, Vector2? iconOffset, Color? c)
  83. // {
  84. // if (c != null) Rect(r, c.Value);
  85. //
  86. // // align center
  87. // if (iconOffset != null)
  88. // {
  89. // r.x += iconOffset.Value.x;
  90. // r.y += iconOffset.Value.y;
  91. // }
  92. //
  93. // return GUI.Button(r, icon, GUIStyle.none);
  94. // }
  95. public static bool ColorIconButton(Rect r, Texture icon, Color? c)
  96. {
  97. var oColor = GUI.color;
  98. if (c != null) GUI.color = c.Value;
  99. var result = GUI.Button(r, icon, GUIStyle.none);
  100. GUI.color = oColor;
  101. return result;
  102. }
  103. public static bool Toggle(ref bool value, string label, GUIStyle style, params GUILayoutOption[] options)
  104. {
  105. var vv = GUILayout.Toggle(value, label, style, options);
  106. if (vv == value) return false;
  107. value = vv;
  108. return true;
  109. }
  110. public static bool Toggle(ref bool value, Texture2D tex, GUIStyle style, params GUILayoutOption[] options)
  111. {
  112. var vv = GUILayout.Toggle(value, tex, style, options);
  113. if (vv == value) return false;
  114. value = vv;
  115. return true;
  116. }
  117. public static bool Toggle(ref bool value, GUIContent tex, GUIStyle style, params GUILayoutOption[] options)
  118. {
  119. var vv = GUILayout.Toggle(value, tex, style, options);
  120. if (vv == value) return false;
  121. value = vv;
  122. return true;
  123. }
  124. public static bool Toggle(Rect rect, ref bool value, GUIContent tex)
  125. {
  126. var vv = GUI.Toggle(rect, value, tex, GUIStyle.none);
  127. if (vv == value) return false;
  128. value = vv;
  129. return true;
  130. }
  131. public static bool Toggle(Rect rect, ref bool value)
  132. {
  133. var vv = GUI.Toggle(rect, value, GUIContent.none);
  134. if (vv == value) return false;
  135. value = vv;
  136. return true;
  137. }
  138. internal static bool Toggle(bool v, string label, Action<bool> setter)
  139. {
  140. var v1 = GUILayout.Toggle(v, label);
  141. if (v1 == v) return false;
  142. if (setter != null) setter(v1);
  143. return true;
  144. }
  145. internal static Dictionary<string, GUIContent> tooltipCache = new Dictionary<string, GUIContent>();
  146. internal static GUIContent GetTooltip(string tooltip)
  147. {
  148. if (string.IsNullOrEmpty(tooltip)) return GUIContent.none;
  149. GUIContent result;
  150. if (tooltipCache.TryGetValue(tooltip, out result)) return result;
  151. result = new GUIContent(string.Empty, tooltip);
  152. tooltipCache.Add(tooltip, result);
  153. return result;
  154. }
  155. internal static bool ToolbarToggle(ref bool value, Texture icon, Vector2 padding, string tooltip = null)
  156. {
  157. var vv = GUILayout.Toggle(value, GetTooltip(tooltip), EditorStyles.toolbarButton, GUILayout.Width(22f));
  158. if (icon != null)
  159. {
  160. var rect = GUILayoutUtility.GetLastRect();
  161. rect = Padding(rect, padding.x, padding.y);
  162. GUI.DrawTexture(rect, icon, ScaleMode.ScaleToFit);
  163. }
  164. if (vv == value) return false;
  165. value = vv;
  166. return true;
  167. }
  168. // TODO : optimize for performance
  169. public static bool EnumPopup<T>(ref T mode, string label, float labelWidth, GUIStyle style, params GUILayoutOption[] options)
  170. {
  171. var sz = EditorGUIUtility.labelWidth;
  172. EditorGUIUtility.labelWidth = labelWidth;
  173. {
  174. var obj = (Enum)(object) mode;
  175. var vv = EditorGUILayout.EnumPopup(label, obj, style, options);
  176. if (Equals(vv, obj))
  177. {
  178. EditorGUIUtility.labelWidth = sz;
  179. return false;
  180. }
  181. mode = (T)(object)vv;
  182. }
  183. EditorGUIUtility.labelWidth = sz;
  184. return true;
  185. }
  186. public static bool EnumPopup<T>(ref T mode, GUIContent icon, GUIStyle style, params GUILayoutOption[] options)
  187. {
  188. var obj = (Enum)(object) mode;
  189. var cRect = GUILayoutUtility.GetRect(16f, 16f);
  190. cRect.xMin -= 2f;
  191. cRect.yMin += 2f;
  192. GUI.Label(cRect, icon);
  193. var vv = EditorGUILayout.EnumPopup(obj, style, options);
  194. if (Equals(vv, obj))
  195. {
  196. return false;
  197. }
  198. mode = (T)(object)vv;
  199. return true;
  200. }
  201. public static Rect Padding(Rect r, float x, float y)
  202. {
  203. return new Rect(r.x + x, r.y + y, r.width - 2 * x, r.height - 2 * y);
  204. }
  205. public static Rect LeftRect(float w, ref Rect rect)
  206. {
  207. rect.x += w;
  208. rect.width -= w;
  209. return new Rect(rect.x - w, rect.y, w, rect.height);
  210. }
  211. public static Rect RightRect(float w, ref Rect rect)
  212. {
  213. rect.width -= w;
  214. return new Rect(rect.x + rect.width, rect.y, w, rect.height);
  215. }
  216. // -----------------------
  217. private static GUIStyle _miniLabelAlignRight;
  218. public static GUIStyle miniLabelAlignRight
  219. {
  220. get { return _miniLabelAlignRight ?? (
  221. _miniLabelAlignRight = new GUIStyle(EditorStyles.miniLabel) {alignment = TextAnchor.MiddleRight}
  222. );
  223. }
  224. }
  225. public static Color darkRed = new Color(0.5f, .0f, 0f, 1f);
  226. public static Color darkGreen = new Color(0, .5f, 0f, 1f);
  227. public static Color darkBlue = new Color(0, .0f, 0.5f, 1f);
  228. public static Color lightRed = new Color(1f, 0.5f, 0.5f, 1f);
  229. }
  230. }