Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SebLague committed Jan 25, 2019
0 parents commit ef4ee37
Show file tree
Hide file tree
Showing 77 changed files with 6,860 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Sebastian Lague

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 10 additions & 0 deletions PathCreator/Core.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions PathCreator/Core/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions PathCreator/Core/Editor/Helper.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions PathCreator/Core/Editor/Helper/MouseUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using UnityEngine;
using UnityEditor;
using PathCreation;

namespace PathCreationEditor
{
public static class MouseUtility
{
/// <summary>
/// Determines mouse position in world. If PathSpace is xy/xz, the position will be locked to that plane.
/// If PathSpace is xyz, then depthFor3DSpace will be used as distance from scene camera.
/// </summary>
public static Vector3 GetMouseWorldPosition(PathSpace space, float depthFor3DSpace = 10)
{
Ray mouseRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
Vector3 worldMouse = mouseRay.GetPoint(depthFor3DSpace);

// Mouse can only move on XY plane
if (space == PathSpace.xy)
{
float zDir = mouseRay.direction.z;
if (zDir != 0)
{
float dstToXYPlane = Mathf.Abs(mouseRay.origin.z / zDir);
worldMouse = mouseRay.GetPoint(dstToXYPlane);
}
}
// Mouse can only move on XZ plane
else if (space == PathSpace.xz)
{
float yDir = mouseRay.direction.y;
if (yDir != 0)
{
float dstToXZPlane = Mathf.Abs(mouseRay.origin.y / yDir);
worldMouse = mouseRay.GetPoint(dstToXZPlane);
}
}

return worldMouse;
}

}
}
11 changes: 11 additions & 0 deletions PathCreator/Core/Editor/Helper/MouseUtility.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

218 changes: 218 additions & 0 deletions PathCreator/Core/Editor/Helper/PathHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using PathCreation;

namespace PathCreationEditor
{
public static class PathHandle
{

public const float extraInputRadius = .005f;

static Vector2 handleDragMouseStart;
static Vector2 handleDragMouseEnd;
static Vector3 handleDragWorldStart;

static int selectedHandleID;
static bool mouseIsOverAHandle;

public enum HandleInputType
{
None,
LMBPress,
LMBClick,
LMBDrag,
LMBRelease,
};

static readonly int hint;
static float dstMouseToDragPointStart;

static List<int> ids;

static PathHandle()
{
hint = 109264;
ids = new List<int>();

dstMouseToDragPointStart = float.MaxValue;
}

public static Vector3 DrawHandle(Vector3 position, PathSpace space, bool isInteractive, float handleDiameter, Handles.CapFunction capFunc, HandleColours colours, out HandleInputType inputType, int handleIndex)
{
int id = GetID(handleIndex);
Vector3 screenPosition = Handles.matrix.MultiplyPoint(position);
Matrix4x4 cachedMatrix = Handles.matrix;

inputType = HandleInputType.None;

EventType eventType = Event.current.GetTypeForControl(id);
float handleRadius = handleDiameter / 2f;
float dstToHandle = HandleUtility.DistanceToCircle(position, handleRadius + extraInputRadius);
float dstToMouse = HandleUtility.DistanceToCircle(position, 0);

// Handle input events
if (isInteractive)
{
// Repaint if mouse is entering/exiting handle (for highlight colour)
if (dstToHandle == 0)
{
if (!mouseIsOverAHandle)
{
HandleUtility.Repaint();
mouseIsOverAHandle = true;
}
}
else
{
if (mouseIsOverAHandle)
{
HandleUtility.Repaint();
mouseIsOverAHandle = false;
}
}
switch (eventType)
{
case EventType.MouseDown:
if (Event.current.button == 0 && Event.current.modifiers != EventModifiers.Alt)
{
if (dstToHandle == 0 && dstToMouse < dstMouseToDragPointStart)
{
dstMouseToDragPointStart = dstToMouse;
GUIUtility.hotControl = id;
handleDragMouseEnd = handleDragMouseStart = Event.current.mousePosition;
handleDragWorldStart = position;
selectedHandleID = id;
inputType = HandleInputType.LMBPress;
}
}
break;

case EventType.MouseUp:
dstMouseToDragPointStart = float.MaxValue;
if (GUIUtility.hotControl == id && Event.current.button == 0)
{
GUIUtility.hotControl = 0;
selectedHandleID = -1;
Event.current.Use();

inputType = HandleInputType.LMBRelease;


if (Event.current.mousePosition == handleDragMouseStart)
{
inputType = HandleInputType.LMBClick;
}
}
break;

case EventType.MouseDrag:
if (GUIUtility.hotControl == id && Event.current.button == 0)
{
handleDragMouseEnd += new Vector2(Event.current.delta.x, -Event.current.delta.y);
Vector3 position2 = Camera.current.WorldToScreenPoint(Handles.matrix.MultiplyPoint(handleDragWorldStart))
+ (Vector3)(handleDragMouseEnd - handleDragMouseStart);
inputType = HandleInputType.LMBDrag;
// Handle can move freely in 3d space
if (space == PathSpace.xyz)
{
position = Handles.matrix.inverse.MultiplyPoint(Camera.current.ScreenToWorldPoint(position2));
}
// Handle is clamped to xy or xz plane
else
{
position = MouseUtility.GetMouseWorldPosition(space);
}

GUI.changed = true;
Event.current.Use();
}
break;
}
}

switch (eventType)
{
case EventType.Repaint:
Color originalColour = Handles.color;
Handles.color = (isInteractive) ? colours.defaultColour : colours.disabledColour;

if (id == GUIUtility.hotControl)
{
Handles.color = colours.selectedColour;
}
else if (dstToHandle == 0 && selectedHandleID == -1 && isInteractive)
{
Handles.color = colours.highlightedColour;
}


Handles.matrix = Matrix4x4.identity;
Vector3 lookForward = Vector3.up;
Camera cam = Camera.current;
if (cam != null)
{
if (cam.orthographic)
{
lookForward= -cam.transform.forward;
}
else
{
lookForward = (cam.transform.position - position);
}
}

capFunc(id, screenPosition, Quaternion.LookRotation(lookForward), handleDiameter, EventType.Repaint);
Handles.matrix = cachedMatrix;

Handles.color = originalColour;
break;

case EventType.Layout:
Handles.matrix = Matrix4x4.identity;
HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(screenPosition, handleDiameter / 2f));
Handles.matrix = cachedMatrix;
break;
}

return position;
}

public struct HandleColours
{
public Color defaultColour;
public Color highlightedColour;
public Color selectedColour;
public Color disabledColour;

public HandleColours(Color defaultColour, Color highlightedColour, Color selectedColour, Color disabledColour)
{
this.defaultColour = defaultColour;
this.highlightedColour = highlightedColour;
this.selectedColour = selectedColour;
this.disabledColour = disabledColour;
}
}

static void AddIDs(int upToIndex)
{
int numIDAtStart = ids.Count;
int numToAdd = (upToIndex - numIDAtStart) + 1;
for (int i = 0; i < numToAdd; i++)
{
ids.Add(GUIUtility.GetControlID(hint + numIDAtStart + i, FocusType.Passive));//
}
}

static int GetID(int handleIndex)
{
if (handleIndex >= ids.Count)
{
AddIDs(handleIndex);
}

return ids[handleIndex];
}
}
}
11 changes: 11 additions & 0 deletions PathCreator/Core/Editor/Helper/PathHandle.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ef4ee37

Please sign in to comment.