-
-
Save MadLittleMods/07c8bca634fd272c0c91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Internal; | |
using UnityEditor; | |
using System; | |
using System.Reflection; | |
public static class MLMEditorGUI | |
{ | |
public struct GUIStructure | |
{ | |
public Rect rect; | |
public GUIContent content; | |
} | |
static TextEditor textEditor = new TextEditor(); | |
// Static Contstructor | |
static MLMEditorGUI() | |
{ | |
textEditor = new TextEditor(); | |
} | |
public static bool SelectableFoldout(Rect position, bool isExpanded, GUIContent content, [DefaultValue ("EditorStyles.label")] GUIStyle labelStyle, [DefaultValue ("EditorStyles.foldout")] GUIStyle foldoutStyle) | |
{ | |
// NOTE: | |
// This method is not entirely complete and is only here to provide reference to someone who | |
// may want to look at this for their own GUI elements. | |
Rect controlPosition = EditorGUI.IndentedRect(position); | |
int controlID = 0;//GUIUtility.GetControlID ();//GUIUtility.GetControlID(content, FocusType.Keyboard, position); | |
controlID = GUIUtility.GetControlID(content, FocusType.Keyboard, controlPosition); | |
//controlID = GUIUtility.GetControlID(content, FocusType.Passive, position); | |
//controlID = GUIUtility.GetControlID(content, FocusType.Native, position); | |
//controlID = EditorGUIUtility.GetControlID(FocusType.Native); | |
//controlID = "SelectableFoldout".GetHashCode(); | |
//Debug.Log("Control id: " + controlID); | |
// Add the text cursor | |
Rect cursorRect = new Rect(controlPosition); | |
Vector2 contentSize = labelStyle.CalcSize(content); | |
cursorRect.width = contentSize.x; | |
cursorRect.height = contentSize.y; | |
EditorGUIUtility.AddCursorRect(cursorRect, MouseCursor.Text, controlID); | |
EventType eventType = Event.current.type; | |
switch (eventType) | |
{ | |
case EventType.MouseDown: | |
{ | |
if (controlPosition.Contains (Event.current.mousePosition) && Event.current.button == 0) | |
{ | |
GUIUtility.hotControl = controlID; | |
textEditor.MoveCursorToPosition(Event.current.mousePosition); | |
textEditor.ClampPos(); | |
Event.current.Use(); | |
} | |
else | |
{ | |
textEditor.SelectNone(); | |
} | |
break; | |
} | |
case EventType.MouseUp: | |
{ | |
//Debug.Log("mouse up: " + Event.current.mousePosition); | |
//Debug.Log(position + " " + position.Contains(Event.current.mousePosition)); | |
GUIUtility.hotControl = 0; | |
// Toggle the foldout | |
if(controlPosition.Contains(Event.current.mousePosition)) | |
{ | |
isExpanded = !isExpanded; | |
//Debug.Log(isExpanded); | |
//GUI.changed = true; | |
Event.current.Use(); | |
} | |
break; | |
} | |
case EventType.MouseDrag: | |
{ | |
if(controlPosition.Contains(Event.current.mousePosition)) | |
{ | |
textEditor.SelectToPosition(Event.current.mousePosition); | |
//textEditor.MoveAltCursorToPosition(Event.current.mousePosition); | |
textEditor.ClampPos(); | |
Event.current.Use(); | |
} | |
break; | |
} | |
case EventType.ValidateCommand: | |
{ | |
// Validate command for Copy | |
if (Event.current.commandName == "Copy") | |
{ | |
// without this line we won't get ExecuteCommand | |
Event.current.Use(); | |
} | |
// Validate command for SelectAll | |
if (Event.current.commandName == "SelectAll") | |
{ | |
// without this line we won't get ExecuteCommand | |
Event.current.Use(); | |
} | |
break; | |
} | |
case EventType.ExecuteCommand: | |
{ | |
if (Event.current.commandName == "Copy") | |
{ | |
if(textEditor.hasSelection) | |
{ | |
//Debug.Log("Copied to clipboard: " + textEditor.SelectedText); | |
EditorGUIUtility.systemCopyBuffer = textEditor.SelectedText; | |
Event.current.Use(); | |
} | |
} | |
if (Event.current.commandName == "SelectAll") | |
{ | |
//Debug.Log("Selecting all text"); | |
textEditor.SelectAll(); | |
Event.current.Use(); | |
} | |
break; | |
} | |
case EventType.Repaint: | |
{ | |
//SweepTest_Tests aewrwa = (SweepTest_Tests)GameObject.FindObjectOfType<SweepTest_Tests>(); | |
//aewrwa.asdf = foldoutStyle; | |
GUIStyle foldoutPaintStyle = new GUIStyle(foldoutStyle); | |
foldoutStyle.padding.right = 0; | |
GUIStyle normalPaintStyle = new GUIStyle(labelStyle); | |
//normalPaintStyle.normal.background = null; | |
//normalPaintStyle.active.background = null; | |
normalPaintStyle.margin = new RectOffset(0, 0, 0, 0); | |
normalPaintStyle.padding = new RectOffset(0, 0, 0, 0); | |
GUIStyle selectedPaintStyle = new GUIStyle(labelStyle); | |
//style = EditorStyles.foldoutSelected; | |
Texture2D blueTextSelectTex = new Texture2D(1, 1); | |
blueTextSelectTex.SetPixel(0, 0, new Color(.231f, .404f, .647f)); | |
blueTextSelectTex.Apply(); | |
selectedPaintStyle.margin = new RectOffset(0, 0, 0, 0); | |
selectedPaintStyle.padding = new RectOffset(0, 0, 0, 0); | |
selectedPaintStyle.normal.background = blueTextSelectTex; | |
selectedPaintStyle.normal.textColor = new Color(.298f, .494f, 1f); | |
selectedPaintStyle.hover.background = blueTextSelectTex; | |
selectedPaintStyle.active.background = blueTextSelectTex; | |
selectedPaintStyle.focused.background = blueTextSelectTex; | |
int startSelectIndex = 0; | |
int endSelectIndex = 0; | |
if(textEditor.selectPos < textEditor.pos) | |
{ | |
startSelectIndex = textEditor.selectPos; | |
endSelectIndex = textEditor.pos; | |
} | |
else | |
{ | |
startSelectIndex = textEditor.pos; | |
endSelectIndex = textEditor.selectPos; | |
} | |
//Debug.Log(startSelectIndex + " " + endSelectIndex + ": " + textEditor.hasSelection); | |
// Doesn't work :( | |
//selectedPaintStyle.DrawWithTextSelection(EditorGUI.IndentedRect(controlPosition), content, controlID, firstSelectPos, lastSelectPos); | |
//Debug.Log(textEditor.selectPos + " " + textEditor.pos + " " + paintStyle.GetCursorStringIndex(controlPosition, content, Event.current.mousePosition)); | |
// Draw the foldout arrow | |
//EditorStyles.foldout.Draw(controlPosition, new GUIContent(""), controlID, isExpanded); | |
//paintStyle.Draw(controlPosition, content, controlID, isExpanded); | |
Rect startRect = new Rect(controlPosition); | |
startRect.width = 0; | |
GUIContent foldoutContent = new GUIContent(""); | |
Rect foldoutRectPos = GetHorizPosFromLastPos(new GUIStructure{rect=startRect, content=foldoutContent}, foldoutPaintStyle); | |
foldoutPaintStyle.Draw(foldoutRectPos, foldoutContent, controlID, isExpanded); | |
//Debug.Log("foldout: " + foldoutStructure.rect); | |
//Debug.Log(foldoutPaintStyle.CalcSize(new GUIContent(""))); | |
Rect wholeRectPos = GetHorizPosFromLastPos(new GUIStructure{rect=foldoutRectPos, content=content}, normalPaintStyle); | |
textEditor.position = wholeRectPos; | |
textEditor.style = labelStyle; | |
textEditor.controlID = controlID; | |
textEditor.content = content; | |
textEditor.multiline = false; | |
textEditor.isPasswordField = false; | |
//textEditor.HandleKeyEvent(Event.current); | |
if(textEditor.hasSelection) | |
{ | |
GUIContent firstContent = SubStringStartToEnd(content, 0, startSelectIndex); | |
Rect firstRectPos = GetHorizPosFromLastPos(new GUIStructure{rect=foldoutRectPos, content=firstContent}, normalPaintStyle); | |
normalPaintStyle.Draw(firstRectPos, firstContent, controlID, isExpanded); | |
// Unity likes to trim trailing spaces so we have to add them to the front | |
int spaces = firstContent.text.Length - firstContent.text.TrimEnd(' ').Length; | |
GUIContent selectedContent = SubStringStartToEnd(content, startSelectIndex, endSelectIndex); | |
Rect selectedRectPos = GetHorizPosFromLastPos(new GUIStructure{rect=firstRectPos, content=selectedContent}, selectedPaintStyle); | |
selectedPaintStyle.Draw(selectedRectPos, selectedContent, controlID, isExpanded); | |
GUIContent endContent = SubStringStartToEnd(content, endSelectIndex, content.text.Length); | |
Rect endRectPos = GetHorizPosFromLastPos(new GUIStructure{rect=selectedRectPos, content=endContent}, normalPaintStyle); | |
normalPaintStyle.Draw(endRectPos, endContent, controlID, isExpanded); | |
//Debug.Log(firstRectPos + " " + selectedRectPos + " " + endRectPos); | |
Debug.Log(firstContent.text + ":" + selectedContent.text + ":" + endContent.text); | |
} | |
else | |
{ | |
//foldoutPaintStyle.Draw(controlPosition, content, controlID, isExpanded); | |
normalPaintStyle.Draw(wholeRectPos, content, controlID, isExpanded); | |
} | |
break; | |
} | |
} | |
return isExpanded; | |
} | |
// Alternative construcotr if you don't want to deal with GUIContent | |
public static bool SelectableFoldout(Rect position, bool isExpanded, string text, [DefaultValue ("EditorStyles.label")] GUIStyle labelStyle, [DefaultValue ("EditorStyles.foldout")] GUIStyle foldoutStyle) | |
{ | |
return SelectableFoldout(position, isExpanded, new GUIContent(text), labelStyle, foldoutStyle); | |
} | |
static Rect GetHorizPosFromLastPos(GUIStructure structure, GUIStyle style) | |
{ | |
Vector2 newContentSize = style.CalcSize(structure.content); | |
return new Rect(structure.rect.x + structure.rect.width, structure.rect.y, newContentSize.x, newContentSize.y); | |
} | |
static string SubStringStartToEnd(string msg, int startIndex, int endIndex) | |
{ | |
return msg.Substring(startIndex, endIndex-startIndex); | |
} | |
static GUIContent SubStringStartToEnd(GUIContent content, int startIndex, int endIndex) | |
{ | |
return new GUIContent(SubStringStartToEnd(content.text, startIndex, endIndex)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment