This commit is contained in:
2021-06-13 10:28:03 +02:00
parent eb70603c85
commit df2d24cbd3
7487 changed files with 943244 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
namespace UnityEditor.Experimental.Rendering.Universal.Path2D
{
internal interface ISelectable<T>
{
bool Select(ISelector<T> selector);
}
}

View File

@@ -0,0 +1,16 @@
using System.Collections.Generic;
namespace UnityEditor.Experimental.Rendering.Universal.Path2D
{
internal interface ISelection<T>
{
int Count { get; }
T activeElement { get; set; }
T[] elements { get; set; }
void Clear();
void BeginSelection();
void EndSelection(bool select);
bool Select(T element, bool select);
bool Contains(T element);
}
}

View File

@@ -0,0 +1,7 @@
namespace UnityEditor.Experimental.Rendering.Universal.Path2D
{
internal interface ISelector<T>
{
bool Select(T element);
}
}

View File

@@ -0,0 +1,13 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace UnityEditor.Experimental.Rendering.Universal.Path2D
{
[Serializable]
internal class IndexedSelection : SerializableSelection<int>
{
protected override int GetInvalidElement() { return -1; }
}
}

View File

@@ -0,0 +1,13 @@
using UnityEngine;
using UnityEditor;
namespace UnityEditor.Experimental.Rendering.Universal.Path2D
{
internal class PointRectSelector : RectSelector<Vector3>
{
protected override bool Select(Vector3 element)
{
return guiRect.Contains(HandleUtility.WorldToGUIPoint(element), true);
}
}
}

View File

@@ -0,0 +1,150 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Experimental.Rendering.Universal.Path2D.GUIFramework;
namespace UnityEditor.Experimental.Rendering.Universal.Path2D
{
internal abstract class RectSelector<T> : ISelector<T>
{
public Action<ISelector<T>, bool> onSelectionBegin;
public Action<ISelector<T>> onSelectionChanged;
public Action<ISelector<T>> onSelectionEnd = null;
private GUISystem m_GUISystem;
private Control m_RectSelectorControl;
private GUIAction m_RectSelectAction;
private Vector3 m_RectStart;
private Vector3 m_RectEnd;
private Rect m_GUIRect;
private IDrawer m_Drawer = new Drawer();
public Rect guiRect
{
get { return m_GUIRect; }
}
public RectSelector() : this(new GUISystem(new GUIState())) {}
public RectSelector(GUISystem guiSystem)
{
m_GUISystem = guiSystem;
m_RectSelectorControl = new GenericDefaultControl("RectSelector")
{
position = (guiState) =>
{
return GUIToWorld(guiState, guiState.mousePosition);
},
forward = (guiState) =>
{
if (Camera.current)
return Camera.current.transform.forward;
return Vector3.forward;
},
right = (guiState) =>
{
if (Camera.current)
return Camera.current.transform.right;
return Vector3.right;
},
up = (guiState) =>
{
if (Camera.current)
return Camera.current.transform.up;
return Vector3.up;
}
};
m_RectSelectAction = new SliderAction(m_RectSelectorControl)
{
enableRepaint = (guiState, action) =>
{
var size = m_RectStart - m_RectEnd;
return size != Vector3.zero && guiState.hotControl == action.ID;
},
onClick = (guiState, control) =>
{
m_RectStart = GUIToWorld(guiState, guiState.mousePosition);
m_RectEnd = m_RectStart;
m_GUIRect = CalculateGUIRect();
},
onSliderBegin = (guiState, control, position) =>
{
m_RectEnd = position;
m_GUIRect = CalculateGUIRect();
if (onSelectionBegin != null)
onSelectionBegin(this, guiState.isShiftDown);
},
onSliderChanged = (guiState, control, position) =>
{
m_RectEnd = position;
m_GUIRect = CalculateGUIRect();
if (onSelectionChanged != null)
onSelectionChanged(this);
},
onSliderEnd = (guiState, control, position) =>
{
if (onSelectionEnd != null)
onSelectionEnd(this);
},
onRepaint = (guiState, action) =>
{
m_Drawer.DrawSelectionRect(m_GUIRect);
}
};
m_GUISystem.AddControl(m_RectSelectorControl);
m_GUISystem.AddAction(m_RectSelectAction);
}
private Vector3 GUIToWorld(IGUIState guiState, Vector2 guiPosition)
{
var forward = Vector3.forward;
if (guiState.HasCurrentCamera())
forward = Camera.current.transform.forward;
return guiState.GUIToWorld(guiPosition, forward, Vector3.zero);
}
private Rect CalculateGUIRect()
{
return FromToRect(HandleUtility.WorldToGUIPoint(m_RectStart), HandleUtility.WorldToGUIPoint(m_RectEnd));
}
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,143 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace UnityEditor.Experimental.Rendering.Universal.Path2D
{
[Serializable]
internal abstract class SerializableSelection<T> : ISelection<T>, ISerializationCallbackReceiver
{
internal readonly static int kInvalidID = -1;
[SerializeField]
private T[] m_Keys = new T[0];
private HashSet<T> m_Selection = new HashSet<T>();
private HashSet<T> m_TemporalSelection = new HashSet<T>();
private bool m_SelectionInProgress = false;
public int Count
{
get { return m_Selection.Count + m_TemporalSelection.Count; }
}
public T activeElement
{
get { return First(); }
set
{
Clear();
Select(value, true);
}
}
public T[] elements
{
get
{
var set = m_Selection;
if (m_SelectionInProgress)
{
var union = new HashSet<T>(m_Selection);
union.UnionWith(m_TemporalSelection);
set = union;
}
return new List<T>(set).ToArray();
}
set
{
Clear();
foreach (var element in value)
Select(element, true);
}
}
protected abstract T GetInvalidElement();
public void Clear()
{
GetSelection().Clear();
}
public void BeginSelection()
{
m_SelectionInProgress = true;
Clear();
}
public void EndSelection(bool select)
{
m_SelectionInProgress = false;
if (select)
m_Selection.UnionWith(m_TemporalSelection);
else
m_Selection.ExceptWith(m_TemporalSelection);
m_TemporalSelection.Clear();
}
public bool Select(T element, bool select)
{
var changed = false;
if (EqualityComparer<T>.Default.Equals(element, GetInvalidElement()))
return changed;
if (select)
changed = GetSelection().Add(element);
else if (Contains(element))
changed = GetSelection().Remove(element);
return changed;
}
public bool Contains(T element)
{
return m_Selection.Contains(element) || m_TemporalSelection.Contains(element);
}
private HashSet<T> GetSelection()
{
if (m_SelectionInProgress)
return m_TemporalSelection;
return m_Selection;
}
private T First()
{
T element = First(m_Selection);
if (EqualityComparer<T>.Default.Equals(element, GetInvalidElement()))
element = First(m_TemporalSelection);
return element;
}
private T First(HashSet<T> set)
{
if (set.Count == 0)
return GetInvalidElement();
using (var enumerator = set.GetEnumerator())
{
Debug.Assert(enumerator.MoveNext());
return enumerator.Current;
}
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
m_Keys = new List<T>(m_Selection).ToArray();
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
elements = m_Keys;
}
}
}