// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) VRMADA, All rights reserved. // // -------------------------------------------------------------------------------------------------------------------- using System.Collections.Generic; using System.Linq; using UnityEditor; using UnityEngine; namespace UltimateXR.Editor { public static partial class UxrEditorUtils { #region Public Types & Data /// /// Default button width used in different inspectors. /// public const int ButtonWidth = 200; /// /// Default value. /// public const float DefaultHandlesAlpha = 0.3f; /// /// Gets or sets the transparency value for handles used in Editor.OnSceneGUI. /// public static float HandlesAlpha { get; set; } = DefaultHandlesAlpha; #endregion #region Public Methods /// /// Creates a stylish foldout editor widget. /// From http://tips.hecomi.com/entry/2016/10/15/004144 /// /// Title shown /// Whether the foldout is expanded or not /// public static bool FoldoutStylish(string title, bool display) { GUIStyle style = new GUIStyle("ShurikenModuleTitle"); style.font = new GUIStyle(EditorStyles.label).font; style.border = new RectOffset(15, 7, 4, 4); style.fixedHeight = 22; style.contentOffset = new Vector2(20f, -2f); var rect = GUILayoutUtility.GetRect(16f, 22f, style); GUI.Box(rect, title, style); var e = Event.current; var toggleRect = new Rect(rect.x + 4f, rect.y + 2f, 13f, 13f); if (e.type == EventType.Repaint) { EditorStyles.foldout.Draw(toggleRect, false, false, display, false); } if (e.type == EventType.MouseDown && rect.Contains(e.mousePosition)) { display = !display; e.Use(); } return display; } /// /// Helper editor UI method to draw a centered button. /// /// Button text and tooltip /// Width in pixels. A negative value will assign the required width for the label /// Whether the button was pressed during the current frame public static bool CenteredButton(GUIContent content, int width = ButtonWidth) { bool pressed = false; EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (width < 0) { if (GUILayout.Button(content)) { pressed = true; } } else if (GUILayout.Button(content, GUILayout.Width(width))) { pressed = true; } GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); return pressed; } /// /// Utility to get the rect where a property line needs to be drawn. /// /// Position passed to the OnGUI method /// Line that needs to be drawn /// Include indentation in the rect? /// Rect to use the property of the given line public static Rect GetRect(Rect position, int line, bool includeIndentation = false) { Rect indentRect = includeIndentation ? EditorGUI.IndentedRect(position) : position; return new Rect(indentRect.x, indentRect.y + EditorGUIUtility.singleLineHeight * line, indentRect.width, EditorGUIUtility.singleLineHeight); } /// /// Utility to get the rect where a property in a horizontal layout needs to be drawn. /// /// Position passed to the OnGUI method /// Line that needs to be drawn /// Total number of columns in the horizontal line /// Column in the horizontal line to get the for /// Separation between columns in the horizontal line /// Padding on the left side /// Padding on the right side /// Include indentation in the rect? /// Rect to use the property of the given line public static Rect GetRect(Rect position, int line, int totalColumns, int column, int separation, int leftPadding = 0, int rightPadding = 0, bool includeIndentation = false) { Rect indentRect = includeIndentation ? EditorGUI.IndentedRect(position) : position; Rect rect = new Rect(indentRect.x, indentRect.y + EditorGUIUtility.singleLineHeight * line, indentRect.width, EditorGUIUtility.singleLineHeight); float elementWidth = (rect.width - leftPadding - rightPadding - (totalColumns - 1) * separation) / totalColumns; float posX = rect.x + leftPadding + column * (separation + elementWidth); return new Rect(posX, rect.y, elementWidth, rect.height); } /// /// Builds a GUIContent array from a set of strings. Some editor UI methods in Unity need GUIContent arrays. /// /// Source strings /// GUIContent array public static GUIContent[] ToGUIContentArray(IEnumerable strings) { GUIContent[] returnArray = new GUIContent[strings.Count()]; int i = 0; foreach (string str in strings) { returnArray[i++] = new GUIContent(str); } return returnArray; } #endregion } }