using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif using Gaia.Internal; namespace Gaia { /// /// Handy editor specific utils /// public class EditorUtilsOLD { #if UNITY_EDITOR private bool m_initialized = false; private GUIStyle m_boxStyle; private GUIStyle m_wrapStyle; private GUIStyle m_titleStyle; private GUIStyle m_headingStyle; private GUIStyle m_bodyStyle; private GUIStyle m_linkStyle; private bool m_positionChecked = false; /// /// Access to position checked method /// public bool PositionChecked { get { return m_positionChecked; } } /// /// Initialize editor styles /// public void Initialize() { //Set up the box style if (m_boxStyle == null) { m_boxStyle = new GUIStyle(GUI.skin.box); m_boxStyle.normal.textColor = GUI.skin.label.normal.textColor; m_boxStyle.fontStyle = FontStyle.Bold; m_boxStyle.alignment = TextAnchor.UpperLeft; } //Setup the wrap style if (m_wrapStyle == null) { m_wrapStyle = new GUIStyle(GUI.skin.label); m_wrapStyle.fontStyle = FontStyle.Normal; m_wrapStyle.wordWrap = true; } if (m_bodyStyle == null) { m_bodyStyle = new GUIStyle(GUI.skin.label); m_bodyStyle.fontStyle = FontStyle.Normal; m_bodyStyle.wordWrap = true; } if (m_titleStyle == null) { m_titleStyle = new GUIStyle(m_bodyStyle); m_titleStyle.fontStyle = FontStyle.Bold; m_titleStyle.fontSize = 20; } if (m_headingStyle == null) { m_headingStyle = new GUIStyle(m_bodyStyle); m_headingStyle.fontStyle = FontStyle.Bold; } if (m_linkStyle == null) { m_linkStyle = new GUIStyle(m_bodyStyle); m_linkStyle.wordWrap = false; m_linkStyle.normal.textColor = new Color(0x00 / 255f, 0x78 / 255f, 0xDA / 255f, 1f); m_linkStyle.stretchWidth = false; } m_initialized = true; } /// /// Check and caclulate window adjustment for editor windows relative to scene & game view /// /// /// /// public Rect CheckPosition(Rect position, bool maximized) { if (!m_positionChecked) { m_positionChecked = true; if (!maximized) { //Get scene position Rect scenePosition = new Rect(0f, 0f, 800f, 600f); if (SceneView.lastActiveSceneView != null) { scenePosition = SceneView.lastActiveSceneView.position; } //Check our position if (position.x < scenePosition.xMin || position.x > scenePosition.xMax) { position.x = scenePosition.xMin + (((scenePosition.xMax - scenePosition.xMin) / 2f) - (position.width / 2f)); } if (position.y < scenePosition.yMin || position.y > scenePosition.yMax) { position.y = scenePosition.yMin + 100f; } } } return position; } /// /// Draw the intro /// /// Name of the intro /// Description /// Make description clickable if supplied public void DrawIntro(string name, string description = "", string url = "") { if (!m_initialized) { Initialize(); } if (string.IsNullOrEmpty(name)) { return; } //Text intro GUILayout.BeginVertical(string.Format("{0} ({1})", name, PWApp.CONF.Version), m_boxStyle); GUILayout.Label(""); if (!string.IsNullOrEmpty(description)) { if (string.IsNullOrEmpty(url)) { DrawBody(description); } else { if (DrawClickableBody(description)) { Application.OpenURL(url); } } } GUILayout.EndVertical(); } /// /// Display an image - the image must be of type editor & legacy gui to display /// /// The image /// The width /// The height public void DrawImage(Texture2D image, float width, float height) { GUILayout.Label(image, GUILayout.Width(width), GUILayout.Height(height)); } /// /// Display text in title style /// /// Text to display public void DrawTitle(string text) { if (string.IsNullOrEmpty(text)) { return; } if (!m_initialized) { Initialize(); } GUILayout.Label(text, m_titleStyle); } /// /// Display text in header style /// /// Text to display public void DrawHeader(string text) { if (string.IsNullOrEmpty(text)) { return; } if (!m_initialized) { Initialize(); } GUILayout.Label(text, m_headingStyle); } /// /// Draw header text as clickable object /// /// Text to display /// Display options /// True if it was clicked public bool DrawClickableHeader(string text, params GUILayoutOption[] options) { if (string.IsNullOrEmpty(text)) { return false; } return DrawClickableHeader(GetContent(text), options); } /// /// Draw header text as clickable object /// /// Text to display /// Display options /// True if it was clicked public bool DrawClickableHeader(GUIContent text, params GUILayoutOption[] options) { if (text == null) { return false; } if (!m_initialized) { Initialize(); } var position = GUILayoutUtility.GetRect(text, m_headingStyle, options); Handles.BeginGUI(); Handles.color = m_headingStyle.normal.textColor; Handles.DrawLine(new Vector3(position.xMin, position.yMax), new Vector3(position.xMax, position.yMax)); Handles.color = Color.white; Handles.EndGUI(); EditorGUIUtility.AddCursorRect(position, MouseCursor.Link); return GUI.Button(position, text, m_headingStyle); } /// /// Display text in body style /// /// public void DrawBody(string text) { if (string.IsNullOrEmpty(text)) { return; } GUILayout.Label(text, m_bodyStyle); } /// /// Draw clickable body text /// /// Text to display /// True if it was clicked public bool DrawClickableBody(string text) { if (string.IsNullOrEmpty(text)) { return false; } return DrawLinkBody(GetContent(text)); } /// /// Draw clickable link body /// /// Text to display /// Options /// public bool DrawLinkBody(GUIContent text, params GUILayoutOption[] options) { if (text == null) { return false; } if (!m_initialized) { Initialize(); } var position = GUILayoutUtility.GetRect(text, m_bodyStyle, options); EditorGUIUtility.AddCursorRect(position, MouseCursor.Link); return GUI.Button(position, text, m_bodyStyle); } /// /// Handy wrapper /// /// /// /// public bool DrawLink(string label, params GUILayoutOption[] options) { if (string.IsNullOrEmpty(label)) { return false; } return DrawLinkLabel(GetContent(label), options); } /// /// Draw link label /// /// Label /// Options /// public bool DrawLinkLabel(GUIContent label, params GUILayoutOption[] options) { if (label == null) { return false; } if (!m_initialized) { Initialize(); } var position = GUILayoutUtility.GetRect(label, m_linkStyle, options); Handles.BeginGUI(); Handles.color = m_linkStyle.normal.textColor; Handles.DrawLine(new Vector3(position.xMin, position.yMax), new Vector3(position.xMax, position.yMax)); Handles.color = Color.white; Handles.EndGUI(); EditorGUIUtility.AddCursorRect(position, MouseCursor.Link); return GUI.Button(position, label, m_linkStyle); } /// /// Draw a line in the space provided by the label /// /// Label - used for spacing / volume /// Parameters - eg height / width public void DrawBodyLine(GUIContent label, params GUILayoutOption[] options) { if (label == null) { return; } if (!m_initialized) { Initialize(); } var position = GUILayoutUtility.GetRect(label, m_bodyStyle, options); Handles.BeginGUI(); Handles.color = m_bodyStyle.normal.textColor; Handles.DrawLine(new Vector3(position.xMin, position.y), new Vector3(position.xMax, position.y)); Handles.color = Color.white; Handles.EndGUI(); } /// /// Draw a button that takes editor indentation into account /// /// Text to display /// True if clicked public bool DrawButton(string text) { if (string.IsNullOrEmpty(text)) { return false; } return DrawButton(GetContent(text)); } /// /// Display a button that takes editor indentation into account /// /// Content to display /// True is clicked public bool DrawButton(GUIContent content) { TextAnchor oldalignment = GUI.skin.button.alignment; GUI.skin.button.alignment = TextAnchor.MiddleLeft; Rect btnR = EditorGUILayout.BeginHorizontal(); btnR.xMin += (EditorGUI.indentLevel * 18f); btnR.height += 20f; btnR.width -= 4f; bool result = GUI.Button(btnR, content); EditorGUILayout.EndHorizontal(); GUILayout.Space(22); GUI.skin.button.alignment = oldalignment; return result; } /// /// Get a content label /// /// Name of the label /// Label public GUIContent GetContent(string name) { return new GUIContent(name); } /// /// Get a content label - look the tooltip up if possible /// /// Name of thing /// Tooltips of things /// Content plus tool tip if it exists public GUIContent GetContent(string name, Dictionary tooltips) { string tooltip = ""; if (tooltips.TryGetValue(name, out tooltip)) { return new GUIContent(name, tooltip); } else { return new GUIContent(name); } } /// /// Get the text of the content indexed by name, or the name if not found /// /// Name to search for /// Toolips dictionary /// public string GetContentText(string name, Dictionary tooltips) { string tooltip = ""; if (tooltips.TryGetValue(name, out tooltip)) { return tooltip; } else { return name; } } #endif } }