|
using UnityEditor; |
|
using UnityEngine; |
|
|
|
namespace CleverCrow.ColdIronCity.QuickCursor { |
|
[InitializeOnLoad] |
|
public class GlobalCursor { |
|
const float normalizationFactor = 1.2f; |
|
static Vector3 cursorPosition = Vector3.zero; |
|
static Quaternion cursorRotation = Quaternion.identity; |
|
static readonly Color lineColor = Color.black; |
|
static readonly Color circleColor = Color.red; |
|
static bool shortcutSetPosition = true; |
|
static bool shortcutSetRotation; |
|
|
|
public static bool IsCursorVisible { get; private set; } = true; |
|
|
|
public static float CursorSizeMultiplier => CursorSizeFactor * normalizationFactor; |
|
|
|
public static float CursorSizeFactor { get; private set; } = 1.0f; |
|
|
|
static GlobalCursor () { |
|
SceneView.duringSceneGui += OnScene; |
|
|
|
LoadCursorData(); |
|
} |
|
|
|
static void OnScene (SceneView sceneView) { |
|
if (!IsCursorVisible) return; |
|
|
|
UpdateCursorPositionShortcut(); |
|
DrawCursor(sceneView.camera); |
|
} |
|
|
|
static void UpdateCursorPositionShortcut () { |
|
if (Event.current.shift && Event.current.type == EventType.MouseDown && Event.current.button == 1) { |
|
var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); |
|
if (Physics.Raycast(ray, out var hit)) { |
|
var pos = hit.point; |
|
if (!shortcutSetPosition) pos = cursorPosition; |
|
|
|
var rotation = Quaternion.FromToRotation(Vector3.up, hit.normal); |
|
if (!shortcutSetRotation) rotation = cursorRotation; |
|
|
|
SetPosition(pos, rotation); |
|
} |
|
} |
|
} |
|
|
|
static void DrawCursor (Camera sceneCamera) { |
|
var distanceToCursor = Vector3.Distance(sceneCamera.transform.position, cursorPosition); |
|
var lineLength = distanceToCursor * 0.05f * CursorSizeMultiplier; |
|
var gapSize = lineLength * 0.2f * CursorSizeMultiplier; |
|
var whiteCircleOffset = |
|
-0.001f * distanceToCursor; // Adjust this value to change the distance between the circles. |
|
|
|
Handles.color = lineColor; |
|
|
|
// Draw lines with a gap in the middle for the left/right axes |
|
Handles.DrawLine(cursorPosition + cursorRotation * (Vector3.left * gapSize), |
|
cursorPosition + cursorRotation * (Vector3.left * lineLength)); |
|
Handles.DrawLine(cursorPosition + cursorRotation * (Vector3.right * gapSize), |
|
cursorPosition + cursorRotation * (Vector3.right * lineLength)); |
|
|
|
// Highlight Y-axis in yellow |
|
Handles.color = Color.green; |
|
Handles.DrawLine(cursorPosition + cursorRotation * (Vector3.up * gapSize), |
|
cursorPosition + cursorRotation * (Vector3.up * lineLength)); |
|
Handles.color = lineColor; // Revert to original color |
|
Handles.DrawLine(cursorPosition + cursorRotation * (Vector3.down * gapSize), |
|
cursorPosition + cursorRotation * (Vector3.down * lineLength)); |
|
|
|
// Draw x axis |
|
Handles.DrawLine(cursorPosition + cursorRotation * (Vector3.forward * gapSize), |
|
cursorPosition + cursorRotation * (Vector3.forward * lineLength)); |
|
Handles.DrawLine(cursorPosition + cursorRotation * (Vector3.back * gapSize), |
|
cursorPosition + cursorRotation * (Vector3.back * lineLength)); |
|
|
|
// Draw red circle outline in the center |
|
Handles.color = circleColor; |
|
Handles.DrawWireDisc(cursorPosition, sceneCamera.transform.forward, gapSize); |
|
|
|
// Draw white circle outline next to the red one |
|
Handles.color = Color.white; |
|
Handles.DrawWireDisc(cursorPosition, sceneCamera.transform.forward, gapSize + whiteCircleOffset); |
|
} |
|
|
|
public static void SetPosition (Vector3 newPosition, Quaternion? newRotation = null) { |
|
cursorPosition = newPosition; |
|
if (newRotation.HasValue) cursorRotation = newRotation.Value; |
|
|
|
SceneView.RepaintAll(); |
|
|
|
SaveCursorData(); |
|
} |
|
|
|
public static Vector3 GetCursorPosition () { |
|
return cursorPosition; |
|
} |
|
|
|
public static Quaternion GetCursorRotation () { |
|
return cursorRotation; |
|
} |
|
|
|
public static bool GetUpdatePosition () { |
|
return shortcutSetPosition; |
|
} |
|
|
|
public static bool GetUpdateRotation () { |
|
return shortcutSetRotation; |
|
} |
|
|
|
public static void SetUpdatePosition (bool value) { |
|
shortcutSetPosition = value; |
|
SaveCursorData(); |
|
} |
|
|
|
public static void SetUpdateRotation (bool value) { |
|
shortcutSetRotation = value; |
|
SaveCursorData(); |
|
} |
|
|
|
public static void SetIsCursorVisible (bool value) { |
|
IsCursorVisible = value; |
|
SaveCursorData(); |
|
} |
|
|
|
public static void SetCursorSizeFactor (float value) { |
|
CursorSizeFactor = value; |
|
SaveCursorData(); |
|
} |
|
|
|
static void SaveCursorData () { |
|
EditorPrefs.SetFloat("GlobalCursorPosX", cursorPosition.x); |
|
EditorPrefs.SetFloat("GlobalCursorPosY", cursorPosition.y); |
|
EditorPrefs.SetFloat("GlobalCursorPosZ", cursorPosition.z); |
|
|
|
EditorPrefs.SetFloat("GlobalCursorRotX", cursorRotation.x); |
|
EditorPrefs.SetFloat("GlobalCursorRotY", cursorRotation.y); |
|
EditorPrefs.SetFloat("GlobalCursorRotZ", cursorRotation.z); |
|
EditorPrefs.SetFloat("GlobalCursorRotW", cursorRotation.w); |
|
|
|
EditorPrefs.SetBool("QuickCursorShortcutSetPosition", shortcutSetPosition); |
|
EditorPrefs.SetBool("QuickCursorShortcutSetRotation", shortcutSetRotation); |
|
|
|
EditorPrefs.SetBool("GlobalCursorIsVisible", IsCursorVisible); |
|
EditorPrefs.SetFloat("GlobalCursorSizeMultiplier", CursorSizeFactor); |
|
} |
|
|
|
static void LoadCursorData () { |
|
if (EditorPrefs.HasKey("GlobalCursorPosX")) { |
|
cursorPosition = new Vector3( |
|
EditorPrefs.GetFloat("GlobalCursorPosX"), |
|
EditorPrefs.GetFloat("GlobalCursorPosY"), |
|
EditorPrefs.GetFloat("GlobalCursorPosZ")); |
|
|
|
cursorRotation = new Quaternion( |
|
EditorPrefs.GetFloat("GlobalCursorRotX"), |
|
EditorPrefs.GetFloat("GlobalCursorRotY"), |
|
EditorPrefs.GetFloat("GlobalCursorRotZ"), |
|
EditorPrefs.GetFloat("GlobalCursorRotW")); |
|
|
|
shortcutSetPosition = EditorPrefs.GetBool("QuickCursorShortcutSetPosition"); |
|
shortcutSetRotation = EditorPrefs.GetBool("QuickCursorShortcutSetRotation"); |
|
|
|
IsCursorVisible = EditorPrefs.GetBool("GlobalCursorIsVisible"); |
|
CursorSizeFactor = EditorPrefs.GetFloat("GlobalCursorSizeMultiplier"); |
|
} |
|
} |
|
} |
|
} |