Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
193 changes: 173 additions & 20 deletions ...ets/PathCreator/Core/Editor/PathEditor.cs → Editor/PathEditor.cs
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,98 @@ public class PathEditor : Editor {
ArcHandle anchorAngleHandle = new ArcHandle ();
VertexPath normalsVertexPath;

// State variables:
int selectedSegmentIndex;
int draggingHandleIndex;
int mouseOverHandleIndex;
int handleIndexToDisplayAsTransform;
// Shared editor state between OnSceneGUI and OnInspectorGUI as they work with different editor instances
private class EditorState
{
public int selectedSegmentIndex;
public int draggingHandleIndex;
public int mouseOverHandleIndex;
public int handleIndexToDisplayAsTransform;

public bool shiftLastFrame;
public bool hasUpdatedScreenSpaceLine;
public bool hasUpdatedNormalsVertexPath;
public bool editingNormalsOld;

public Vector3 transformPos;
public Vector3 transformScale;
public Quaternion transformRot;

public Color handlesStartCol;
}

static Dictionary<PathCreator, EditorState> editorStateMap = new Dictionary<PathCreator, EditorState>();

bool shiftLastFrame;
bool hasUpdatedScreenSpaceLine;
bool hasUpdatedNormalsVertexPath;
bool editingNormalsOld;
EditorState editorState;

Vector3 transformPos;
Vector3 transformScale;
Quaternion transformRot;
// State variables accessors:
int selectedSegmentIndex
{
get { return editorState.selectedSegmentIndex; }
set { editorState.selectedSegmentIndex = value; }
}

Color handlesStartCol;
int draggingHandleIndex
{
get { return editorState.draggingHandleIndex; }
set { editorState.draggingHandleIndex = value; }
}
int mouseOverHandleIndex
{
get { return editorState.mouseOverHandleIndex; }
set { editorState.mouseOverHandleIndex = value; }
}
int handleIndexToDisplayAsTransform
{
get { return editorState.handleIndexToDisplayAsTransform; }
set { editorState.handleIndexToDisplayAsTransform = value; }
}

bool shiftLastFrame
{
get { return editorState.shiftLastFrame; }
set { editorState.shiftLastFrame = value; }
}
bool hasUpdatedScreenSpaceLine
{
get { return editorState.hasUpdatedScreenSpaceLine; }
set { editorState.hasUpdatedScreenSpaceLine = value; }
}
bool hasUpdatedNormalsVertexPath
{
get { return editorState.hasUpdatedNormalsVertexPath; }
set { editorState.hasUpdatedNormalsVertexPath = value; }
}
bool editingNormalsOld
{
get { return editorState.editingNormalsOld; }
set { editorState.editingNormalsOld = value; }
}

Vector3 transformPos
{
get { return editorState.transformPos; }
set { editorState.transformPos = value; }
}
Vector3 transformScale
{
get { return editorState.transformScale; }
set { editorState.transformScale = value; }
}
Quaternion transformRot
{
get { return editorState.transformRot; }
set { editorState.transformRot = value; }
}

Color handlesStartCol
{
get { return editorState.handlesStartCol; }
set { editorState.handlesStartCol = value; }
}

private Quaternion handleRotation = Quaternion.identity;
private bool isSceneViewMouseDown;

// Constants
const int bezierPathTab = 0;
Expand Down Expand Up @@ -278,7 +354,22 @@ void OnSceneGUI () {

EventType eventType = Event.current.type;

using (var check = new EditorGUI.ChangeCheckScope ()) {
switch (eventType)
{
case EventType.MouseDown:
isSceneViewMouseDown = true;
break;
case EventType.MouseUp:
isSceneViewMouseDown = false;
break;
case EventType.ExecuteCommand:
if (Event.current.commandName == "FrameSelected")
FrameSelected();
break;
}

using (var check = new EditorGUI.ChangeCheckScope())
{
handlesStartCol = Handles.color;
switch (data.tabIndex) {
case bezierPathTab:
Expand Down Expand Up @@ -308,9 +399,31 @@ void OnSceneGUI () {
SetTransformState ();
}

private void FrameSelected()
{
// keep original event from doing its thing
Event.current.commandName = "";
Event.current.Use();

var sceneView = SceneView.lastActiveSceneView;
if (handleIndexToDisplayAsTransform > -1)
{
var pos = creator.bezierPath[handleIndexToDisplayAsTransform];
pos = creator.transform.localToWorldMatrix.MultiplyPoint(pos);
sceneView.LookAt(pos, sceneView.camera.transform.rotation, creator.EditorData.bezierHandleScale);
}
else
{
var bounds = creator.bezierPath.PathBounds;
bounds.min = creator.transform.localToWorldMatrix.MultiplyPoint(bounds.min);
bounds.max = creator.transform.localToWorldMatrix.MultiplyPoint(bounds.max);
sceneView.Frame(bounds);
}
}

void DrawVertexPathSceneEditor () {

Color bezierCol = globalDisplaySettings.bezierPath;
Color bezierCol = globalDisplaySettings.selectedBezierPath;
bezierCol.a *= .5f;

if (data.showBezierPathInVertexMode) {
Expand Down Expand Up @@ -463,7 +576,7 @@ void DrawBezierPathSceneEditor () {

// Draw path
bool highlightSegment = (i == selectedSegmentIndex && Event.current.shift && draggingHandleIndex == -1 && mouseOverHandleIndex == -1);
Color segmentCol = (highlightSegment) ? globalDisplaySettings.highlightedPath : globalDisplaySettings.bezierPath;
Color segmentCol = (highlightSegment) ? globalDisplaySettings.highlightedPath : globalDisplaySettings.selectedBezierPath;
Handles.DrawBezier (points[0], points[3], points[1], points[2], segmentCol, null, 2);
}

Expand Down Expand Up @@ -553,8 +666,15 @@ void DrawHandle (int i) {
}
}

} else {
handlePosition = Handles.DoPositionHandle (handlePosition, Quaternion.identity);
}
else
{
HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));

if(isSceneViewMouseDown == false)
UpdateHandleRotation(i);

handlePosition = Handles.DoPositionHandle(handlePosition, handleRotation);
}

}
Expand All @@ -579,6 +699,7 @@ void DrawHandle (int i) {
handleIndexToDisplayAsTransform = -1; // disable move tool if clicking on point under move tool
} else {
handleIndexToDisplayAsTransform = i;
UpdateHandleRotation(i);
}
}
Repaint ();
Expand All @@ -601,16 +722,48 @@ void DrawHandle (int i) {

}

private void UpdateHandleRotation(int i)
{
var rot = Quaternion.identity;
if (Tools.pivotRotation == PivotRotation.Local)
{
var i3 = i % 3;

var t = creator.path.GetClosestTimeOnPath(bezierPath.GetPoint(i));
rot = creator.path.GetRotation(t, bezierPath.IsClosed ? EndOfPathInstruction.Loop : EndOfPathInstruction.Stop);
if (i3 != 0)
{
var anchorIndex = 0;
if (i3 == 1)
anchorIndex = i - 1;
else if (i3 == 2)
anchorIndex = i + 1;

rot = Quaternion.LookRotation(bezierPath.GetPoint(i) - bezierPath.GetPoint(anchorIndex), rot * Vector3.up);
}
}
handleRotation = rot;
}

#endregion

#region Internal methods

void OnDisable () {
Tools.hidden = false;

if(editorStateMap.ContainsKey(creator))
editorStateMap.Remove(creator);
}

void OnEnable () {
creator = (PathCreator) target;
void OnEnable()
{
creator = (PathCreator)target;

// Ensure we have an EditorState object for this creator instance
if (editorStateMap.TryGetValue(creator, out editorState) == false)
editorState = editorStateMap[creator] = new EditorState();

bool in2DEditorMode = EditorSettings.defaultBehaviorMode == EditorBehaviorMode.Mode2D;
creator.InitializeEditorData (in2DEditorMode);

Expand Down
File renamed without changes.
Loading