Initial commit

This commit is contained in:
AbstractConcept 2022-09-13 00:36:34 -05:00
commit 3c7cc0c973
8391 changed files with 704313 additions and 0 deletions

View file

@ -0,0 +1,48 @@
using System;
using UnityEngine;
using UnityEditor.U2D.Path.GUIFramework;
namespace UnityEditor.U2D.Path
{
public class CreatePointAction : ClickAction
{
private Control m_PointControl;
public Func<IGUIState, Vector2, Vector3> guiToWorld;
public Action<int, Vector3> onCreatePoint;
public CreatePointAction(Control pointControl, Control edgeControl) : base(edgeControl, 0, false)
{
m_PointControl = pointControl;
}
protected override void OnTrigger(IGUIState guiState)
{
base.OnTrigger(guiState);
var index = hoveredControl.layoutData.index;
var position = GetMousePositionWorld(guiState);
if (onCreatePoint != null)
onCreatePoint(index, position);
guiState.nearestControl = m_PointControl.ID;
var data = m_PointControl.layoutData;
data.index = index + 1;
data.position = position;
data.distance = 0f;
m_PointControl.layoutData = data;
guiState.changed = true;
}
private Vector3 GetMousePositionWorld(IGUIState guiState)
{
if (guiToWorld != null)
return guiToWorld(guiState, guiState.mousePosition);
return guiState.GUIToWorld(guiState.mousePosition, hoveredControl.layoutData.forward, hoveredControl.layoutData.position);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ed2cb354a5d670f4881c41d7d2fc6a45
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,118 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.U2D.Path.GUIFramework;
namespace UnityEditor.U2D.Path
{
public class Drawer : IDrawer
{
internal class Styles
{
public readonly GUIStyle pointNormalStyle;
public readonly GUIStyle pointHoveredStyle;
public readonly GUIStyle pointSelectedStyle;
public readonly GUIStyle pointPreviewStyle;
public readonly GUIStyle tangentNormalStyle;
public readonly GUIStyle tangentHoveredStyle;
public Styles()
{
var pointNormal = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointNormal.png");
var pointHovered = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointHovered.png");
var pointSelected = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointSelected.png");
var pointPreview = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointPreview.png");
var tangentNormal = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/tangentNormal.png");
pointNormalStyle = CreateStyle(AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointNormal.png"), Vector2.one * 12f);
pointHoveredStyle = CreateStyle(AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointHovered.png"), Vector2.one * 12f);
pointSelectedStyle = CreateStyle(AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointSelected.png"), Vector2.one * 12f);
pointPreviewStyle = CreateStyle(AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointPreview.png"), Vector2.one * 12f);
tangentNormalStyle = CreateStyle(AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/tangentNormal.png"), Vector2.one * 8f);
tangentHoveredStyle = CreateStyle(AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/Path/pointHovered.png"), Vector2.one * 10f);
}
private GUIStyle CreateStyle(Texture2D texture, Vector2 size)
{
var guiStyle = new GUIStyle();
guiStyle.normal.background = texture;
guiStyle.fixedWidth = size.x;
guiStyle.fixedHeight = size.y;
return guiStyle;
}
}
private IGUIState m_GUIState = new GUIState();
private Styles m_Styles;
private Styles styles
{
get
{
if (m_Styles == null)
m_Styles = new Styles();
return m_Styles;
}
}
public void DrawCreatePointPreview(Vector3 position)
{
DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointPreviewStyle);
}
public void DrawPoint(Vector3 position)
{
DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointNormalStyle);
}
public void DrawPointHovered(Vector3 position)
{
DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointHoveredStyle);
}
public void DrawPointSelected(Vector3 position)
{
DrawGUIStyleCap(0, position, Quaternion.identity, m_GUIState.GetHandleSize(position), styles.pointSelectedStyle);
}
public void DrawLine(Vector3 p1, Vector3 p2, float width, Color color)
{
Handles.color = color;
Handles.DrawAAPolyLine(width, new Vector3[] { p1, p2 });
}
public void DrawBezier(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, float width, Color color)
{
Handles.color = color;
Handles.DrawBezier(p1, p4, p2, p3, color, null, width);
}
public void DrawTangent(Vector3 position, Vector3 tangent)
{
DrawLine(position, tangent, 3f, Color.yellow);
DrawGUIStyleCap(0, tangent, Quaternion.identity, m_GUIState.GetHandleSize(tangent), styles.tangentNormalStyle);
}
private void DrawGUIStyleCap(int controlID, Vector3 position, Quaternion rotation, float size, GUIStyle guiStyle)
{
if (Camera.current && Vector3.Dot(position - Camera.current.transform.position, Camera.current.transform.forward) < 0f)
return;
Handles.BeginGUI();
guiStyle.Draw(GetGUIStyleRect(guiStyle, position), GUIContent.none, controlID);
Handles.EndGUI();
}
private Rect GetGUIStyleRect(GUIStyle style, Vector3 position)
{
Vector2 vector = HandleUtility.WorldToGUIPoint(position);
float fixedWidth = style.fixedWidth;
float fixedHeight = style.fixedHeight;
return new Rect(vector.x - fixedWidth / 2f, vector.y - fixedHeight / 2f, fixedWidth, fixedHeight);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1b31573f9ab124c47b5e877a88088db1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 33e44f9d4edce8344a708573c37ebe4a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,52 @@
using System;
using UnityEngine;
namespace UnityEditor.U2D.Path.GUIFramework
{
public class ClickAction : HoveredControlAction
{
private int m_Button;
private bool m_UseEvent;
public int clickCount = 1;
public Action<IGUIState, Control> onClick;
private int m_ClickCounter = 0;
public ClickAction(Control control, int button, bool useEvent = true) : base(control)
{
m_Button = button;
m_UseEvent = useEvent;
}
protected override bool GetTriggerContidtion(IGUIState guiState)
{
if (guiState.mouseButton == m_Button && guiState.eventType == EventType.MouseDown)
{
if (guiState.clickCount == 1)
m_ClickCounter = 0;
++m_ClickCounter;
if (m_ClickCounter == clickCount)
return true;
}
return false;
}
protected override void OnTrigger(IGUIState guiState)
{
base.OnTrigger(guiState);
if (onClick != null)
onClick(guiState, hoveredControl);
if (m_UseEvent)
guiState.UseEvent();
}
protected override bool GetFinishContidtion(IGUIState guiState)
{
return true;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 175bf12750eca384fa2ece4cd8f67cd4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,46 @@
using System;
using UnityEngine;
namespace UnityEditor.U2D.Path.GUIFramework
{
public class CommandAction : GUIAction
{
private string m_CommandName;
public Action<IGUIState> onCommand;
public CommandAction(string commandName)
{
m_CommandName = commandName;
}
protected override bool GetTriggerContidtion(IGUIState guiState)
{
if (guiState.eventType == EventType.ValidateCommand && guiState.commandName == m_CommandName)
{
guiState.UseEvent();
return true;
}
return false;
}
protected override bool GetFinishContidtion(IGUIState guiState)
{
if (guiState.eventType == EventType.ExecuteCommand && guiState.commandName == m_CommandName)
{
guiState.UseEvent();
return true;
}
return false;
}
protected override void OnFinish(IGUIState guiState)
{
if (onCommand != null)
onCommand(guiState);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d8d8b17ba93dfad489ee2c2ff2546fb0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,152 @@
using System;
using UnityEngine;
namespace UnityEditor.U2D.Path.GUIFramework
{
public abstract class Control
{
private string m_Name;
private int m_NameHashCode;
private int m_ID;
private LayoutData m_LayoutData;
private int m_ActionID = -1;
private LayoutData m_HotLayoutData;
public string name
{
get { return m_Name; }
}
public int ID
{
get { return m_ID; }
}
public int actionID
{
get { return m_ActionID; }
}
public LayoutData layoutData
{
get { return m_LayoutData; }
set { m_LayoutData = value; }
}
public LayoutData hotLayoutData
{
get { return m_HotLayoutData; }
}
public Control(string name)
{
m_Name = name;
m_NameHashCode = name.GetHashCode();
}
public void GetControl(IGUIState guiState)
{
m_ID = guiState.GetControlID(m_NameHashCode, FocusType.Passive);
}
internal void SetActionID(int actionID)
{
m_ActionID = actionID;
m_HotLayoutData = m_LayoutData;
}
public void BeginLayout(IGUIState guiState)
{
Debug.Assert(guiState.eventType == EventType.Layout);
m_LayoutData = OnBeginLayout(LayoutData.zero, guiState);
}
public void Layout(IGUIState guiState)
{
Debug.Assert(guiState.eventType == EventType.Layout);
for (var i = 0; i < GetCount(); ++i)
{
if (guiState.hotControl == actionID && hotLayoutData.index == i)
continue;
var layoutData = new LayoutData()
{
index = i,
position = GetPosition(guiState, i),
distance = GetDistance(guiState, i),
forward = GetForward(guiState, i),
up = GetUp(guiState, i),
right = GetRight(guiState, i),
userData = GetUserData(guiState, i)
};
m_LayoutData = LayoutData.Nearest(m_LayoutData, layoutData);
}
}
public void EndLayout(IGUIState guiState)
{
Debug.Assert(guiState.eventType == EventType.Layout);
OnEndLayout(guiState);
}
public void Repaint(IGUIState guiState)
{
for (var i = 0; i < GetCount(); ++i)
OnRepaint(guiState, i);
}
protected virtual LayoutData OnBeginLayout(LayoutData data, IGUIState guiState)
{
return data;
}
protected virtual void OnEndLayout(IGUIState guiState)
{
}
protected virtual void OnRepaint(IGUIState guiState, int index)
{
}
protected virtual int GetCount()
{
return 1;
}
protected virtual Vector3 GetPosition(IGUIState guiState, int index)
{
return Vector3.zero;
}
protected virtual Vector3 GetForward(IGUIState guiState, int index)
{
return Vector3.forward;
}
protected virtual Vector3 GetUp(IGUIState guiState, int index)
{
return Vector3.up;
}
protected virtual Vector3 GetRight(IGUIState guiState, int index)
{
return Vector3.right;
}
protected virtual float GetDistance(IGUIState guiState, int index)
{
return layoutData.distance;
}
protected virtual object GetUserData(IGUIState guiState, int index)
{
return null;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1ab9c1eea0b4cc74bbee507825f10d5a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,20 @@
using System;
using UnityEngine;
namespace UnityEditor.U2D.Path.GUIFramework
{
public abstract class DefaultControl : Control
{
public static readonly float kPickDistance = 5f;
public DefaultControl(string name) : base(name)
{
}
protected override LayoutData OnBeginLayout(LayoutData data, IGUIState guiState)
{
data.distance = kPickDistance;
return data;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4dc9a9d2bfd656d4cb3ccb675fe0a426
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,112 @@
using System;
using UnityEngine;
namespace UnityEditor.U2D.Path.GUIFramework
{
public abstract class GUIAction
{
private int m_ID = -1;
public Func<IGUIState, GUIAction, bool> enable;
public Func<IGUIState, GUIAction, bool> enableRepaint;
public Func<IGUIState, GUIAction, bool> repaintOnMouseMove;
public Action<IGUIState, GUIAction> onPreRepaint;
public Action<IGUIState, GUIAction> onRepaint;
public int ID
{
get { return m_ID; }
}
public void OnGUI(IGUIState guiState)
{
m_ID = guiState.GetControlID(GetType().GetHashCode(), FocusType.Passive);
if (guiState.hotControl == 0 && IsEnabled(guiState) && CanTrigger(guiState) && GetTriggerContidtion(guiState))
{
guiState.hotControl = ID;
OnTrigger(guiState);
}
if (guiState.hotControl == ID)
{
if (GetFinishContidtion(guiState))
{
OnFinish(guiState);
guiState.hotControl = 0;
}
else
{
OnPerform(guiState);
}
}
if (guiState.eventType == EventType.Repaint && IsRepaintEnabled(guiState))
Repaint(guiState);
}
public bool IsEnabled(IGUIState guiState)
{
if (enable != null)
return enable(guiState, this);
return true;
}
public bool IsRepaintEnabled(IGUIState guiState)
{
if (!IsEnabled(guiState))
return false;
if (enableRepaint != null)
return enableRepaint(guiState, this);
return true;
}
public void PreRepaint(IGUIState guiState)
{
Debug.Assert(guiState.eventType == EventType.Repaint);
if (IsEnabled(guiState) && onPreRepaint != null)
onPreRepaint(guiState, this);
}
private void Repaint(IGUIState guiState)
{
Debug.Assert(guiState.eventType == EventType.Repaint);
if (onRepaint != null)
onRepaint(guiState, this);
}
internal bool IsRepaintOnMouseMoveEnabled(IGUIState guiState)
{
if (!IsEnabled(guiState) || !IsRepaintEnabled(guiState))
return false;
if (repaintOnMouseMove != null)
return repaintOnMouseMove(guiState, this);
return false;
}
protected abstract bool GetFinishContidtion(IGUIState guiState);
protected abstract bool GetTriggerContidtion(IGUIState guiState);
protected virtual bool CanTrigger(IGUIState guiState) { return true; }
protected virtual void OnTrigger(IGUIState guiState)
{
}
protected virtual void OnPerform(IGUIState guiState)
{
}
protected virtual void OnFinish(IGUIState guiState)
{
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2f11e220b90636b42bf596fb50ccdb6a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,156 @@
using UnityEngine;
using UnityEditor;
namespace UnityEditor.U2D.Path.GUIFramework
{
public class GUIState : IGUIState
{
private Handles.CapFunction nullCap = (int c, Vector3 p , Quaternion r, float s, EventType ev) => {};
public Vector2 mousePosition
{
get { return Event.current.mousePosition; }
}
public int mouseButton
{
get { return Event.current.button; }
}
public int clickCount
{
get { return Event.current.clickCount; }
set { Event.current.clickCount = Mathf.Max(0, value); }
}
public bool isShiftDown
{
get { return Event.current.shift; }
}
public bool isAltDown
{
get { return Event.current.alt; }
}
public bool isActionKeyDown
{
get { return EditorGUI.actionKey; }
}
public KeyCode keyCode
{
get { return Event.current.keyCode; }
}
public EventType eventType
{
get { return Event.current.type; }
}
public string commandName
{
get { return Event.current.commandName; }
}
public int nearestControl
{
get { return HandleUtility.nearestControl; }
set { HandleUtility.nearestControl = value; }
}
public int hotControl
{
get { return GUIUtility.hotControl; }
set { GUIUtility.hotControl = value; }
}
public bool changed
{
get { return GUI.changed; }
set { GUI.changed = value; }
}
public int GetControlID(int hint, FocusType focusType)
{
return GUIUtility.GetControlID(hint, focusType);
}
public void AddControl(int controlID, float distance)
{
HandleUtility.AddControl(controlID, distance);
}
public bool Slider(int id, SliderData sliderData, out Vector3 newPosition)
{
if (mouseButton == 0 && eventType == EventType.MouseDown)
{
hotControl = 0;
nearestControl = id;
}
EditorGUI.BeginChangeCheck();
newPosition = Handles.Slider2D(id, sliderData.position, sliderData.forward, sliderData.right, sliderData.up, 1f, nullCap, Vector2.zero);
return EditorGUI.EndChangeCheck();
}
public void UseEvent()
{
Event.current.Use();
}
public void Repaint()
{
HandleUtility.Repaint();
}
public bool HasCurrentCamera()
{
return Camera.current != null;
}
public float GetHandleSize(Vector3 position)
{
var scale = HasCurrentCamera() ? 0.01f : 0.05f;
return HandleUtility.GetHandleSize(position) * scale;
}
public float DistanceToSegment(Vector3 p1, Vector3 p2)
{
p1 = HandleUtility.WorldToGUIPoint(p1);
p2 = HandleUtility.WorldToGUIPoint(p2);
return HandleUtility.DistancePointToLineSegment(Event.current.mousePosition, p1, p2);
}
public float DistanceToCircle(Vector3 center, float radius)
{
return HandleUtility.DistanceToCircle(center, radius);
}
public Vector3 GUIToWorld(Vector2 guiPosition, Vector3 planeNormal, Vector3 planePos)
{
Vector3 worldPos = Handles.inverseMatrix.MultiplyPoint(guiPosition);
if (Camera.current)
{
Ray ray = HandleUtility.GUIPointToWorldRay(guiPosition);
planeNormal = Handles.matrix.MultiplyVector(planeNormal);
planePos = Handles.matrix.MultiplyPoint(planePos);
Plane plane = new Plane(planeNormal, planePos);
float distance = 0f;
if (plane.Raycast(ray, out distance))
{
worldPos = Handles.inverseMatrix.MultiplyPoint(ray.GetPoint(distance));
}
}
return worldPos;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a6ffe588dbbb4714695514536c29df95
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,124 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityEditor.U2D.Path.GUIFramework
{
public class GUISystem
{
private readonly int kControlIDCheckHashCode = "ControlIDCheckHashCode".GetHashCode();
private List<Control> m_Controls = new List<Control>();
private List<GUIAction> m_Actions = new List<GUIAction>();
private IGUIState m_GUIState;
private int m_PrevNearestControl = -1;
private LayoutData m_PrevNearestLayoutData = LayoutData.zero;
private int m_ControlIDCheck = -1;
public GUISystem(IGUIState guiState)
{
m_GUIState = guiState;
}
public void AddControl(Control control)
{
if (control == null)
throw new NullReferenceException("Control is null");
m_Controls.Add(control);
}
public void RemoveControl(Control control)
{
m_Controls.Remove(control);
}
public void AddAction(GUIAction action)
{
if (action == null)
throw new NullReferenceException("Action is null");
m_Actions.Add(action);
}
public void RemoveAction(GUIAction action)
{
m_Actions.Remove(action);
}
public void OnGUI()
{
var controlIDCheck = m_GUIState.GetControlID(kControlIDCheckHashCode, FocusType.Passive);
if (m_GUIState.eventType == EventType.Layout)
m_ControlIDCheck = controlIDCheck;
else if (m_GUIState.eventType != EventType.Used && m_ControlIDCheck != controlIDCheck)
Debug.LogWarning("GetControlID at event " + m_GUIState.eventType + " returns a controlID different from the one in Layout event");
var nearestLayoutData = LayoutData.zero;
foreach (var control in m_Controls)
control.GetControl(m_GUIState);
if (m_GUIState.eventType == EventType.Layout)
{
foreach (var control in m_Controls)
control.BeginLayout(m_GUIState);
foreach (var control in m_Controls)
{
control.Layout(m_GUIState);
nearestLayoutData = LayoutData.Nearest(nearestLayoutData, control.layoutData);
}
foreach (var control in m_Controls)
m_GUIState.AddControl(control.ID, control.layoutData.distance);
foreach (var control in m_Controls)
control.EndLayout(m_GUIState);
if (m_PrevNearestControl == m_GUIState.nearestControl)
{
if (nearestLayoutData.index != m_PrevNearestLayoutData.index)
m_GUIState.Repaint();
}
else
{
m_PrevNearestControl = m_GUIState.nearestControl;
m_GUIState.Repaint();
}
m_PrevNearestLayoutData = nearestLayoutData;
}
if (m_GUIState.eventType == EventType.Repaint)
{
foreach (var action in m_Actions)
if (action.IsRepaintEnabled(m_GUIState))
action.PreRepaint(m_GUIState);
foreach (var control in m_Controls)
control.Repaint(m_GUIState);
}
var repaintOnMouseMove = false;
foreach (var action in m_Actions)
{
if (IsMouseMoveEvent())
repaintOnMouseMove |= action.IsRepaintOnMouseMoveEnabled(m_GUIState);
action.OnGUI(m_GUIState);
}
if (repaintOnMouseMove)
m_GUIState.Repaint();
}
private bool IsMouseMoveEvent()
{
return m_GUIState.eventType == EventType.MouseMove || m_GUIState.eventType == EventType.MouseDrag;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: be957506ccdeb4a4789fd53667da3649
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,99 @@
using System;
using UnityEngine;
namespace UnityEditor.U2D.Path.GUIFramework
{
public class GenericControl : Control
{
public Func<IGUIState, LayoutData> onBeginLayout;
public Action<IGUIState> onEndLayout;
public Action<IGUIState, Control, int> onRepaint;
public Func<int> count;
public Func<int, Vector3> position;
public Func<IGUIState, int, float> distance;
public Func<int, Vector3> forward;
public Func<int, Vector3> up;
public Func<int, Vector3> right;
public Func<int, object> userData;
public GenericControl(string name) : base(name)
{
}
protected override int GetCount()
{
if (count != null)
return count();
return base.GetCount();
}
protected override void OnEndLayout(IGUIState guiState)
{
if (onEndLayout != null)
onEndLayout(guiState);
}
protected override void OnRepaint(IGUIState guiState, int index)
{
if (onRepaint != null)
onRepaint(guiState, this, index);
}
protected override LayoutData OnBeginLayout(LayoutData data, IGUIState guiState)
{
if (onBeginLayout != null)
return onBeginLayout(guiState);
return data;
}
protected override Vector3 GetPosition(IGUIState guiState, int index)
{
if (position != null)
return position(index);
return base.GetPosition(guiState,index);
}
protected override float GetDistance(IGUIState guiState, int index)
{
if (distance != null)
return distance(guiState, index);
return base.GetDistance(guiState, index);
}
protected override Vector3 GetForward(IGUIState guiState, int index)
{
if (forward != null)
return forward(index);
return base.GetForward(guiState,index);
}
protected override Vector3 GetUp(IGUIState guiState, int index)
{
if (up != null)
return up(index);
return base.GetUp(guiState,index);
}
protected override Vector3 GetRight(IGUIState guiState, int index)
{
if (right != null)
return right(index);
return base.GetRight(guiState,index);
}
protected override object GetUserData(IGUIState guiState, int index)
{
if (userData != null)
return userData(index);
return base.GetUserData(guiState,index);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 42c4e70b47496ce448cebce5780a4a58
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,58 @@
using System;
using UnityEngine;
namespace UnityEditor.U2D.Path.GUIFramework
{
public class GenericDefaultControl : DefaultControl
{
public Func<IGUIState, Vector3> position;
public Func<IGUIState, Vector3> forward;
public Func<IGUIState, Vector3> up;
public Func<IGUIState, Vector3> right;
public Func<IGUIState, object> userData;
public GenericDefaultControl(string name) : base(name)
{
}
protected override Vector3 GetPosition(IGUIState guiState, int index)
{
if (position != null)
return position(guiState);
return base.GetPosition(guiState, index);
}
protected override Vector3 GetForward(IGUIState guiState, int index)
{
if (forward != null)
return forward(guiState);
return base.GetForward(guiState, index);
}
protected override Vector3 GetUp(IGUIState guiState, int index)
{
if (up != null)
return up(guiState);
return base.GetUp(guiState, index);
}
protected override Vector3 GetRight(IGUIState guiState, int index)
{
if (right != null)
return right(guiState);
return base.GetRight(guiState, index);
}
protected override object GetUserData(IGUIState guiState, int index)
{
if (userData != null)
return userData(guiState);
return base.GetUserData(guiState, index);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 95695db3b2c152a4699bcb541136dc49
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,30 @@
using System;
using UnityEngine;
namespace UnityEditor.U2D.Path.GUIFramework
{
public abstract class HoveredControlAction : GUIAction
{
private Control m_HoveredControl;
public Control hoveredControl
{
get { return m_HoveredControl; }
}
public HoveredControlAction(Control control)
{
m_HoveredControl = control;
}
protected override bool CanTrigger(IGUIState guiState)
{
return guiState.nearestControl == hoveredControl.ID;
}
protected override void OnTrigger(IGUIState guiState)
{
m_HoveredControl.SetActionID(ID);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f0a8a8dd05c56b141ab6abfd7ec9055e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,41 @@
using UnityEngine;
using UnityEditor;
namespace UnityEditor.U2D.Path.GUIFramework
{
public struct SliderData
{
public Vector3 position;
public Vector3 forward;
public Vector3 up;
public Vector3 right;
public static readonly SliderData zero = new SliderData() { position = Vector3.zero, forward = Vector3.forward, up = Vector3.up, right = Vector3.right };
}
public interface IGUIState
{
Vector2 mousePosition { get; }
int mouseButton { get; }
int clickCount { get; set; }
bool isShiftDown { get; }
bool isAltDown { get; }
bool isActionKeyDown { get; }
KeyCode keyCode { get; }
EventType eventType { get; }
string commandName { get; }
int nearestControl { get; set; }
int hotControl { get; set; }
bool changed { get; set; }
int GetControlID(int hint, FocusType focusType);
void AddControl(int controlID, float distance);
bool Slider(int id, SliderData sliderData, out Vector3 newPosition);
void UseEvent();
void Repaint();
bool HasCurrentCamera();
float GetHandleSize(Vector3 position);
float DistanceToSegment(Vector3 p1, Vector3 p2);
float DistanceToCircle(Vector3 center, float radius);
Vector3 GUIToWorld(Vector2 guiPosition, Vector3 planeNormal, Vector3 planePos);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d72956f1b6eff1a459c9103dcae8bfef
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,25 @@
using UnityEngine;
namespace UnityEditor.U2D.Path.GUIFramework
{
public struct LayoutData
{
public int index;
public float distance;
public Vector3 position;
public Vector3 forward;
public Vector3 up;
public Vector3 right;
public object userData;
public static readonly LayoutData zero = new LayoutData() { index = 0, distance = float.MaxValue, position = Vector3.zero, forward = Vector3.forward, up = Vector3.up, right = Vector3.right };
public static LayoutData Nearest(LayoutData currentData, LayoutData newData)
{
if (newData.distance <= currentData.distance)
return newData;
return currentData;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6aa087efd8b80a948a0b4273603bc269
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,60 @@
using System;
using UnityEngine;
namespace UnityEditor.U2D.Path.GUIFramework
{
public class SliderAction : ClickAction
{
private SliderData m_SliderData;
public Action<IGUIState, Control, Vector3> onSliderBegin;
public Action<IGUIState, Control, Vector3> onSliderChanged;
public Action<IGUIState, Control, Vector3> onSliderEnd;
public SliderAction(Control control) : base(control, 0, false)
{
}
protected override bool GetFinishContidtion(IGUIState guiState)
{
return guiState.eventType == EventType.MouseUp && guiState.mouseButton == 0;
}
protected override void OnTrigger(IGUIState guiState)
{
base.OnTrigger(guiState);
m_SliderData.position = hoveredControl.hotLayoutData.position;
m_SliderData.forward = hoveredControl.hotLayoutData.forward;
m_SliderData.right = hoveredControl.hotLayoutData.right;
m_SliderData.up = hoveredControl.hotLayoutData.up;
if (onSliderBegin != null)
onSliderBegin(guiState, hoveredControl, m_SliderData.position);
}
protected override void OnFinish(IGUIState guiState)
{
if (onSliderEnd != null)
onSliderEnd(guiState, hoveredControl, m_SliderData.position);
guiState.UseEvent();
guiState.Repaint();
}
protected override void OnPerform(IGUIState guiState)
{
Vector3 newPosition;
var changed = guiState.Slider(ID, m_SliderData, out newPosition);
if (changed)
{
m_SliderData.position = newPosition;
if (onSliderChanged != null)
onSliderChanged(guiState, hoveredControl, newPosition);
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c6e0d9eacb9684e46b55a1fe312301e5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,15 @@
using UnityEngine;
namespace UnityEditor.U2D.Path
{
public interface IDrawer
{
void DrawCreatePointPreview(Vector3 position);
void DrawPoint(Vector3 position);
void DrawPointHovered(Vector3 position);
void DrawPointSelected(Vector3 position);
void DrawLine(Vector3 p1, Vector3 p2, float width, Color color);
void DrawBezier(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, float width, Color color);
void DrawTangent(Vector3 position, Vector3 tangent);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1a38d8ca070072b4f82fca33e7316cc0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,537 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.U2D.Path.GUIFramework;
namespace UnityEditor.U2D.Path
{
public class PathEditor
{
const float kSnappingDistance = 15f;
const string kDeleteCommandName = "Delete";
const string kSoftDeleteCommandName = "SoftDelete";
public IEditablePathController controller { get; set; }
public bool linearTangentIsZero { get; set; }
private IDrawer m_Drawer = new Drawer();
private IDrawer m_DrawerOverride;
private GUISystem m_GUISystem;
public IDrawer drawerOverride { get; set; }
private IDrawer drawer
{
get
{
if (drawerOverride != null)
return drawerOverride;
return m_Drawer;
}
}
public PathEditor() : this(new GUISystem(new GUIState())) { }
public PathEditor(GUISystem guiSystem)
{
m_GUISystem = guiSystem;
var m_PointControl = new GenericControl("Point")
{
count = GetPointCount,
distance = (guiState, i) =>
{
var position = GetPoint(i).position;
return guiState.DistanceToCircle(position, guiState.GetHandleSize(position) * 10f);
},
position = (i) => { return GetPoint(i).position; },
forward = (i) => { return GetForward(); },
up = (i) => { return GetUp(); },
right = (i) => { return GetRight(); },
onRepaint = DrawPoint
};
var m_EdgeControl = new GenericControl("Edge")
{
count = GetEdgeCount,
distance = DistanceToEdge,
position = (i) => { return GetPoint(i).position; },
forward = (i) => { return GetForward(); },
up = (i) => { return GetUp(); },
right = (i) => { return GetRight(); },
onRepaint = DrawEdge
};
m_EdgeControl.onEndLayout = (guiState) => { controller.AddClosestPath(m_EdgeControl.layoutData.distance); };
var m_LeftTangentControl = new GenericControl("LeftTangent")
{
count = () =>
{
if (GetShapeType() != ShapeType.Spline)
return 0;
return GetPointCount();
},
distance = (guiState, i) =>
{
if (linearTangentIsZero && GetPoint(i).tangentMode == TangentMode.Linear)
return float.MaxValue;
if (!IsSelected(i) || IsOpenEnded() && i == 0)
return float.MaxValue;
var position = GetLeftTangent(i);
return guiState.DistanceToCircle(position, guiState.GetHandleSize(position) * 10f);
},
position = (i) => { return GetLeftTangent(i); },
forward = (i) => { return GetForward(); },
up = (i) => { return GetUp(); },
right = (i) => { return GetRight(); },
onRepaint = (guiState, control, i) =>
{
if (!IsSelected(i) || IsOpenEnded() && i == 0)
return;
var point = GetPoint(i);
if (linearTangentIsZero && point.tangentMode == TangentMode.Linear)
return;
var position = point.position;
var leftTangent = GetLeftTangent(i);
drawer.DrawTangent(position, leftTangent);
}
};
var m_RightTangentControl = new GenericControl("RightTangent")
{
count = () =>
{
if (GetShapeType() != ShapeType.Spline)
return 0;
return GetPointCount();
},
distance = (guiState, i) =>
{
if (linearTangentIsZero && GetPoint(i).tangentMode == TangentMode.Linear)
return float.MaxValue;
if (!IsSelected(i) || IsOpenEnded() && i == GetPointCount()-1)
return float.MaxValue;
var position = GetRightTangent(i);
return guiState.DistanceToCircle(position, guiState.GetHandleSize(position) * 10f);
},
position = (i) => { return GetRightTangent(i); },
forward = (i) => { return GetForward(); },
up = (i) => { return GetUp(); },
right = (i) => { return GetRight(); },
onRepaint = (guiState, control, i) =>
{
if (!IsSelected(i) || IsOpenEnded() && i == GetPointCount()-1)
return;
var point = GetPoint(i);
if (linearTangentIsZero && point.tangentMode == TangentMode.Linear)
return;
var position = point.position;
var rightTangent = GetRightTangent(i);
drawer.DrawTangent(position, rightTangent);
}
};
var m_CreatePointAction = new CreatePointAction(m_PointControl, m_EdgeControl)
{
enable = (guiState, action) => !IsAltDown(guiState) && !guiState.isActionKeyDown && controller.closestEditablePath == controller.editablePath,
enableRepaint = (guiState, action) => EnableCreatePointRepaint(guiState, m_PointControl, m_LeftTangentControl, m_RightTangentControl),
repaintOnMouseMove = (guiState, action) => true,
guiToWorld = GUIToWorld,
onCreatePoint = (index, position) =>
{
controller.RegisterUndo("Create Point");
controller.CreatePoint(index, position);
},
onPreRepaint = (guiState, action) =>
{
if (GetPointCount() > 0)
{
var position = ClosestPointInEdge(guiState, guiState.mousePosition, m_EdgeControl.layoutData.index);
drawer.DrawCreatePointPreview(position);
}
}
};
Action<IGUIState> removePoints = (guiState) =>
{
controller.RegisterUndo("Remove Point");
controller.RemoveSelectedPoints();
guiState.changed = true;
};
var m_RemovePointAction1 = new CommandAction(kDeleteCommandName)
{
enable = (guiState, action) => { return GetSelectedPointCount() > 0; },
onCommand = removePoints
};
var m_RemovePointAction2 = new CommandAction(kSoftDeleteCommandName)
{
enable = (guiState, action) => { return GetSelectedPointCount() > 0; },
onCommand = removePoints
};
var dragged = false;
var m_MovePointAction = new SliderAction(m_PointControl)
{
enable = (guiState, action) => !IsAltDown(guiState),
onClick = (guiState, control) =>
{
dragged = false;
var index = control.layoutData.index;
if (!IsSelected(index))
{
controller.RegisterUndo("Selection");
if (!guiState.isActionKeyDown)
controller.ClearSelection();
controller.SelectPoint(index, true);
guiState.changed = true;
}
},
onSliderChanged = (guiState, control, position) =>
{
var index = control.hotLayoutData.index;
var delta = SnapIfNeeded(position) - GetPoint(index).position;
if (!dragged)
{
controller.RegisterUndo("Move Point");
dragged = true;
}
controller.MoveSelectedPoints(delta);
}
};
var m_MoveEdgeAction = new SliderAction(m_EdgeControl)
{
enable = (guiState, action) => !IsAltDown(guiState) && guiState.isActionKeyDown,
onSliderBegin = (guiState, control, position) =>
{
dragged = false;
},
onSliderChanged = (guiState, control, position) =>
{
var index = control.hotLayoutData.index;
var delta = position - GetPoint(index).position;
if (!dragged)
{
controller.RegisterUndo("Move Edge");
dragged = true;
}
controller.MoveEdge(index, delta);
}
};
var cachedRightTangent = Vector3.zero;
var cachedLeftTangent = Vector3.zero;
var cachedTangentMode = TangentMode.Linear;
var m_MoveLeftTangentAction = new SliderAction(m_LeftTangentControl)
{
enable = (guiState, action) => !IsAltDown(guiState),
onSliderBegin = (guiState, control, position) =>
{
dragged = false;
var point = GetPoint(control.hotLayoutData.index);
cachedRightTangent = point.rightTangent;
cachedTangentMode = point.tangentMode;
},
onSliderChanged = (guiState, control, position) =>
{
var index = control.hotLayoutData.index;
var setToLinear = m_PointControl.distance(guiState, index) <= DefaultControl.kPickDistance;
if (!dragged)
{
controller.RegisterUndo("Move Tangent");
dragged = true;
}
position = SnapIfNeeded(position);
controller.SetLeftTangent(index, position, setToLinear, guiState.isShiftDown, cachedRightTangent, cachedTangentMode);
}
};
var m_MoveRightTangentAction = new SliderAction(m_RightTangentControl)
{
enable = (guiState, action) => !IsAltDown(guiState),
onSliderBegin = (guiState, control, position) =>
{
dragged = false;
var point = GetPoint(control.hotLayoutData.index);
cachedLeftTangent = point.leftTangent;
cachedTangentMode = point.tangentMode;
},
onSliderChanged = (guiState, control, position) =>
{
var index = control.hotLayoutData.index;
var setToLinear = m_PointControl.distance(guiState, index) <= DefaultControl.kPickDistance;
if (!dragged)
{
controller.RegisterUndo("Move Tangent");
dragged = true;
}
position = SnapIfNeeded(position);
controller.SetRightTangent(index, position, setToLinear, guiState.isShiftDown, cachedLeftTangent, cachedTangentMode);
}
};
m_GUISystem.AddControl(m_EdgeControl);
m_GUISystem.AddControl(m_PointControl);
m_GUISystem.AddControl(m_LeftTangentControl);
m_GUISystem.AddControl(m_RightTangentControl);
m_GUISystem.AddAction(m_CreatePointAction);
m_GUISystem.AddAction(m_RemovePointAction1);
m_GUISystem.AddAction(m_RemovePointAction2);
m_GUISystem.AddAction(m_MovePointAction);
m_GUISystem.AddAction(m_MoveEdgeAction);
m_GUISystem.AddAction(m_MoveLeftTangentAction);
m_GUISystem.AddAction(m_MoveRightTangentAction);
}
public void OnGUI()
{
m_GUISystem.OnGUI();
}
private bool IsAltDown(IGUIState guiState)
{
return guiState.hotControl == 0 && guiState.isAltDown;
}
private ControlPoint GetPoint(int index)
{
return controller.editablePath.GetPoint(index);
}
private int GetPointCount()
{
return controller.editablePath.pointCount;
}
private int GetEdgeCount()
{
if (controller.editablePath.isOpenEnded)
return controller.editablePath.pointCount - 1;
return controller.editablePath.pointCount;
}
private int GetSelectedPointCount()
{
return controller.editablePath.selection.Count;
}
private bool IsSelected(int index)
{
return controller.editablePath.selection.Contains(index);
}
private Vector3 GetForward()
{
return controller.editablePath.forward;
}
private Vector3 GetUp()
{
return controller.editablePath.up;
}
private Vector3 GetRight()
{
return controller.editablePath.right;
}
private Matrix4x4 GetLocalToWorldMatrix()
{
return controller.editablePath.localToWorldMatrix;
}
private ShapeType GetShapeType()
{
return controller.editablePath.shapeType;
}
private bool IsOpenEnded()
{
return controller.editablePath.isOpenEnded;
}
private Vector3 GetLeftTangent(int index)
{
if (linearTangentIsZero)
return GetPoint(index).leftTangent;
return controller.editablePath.CalculateLeftTangent(index);
}
private Vector3 GetRightTangent(int index)
{
if (linearTangentIsZero)
return GetPoint(index).rightTangent;
return controller.editablePath.CalculateRightTangent(index);
}
private int NextIndex(int index)
{
return EditablePathUtility.Mod(index + 1, GetPointCount());
}
private ControlPoint NextControlPoint(int index)
{
return GetPoint(NextIndex(index));
}
private int PrevIndex(int index)
{
return EditablePathUtility.Mod(index - 1, GetPointCount());
}
private ControlPoint PrevControlPoint(int index)
{
return GetPoint(PrevIndex(index));
}
private Vector3 ClosestPointInEdge(IGUIState guiState, Vector2 mousePosition, int index)
{
if (GetShapeType() == ShapeType.Polygon)
{
var p0 = GetPoint(index).position;
var p1 = NextControlPoint(index).position;
var mouseWorldPosition = GUIToWorld(guiState, mousePosition);
var dir1 = (mouseWorldPosition - p0);
var dir2 = (p1 - p0);
return Mathf.Clamp01(Vector3.Dot(dir1, dir2.normalized) / dir2.magnitude) * dir2 + p0;
}
else if (GetShapeType() == ShapeType.Spline)
{
var nextIndex = NextIndex(index);
float t;
return BezierUtility.ClosestPointOnCurve(
GUIToWorld(guiState, mousePosition),
GetPoint(index).position,
GetPoint(nextIndex).position,
GetRightTangent(index),
GetLeftTangent(nextIndex),
out t);
}
return Vector3.zero;
}
private float DistanceToEdge(IGUIState guiState, int index)
{
if (GetShapeType() == ShapeType.Polygon)
{
return guiState.DistanceToSegment(GetPoint(index).position, NextControlPoint(index).position);
}
else if (GetShapeType() == ShapeType.Spline)
{
var closestPoint = ClosestPointInEdge(guiState, guiState.mousePosition, index);
var closestPoint2 = HandleUtility.WorldToGUIPoint(closestPoint);
return (closestPoint2 - guiState.mousePosition).magnitude;
}
return float.MaxValue;
}
private Vector3 GUIToWorld(IGUIState guiState, Vector2 position)
{
return guiState.GUIToWorld(position, GetForward(), GetLocalToWorldMatrix().MultiplyPoint3x4(Vector3.zero));
}
private void DrawPoint(IGUIState guiState, Control control, int index)
{
var position = GetPoint(index).position;
if (guiState.hotControl == control.actionID && control.hotLayoutData.index == index || IsSelected(index))
drawer.DrawPointSelected(position);
else if (guiState.hotControl == 0 && guiState.nearestControl == control.ID && !IsAltDown(guiState) && control.layoutData.index == index)
drawer.DrawPointHovered(position);
else
drawer.DrawPoint(position);
}
private void DrawEdge(IGUIState guiState, Control control, int index)
{
if (GetShapeType() == ShapeType.Polygon)
{
var nextIndex = NextIndex(index);
var color = Color.white;
if(guiState.nearestControl == control.ID && control.layoutData.index == index && guiState.hotControl == 0 && !IsAltDown(guiState))
color = Color.yellow;
drawer.DrawLine(GetPoint(index).position, GetPoint(nextIndex).position, 5f, color);
}
else if (GetShapeType() == ShapeType.Spline)
{
var nextIndex = NextIndex(index);
var color = Color.white;
if(guiState.nearestControl == control.ID && control.layoutData.index == index && guiState.hotControl == 0 && !IsAltDown(guiState))
color = Color.yellow;
drawer.DrawBezier(
GetPoint(index).position,
GetRightTangent(index),
GetLeftTangent(nextIndex),
GetPoint(nextIndex).position,
5f,
color);
}
}
private bool EnableCreatePointRepaint(IGUIState guiState, Control pointControl, Control leftTangentControl, Control rightTangentControl)
{
return guiState.nearestControl != pointControl.ID &&
guiState.hotControl == 0 &&
(guiState.nearestControl != leftTangentControl.ID) &&
(guiState.nearestControl != rightTangentControl.ID);
}
private Vector3 SnapIfNeeded(Vector3 position)
{
if (!controller.enableSnapping || controller.snapping == null)
return position;
var guiPosition = HandleUtility.WorldToGUIPoint(position);
var snappedGuiPosition = HandleUtility.WorldToGUIPoint(controller.snapping.Snap(position));
var sqrDistance = (guiPosition - snappedGuiPosition).sqrMagnitude;
if (sqrDistance < kSnappingDistance * kSnappingDistance)
position = controller.snapping.Snap(position);
return position;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 20eb80169ce2c0944aca58413fc34b1a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.U2D.Path.GUIFramework;
namespace UnityEditor.U2D.Path
{
public abstract class RectSelector<T> : ISelector<T>
{
public class Styles
{
public readonly GUIStyle selectionRectStyle;
public Styles()
{
selectionRectStyle = GUI.skin.FindStyle("selectionRect");
}
}
public Action<ISelector<T>, bool> onSelectionBegin;
public Action<ISelector<T>> onSelectionChanged;
public Action<ISelector<T>> onSelectionEnd;
private GUISystem m_GUISystem;
private Control m_RectSelectorControl;
private GUIAction m_RectSelectAction;
private Rect m_GUIRect;
private Styles m_Styles;
public Rect guiRect
{
get { return m_GUIRect; }
}
private Styles styles
{
get
{
if (m_Styles == null)
m_Styles = new Styles();
return m_Styles;
}
}
public RectSelector() : this(new GUISystem(new GUIState())) { }
public RectSelector(GUISystem guiSystem)
{
m_GUISystem = guiSystem;
m_RectSelectorControl = new GenericDefaultControl("RectSelector");
var start = Vector2.zero;
var rectEnd = Vector2.zero;
m_RectSelectAction = new SliderAction(m_RectSelectorControl)
{
enable = (guiState, action) => !IsAltDown(guiState),
enableRepaint = (guiState, action) =>
{
var size = start - rectEnd;
return size != Vector2.zero && guiState.hotControl == action.ID;
},
onSliderBegin = (guiState, control, position) =>
{
start = guiState.mousePosition;
rectEnd = guiState.mousePosition;
m_GUIRect = FromToRect(start, rectEnd);
if (onSelectionBegin != null)
onSelectionBegin(this, guiState.isShiftDown);
},
onSliderChanged = (guiState, control, position) =>
{
rectEnd = guiState.mousePosition;
m_GUIRect = FromToRect(start, rectEnd);
if (onSelectionChanged != null)
onSelectionChanged(this);
},
onSliderEnd = (guiState, control, position) =>
{
if (onSelectionEnd != null)
onSelectionEnd(this);
},
onRepaint = (guiState, action) =>
{
Handles.BeginGUI();
styles.selectionRectStyle.Draw(m_GUIRect, GUIContent.none, false, false, false, false);
Handles.EndGUI();
}
};
m_GUISystem.AddControl(m_RectSelectorControl);
m_GUISystem.AddAction(m_RectSelectAction);
}
private bool IsAltDown(IGUIState guiState)
{
return guiState.hotControl == 0 && guiState.isAltDown;
}
private Rect FromToRect(Vector2 start, Vector2 end)
{
Rect r = new Rect(start.x, start.y, end.x - start.x, end.y - start.y);
if (r.width < 0)
{
r.x += r.width;
r.width = -r.width;
}
if (r.height < 0)
{
r.y += r.height;
r.height = -r.height;
}
return r;
}
public void OnGUI()
{
m_GUISystem.OnGUI();
}
bool ISelector<T>.Select(T element)
{
return Select(element);
}
protected abstract bool Select(T element);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 24a6adab990cd9541b5ad6343eb12b32
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: