summaryrefslogtreecommitdiff
path: root/AlienSurvival/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs
diff options
context:
space:
mode:
Diffstat (limited to 'AlienSurvival/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs')
-rw-r--r--AlienSurvival/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs125
1 files changed, 118 insertions, 7 deletions
diff --git a/AlienSurvival/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs b/AlienSurvival/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs
index 0585894..fb9fb53 100644
--- a/AlienSurvival/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs
+++ b/AlienSurvival/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs
@@ -8,13 +8,16 @@ using UnityEditor;
// Scene视图中的自定义handles
public static class EditorHandlesHelper
{
- static int s_ValueScaleHandleHash;
+ static int s_ValueScaleHandleHash;
+ static int s_PositionArrowHandleHash;
// limit value 数据
static float s_StartScale;
static float s_ValueDrag;
static float s_ScaleDrawLength;
+ static Vector3 s_LastPosition;
+
// style
static GUIStyle s_StyleBlackLabel;
static GUIStyle s_StyleWhiteLabel;
@@ -22,9 +25,11 @@ public static class EditorHandlesHelper
static EditorHandlesHelper()
{
- s_ValueScaleHandleHash = "ValueScaleHandleHash".GetHashCode();
+ s_ValueScaleHandleHash = "ValueScaleHandle".GetHashCode();
+ s_PositionArrowHandleHash = "PositionArrowHandle".GetHashCode();
+
- SetupStyles();
+ SetupStyles();
}
static void SetupStyles()
@@ -38,7 +43,14 @@ public static class EditorHandlesHelper
#region 单一handle
- // 箭头
+ /// <summary>
+ /// 无事件箭头
+ /// </summary>
+ /// <param name="position"></param>
+ /// <param name="direction"></param>
+ /// <param name="length"></param>
+ /// <param name="arrowSize"></param>
+ /// <param name="dotted"></param>
public static void Arrow(Vector3 position, Vector3 direction, float length = 1f, float arrowSize = 0.1f, bool dotted = false)
{
direction = direction.normalized;
@@ -49,8 +61,106 @@ public static class EditorHandlesHelper
Handles.ConeHandleCap(0, position + direction * length, Quaternion.LookRotation(direction, Vector3.up), arrowSize, EventType.Repaint);
}
- // 控制数值的箭头
- public static float ScaleValue(float value, Vector3 position, Vector3 direction, float length, Handles.CapFunction capFunc, Quaternion capRot, float size, float snap = 0)
+ /// <summary>
+ /// 处理位置的可拖拽箭头
+ /// </summary>
+ /// <param name="currentValue"></param>
+ /// <param name="position"></param>
+ /// <param name="direction"></param>
+ /// <param name="length"></param>
+ /// <param name="arrowSize"></param>
+ /// <param name="dotted"></param>
+ /// <returns></returns>
+ public static Vector3 PositionArrow(Vector3 position, Vector3 direction, float length, float arrowSize, bool dotted = false)
+ {
+ direction = direction.normalized;
+
+ Vector3 value = Vector3.zero;
+ Handles.CapFunction capFunc = Handles.ConeHandleCap;
+
+ Quaternion capRot = Quaternion.LookRotation(direction, Vector3.up);
+ float size = arrowSize;
+ Vector3 arrowPos = position + direction * length;
+
+ int id = GUIUtility.GetControlID(s_PositionArrowHandleHash, FocusType.Keyboard);
+
+ Event current = Event.current;
+ switch (current.GetTypeForControl(id))
+ {
+ case EventType.MouseDown:
+ if ((HandleUtility.nearestControl == id && current.button == 0) || (GUIUtility.keyboardControl == id && current.button == 2))
+ {
+ s_LastPosition = Camera.current.ScreenToWorldPoint(Event.current.mousePosition);
+
+ GUIUtility.keyboardControl = id;
+ GUIUtility.hotControl = id;
+ current.Use();
+ EditorGUIUtility.SetWantsMouseJumping(1);
+ }
+ break;
+ case EventType.MouseUp:
+ if (GUIUtility.hotControl == id && (current.button == 0 || current.button == 2))
+ {
+ GUIUtility.keyboardControl = 0;
+ GUIUtility.hotControl = 0;
+ current.Use();
+ EditorGUIUtility.SetWantsMouseJumping(0);
+ }
+ break;
+ case EventType.MouseDrag:
+ if (GUIUtility.hotControl == id)
+ {
+ Vector3 worldpos = Camera.current.ScreenToWorldPoint(Event.current.mousePosition);
+ value = worldpos - s_LastPosition;
+
+ s_LastPosition = worldpos;
+
+ GUI.changed = true;
+ current.Use();
+ }
+ break;
+ //case EventType.KeyDown:
+ // if (GUIUtility.hotControl == id && current.keyCode == KeyCode.Escape)
+ // {
+ // GUIUtility.hotControl = 0;
+ // GUI.changed = true;
+ // current.Use();
+ // }
+ // break;
+ case EventType.Repaint:
+ {
+ Color color = Color.white;
+ if (id == GUIUtility.keyboardControl)
+ {
+ color = Handles.color;
+ Handles.color = Handles.selectedColor;
+ }
+ capFunc(id, arrowPos, capRot, size * 0.15f, EventType.Repaint);
+ DottedLine(position, arrowPos, 1f, dotted);
+ if (id == GUIUtility.keyboardControl)
+ {
+ Handles.color = color;
+ }
+ break;
+ }
+ case EventType.Layout:
+ HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(arrowPos, size * 0.15f));
+ break;
+ }
+
+ return value;
+ }
+
+ public static void DottedLine(Vector3 p1, Vector3 p2, float length, bool dotted = true)
+ {
+ if (!dotted)
+ Handles.DrawLine(p1, p2);
+ else
+ Handles.DrawDottedLine(p1, p2, 1f);
+ }
+
+ // 控制数值的箭头
+ public static float ScaleValue(float value, Vector3 position, Vector3 direction, float length, Handles.CapFunction capFunc, Quaternion capRot, float size, float snap = 0)
{
Handles.DrawLine(position, position + direction * length);
value = ScaleValue(value, position + direction, capRot, size, capFunc, snap);
@@ -89,7 +199,8 @@ public static class EditorHandlesHelper
if (GUIUtility.hotControl == id)
{
s_ValueDrag += HandleUtility.niceMouseDelta * 0.01f;
- value = (Handles.SnapValue(s_ValueDrag, snap) + 1f) * s_StartScale;
+ //value = (Handles.SnapValue(s_ValueDrag, snap) + 1f) * s_StartScale;
+ value = Camera.current.ScreenToWorldPoint(Event.current.mousePosition).x - 1;
s_ScaleDrawLength = value / s_StartScale;
GUI.changed = true;
current.Use();