Skip to content

Commit

Permalink
added IMGUI Handles
Browse files Browse the repository at this point in the history
  • Loading branch information
alelievr committed Nov 10, 2017
1 parent dc7bfa1 commit f70443e
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 6 deletions.
128 changes: 128 additions & 0 deletions Editor/HandlesExtended.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.IMGUI.Controls;

public static class HandlesExtended
{

#region Handes Draw Functions

public static void DrawScaledCap(Handles.CapFunction capFunction, Vector3 center, Quaternion rotation, Vector3 size, Color color)
{
Handles.color = color;
Expand Down Expand Up @@ -72,4 +76,128 @@ public static void DrawSphere(Vector3 center, Quaternion rotation, Vector3 size,
DrawScaledCap(Handles.SphereHandleCap, center, rotation, size, color);
}

#endregion

#region Handles controls Functions

static ArcHandle arcHandle = new ArcHandle();
static BoxBoundsHandle boxBoundsHandle = new BoxBoundsHandle();
static CapsuleBoundsHandle capsuleBoundsHandle = new CapsuleBoundsHandle();
static JointAngularLimitHandle jointAngularLimitHandle = new JointAngularLimitHandle();
static SphereBoundsHandle sphereBoundsHandle = new SphereBoundsHandle();

public static void ArcHandle(Vector3 center, Quaternion rotation, Vector3 size, ref float angle, ref float radius) { ArcHandle(center, rotation, size, ref angle, ref radius, arcHandle.fillColor, arcHandle.wireframeColor); }
public static void ArcHandle(Vector3 center, Quaternion rotation, Vector3 size, ref float angle, ref float radius, Color fillColor, Color wireframeColor)
{
Matrix4x4 trs = Matrix4x4.TRS(center, rotation, size);

using (new Handles.DrawingScope(trs))
{
arcHandle.angle = angle;
arcHandle.radius = radius;
arcHandle.radiusHandleColor = Color.white;
arcHandle.fillColor = fillColor;
arcHandle.wireframeColor = wireframeColor;
arcHandle.DrawHandle();
angle = arcHandle.angle;
radius = arcHandle.radius;
}
}

public static void BoxBoundsHandle(Vector3 center, Quaternion rotation, Vector3 size, ref Vector3 boxSize) { BoxBoundsHandle(center, rotation, size, ref boxSize, PrimitiveBoundsHandle.Axes.All); }
public static void BoxBoundsHandle(Vector3 center, Quaternion rotation, Vector3 size, ref Vector3 boxSize, PrimitiveBoundsHandle.Axes handleAxes) { BoxBoundsHandle(center, rotation, size, ref boxSize, handleAxes, boxBoundsHandle.wireframeColor, boxBoundsHandle.handleColor); }
public static void BoxBoundsHandle(Vector3 center, Quaternion rotation, Vector3 size, ref Vector3 boxSize, PrimitiveBoundsHandle.Axes handleAxes, Color wireframeColor, Color handleColor)
{
Matrix4x4 trs = Matrix4x4.TRS(center, rotation, size);

using (new Handles.DrawingScope(trs))
{
boxBoundsHandle.axes = handleAxes;
boxBoundsHandle.size = boxSize;
boxBoundsHandle.handleColor = handleColor;
boxBoundsHandle.wireframeColor = wireframeColor;
boxBoundsHandle.DrawHandle();
boxSize = boxBoundsHandle.size;
}
}

public static void CapsuleBoundsHandle(Vector3 center, Quaternion rotation, Vector3 size, ref float height, ref float radius) { CapsuleBoundsHandle(center, rotation, size, ref height, ref radius, capsuleBoundsHandle.heightAxis, PrimitiveBoundsHandle.Axes.All); }
public static void CapsuleBoundsHandle(Vector3 center, Quaternion rotation, Vector3 size, ref float height, ref float radius,CapsuleBoundsHandle.HeightAxis heightAxis, PrimitiveBoundsHandle.Axes handleAxes) { CapsuleBoundsHandle(center, rotation, size, ref height, ref radius, heightAxis, handleAxes, capsuleBoundsHandle.handleColor, capsuleBoundsHandle.wireframeColor); }
public static void CapsuleBoundsHandle(Vector3 center, Quaternion rotation, Vector3 size, ref float height, ref float radius, CapsuleBoundsHandle.HeightAxis heightAxis, PrimitiveBoundsHandle.Axes handleAxes, Color handleColor, Color wireframeColor)
{
Matrix4x4 trs = Matrix4x4.TRS(center, rotation, size);

using (new Handles.DrawingScope(trs))
{
capsuleBoundsHandle.heightAxis = heightAxis;
capsuleBoundsHandle.radius = radius;
capsuleBoundsHandle.height = height;
capsuleBoundsHandle.handleColor = handleColor;
capsuleBoundsHandle.wireframeColor = wireframeColor;
capsuleBoundsHandle.DrawHandle();
radius = capsuleBoundsHandle.radius;
height = capsuleBoundsHandle.height;
}
}

public static void JointAngularLimitHandle(Vector3 center, Quaternion rotation, Vector3 size, ref Vector3 minAngles, ref Vector3 maxAngles) { JointAngularLimitHandle(center, rotation, size, ref minAngles, ref maxAngles, jointAngularLimitHandle.xHandleColor, jointAngularLimitHandle.yHandleColor, jointAngularLimitHandle.zHandleColor); }
public static void JointAngularLimitHandle(Vector3 center, Quaternion rotation, Vector3 size, ref Vector3 minAngles, ref Vector3 maxAngles, Color xHandleColor, Color yHandleColor, Color zHandleColor)
{
Matrix4x4 trs = Matrix4x4.TRS(center, rotation, size);

using (new Handles.DrawingScope(trs))
{
jointAngularLimitHandle.xHandleColor = xHandleColor;
jointAngularLimitHandle.yHandleColor = yHandleColor;
jointAngularLimitHandle.zHandleColor = zHandleColor;

jointAngularLimitHandle.xMin = minAngles.x;
jointAngularLimitHandle.yMin = minAngles.y;
jointAngularLimitHandle.zMin = minAngles.z;
jointAngularLimitHandle.xMax = maxAngles.x;
jointAngularLimitHandle.yMax = maxAngles.y;
jointAngularLimitHandle.zMax = maxAngles.z;

jointAngularLimitHandle.DrawHandle();

minAngles.x = jointAngularLimitHandle.xMin;
minAngles.y = jointAngularLimitHandle.yMin;
minAngles.z = jointAngularLimitHandle.zMin;
maxAngles.x = jointAngularLimitHandle.xMax;
maxAngles.y = jointAngularLimitHandle.yMax;
maxAngles.z = jointAngularLimitHandle.zMax;
}
}

public static void SphereBoundsHandle(Vector3 center, Quaternion rotation, Vector3 size, ref float radius) { SphereBoundsHandle(center, rotation, size, ref radius, PrimitiveBoundsHandle.Axes.All); }
public static void SphereBoundsHandle(Vector3 center, Quaternion rotation, Vector3 size, ref float radius, PrimitiveBoundsHandle.Axes handleAxes) { SphereBoundsHandle(center, rotation, size, ref radius, handleAxes, sphereBoundsHandle.handleColor, sphereBoundsHandle.wireframeColor); }
public static void SphereBoundsHandle(Vector3 center, Quaternion rotation, Vector3 size, ref float radius, PrimitiveBoundsHandle.Axes handleAxes, Color handleColor, Color wireframeColor)
{
Matrix4x4 trs = Matrix4x4.TRS(center, rotation, size);

using (new Handles.DrawingScope(trs))
{
sphereBoundsHandle.radius = radius;

sphereBoundsHandle.axes = handleAxes;
sphereBoundsHandle.wireframeColor = wireframeColor;
sphereBoundsHandle.handleColor = handleColor;

sphereBoundsHandle.DrawHandle();

radius = sphereBoundsHandle.radius;
}
}

#endregion

#region Full custom Handles

public static void CurveHandle(Vector3 startPoint, Vector3 EndPoint, Gradient curveGradient, float yScale = 1)
{

}

#endregion

}
22 changes: 16 additions & 6 deletions ExamplesScenes/Editor/HandlesExampleWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@
using UnityEditor;
using System.Linq;
using System;
using UnityEditor.IMGUI.Controls;

public class HandlesExampleWindow : EditorWindow
{
[NonSerialized]
bool focus = false;
bool focus = false;
[SerializeField]
string currentKey = null;
string currentKey = null;

static float angle = 42, radius = 2, capsRadius = .5f, height = 2;
static Vector3 boxSize = new Vector3(2, 1, 2);
static Vector3 minAngles = new Vector3(0, 0, 0), maxAngles = new Vector3(45, 45, 45);

Dictionary< string, Action > handlesActions = new Dictionary< string, Action >()
{
{"Simple custom handles (using existing Handles)", null},
{"Simple custom Handles (using existing Handles)", null},
{"Solid scaled cube", () => HandlesExtended.DrawSolidCube(Vector3.zero, Quaternion.identity, new Vector3(5, 1, 3), new Color(1f, 0, .2f, .2f))},
{"Wire rotated cube", () => HandlesExtended.DrawWireCube(Vector3.zero, Quaternion.Euler(45, 45, 0), new Vector3(2, 1, 2), new Color(1f, 0, .2f, 1f))},
{"Solid scaled cylinder", () => HandlesExtended.DrawCylinder(Vector3.zero, Quaternion.Euler(90, 0, 90), new Vector3(1, 3, 2), new Color(0, 1, 0, .3f))},
{"Solid scaled cone", () => HandlesExtended.DrawCone(Vector3.zero, Quaternion.Euler(-90, 0, 0), new Vector3(1, 3, 2), new Color(0, 0, 1, .3f))},
{"Solid scaled circle", () => HandlesExtended.DrawCircle(Vector3.zero, Quaternion.Euler(45, 0, 45), new Vector3(1, 3, 2), new Color(0, 0, 1, .3f))},
{"Solid scaled circle", () => HandlesExtended.DrawCircle(Vector3.zero, Quaternion.Euler(45, 0, 45), new Vector3(1, 3, 2), new Color(0, 1, 0, 1f))},
{"Solid scaled arrow", () => HandlesExtended.DrawArrow(Vector3.zero, Quaternion.Euler(-90, 0, 0), new Vector3(1, 3, 1), new Color(0, 0, 1, .3f))},
{"Solid scaled rectangle", () => HandlesExtended.DrawRectange(Vector3.zero, Quaternion.Euler(90, 0, 90), new Vector3(1, 3, 2), new Color(0, 0, 1, .3f))},
{"Solid scaled rectangle", () => HandlesExtended.DrawRectange(Vector3.zero, Quaternion.Euler(90, 0, 90), new Vector3(1, 3, 2), new Color(0, 0, 1, 1f))},
{"Solid scaled sphere", () => HandlesExtended.DrawSphere(Vector3.zero, Quaternion.Euler(90, 0, 90), new Vector3(1, 3, 2), new Color(0, 0, 1, .3f))},
{"Full custom Handles", null},
{"", () => {}},
{"IMGUI Handles", null},
{"Arc Handle", () => HandlesExtended.ArcHandle(Vector3.zero, Quaternion.identity, Vector3.one, ref angle, ref radius, new Color(1, 0, 0, .1f), new Color(0, 0, 1, 1f))},
{"Box Bounds Handle", () => HandlesExtended.BoxBoundsHandle(Vector3.zero, Quaternion.identity, Vector3.one, ref boxSize, PrimitiveBoundsHandle.Axes.All, new Color(1, 0, 0, 1f), new Color(0, 0, 1, 1f))},
{"Capsule Bounds Handle", () => HandlesExtended.CapsuleBoundsHandle(Vector3.zero, Quaternion.identity, Vector3.one, ref height, ref capsRadius)},
{"Joint Angular Limit Handle", () => HandlesExtended.JointAngularLimitHandle(Vector3.zero, Quaternion.identity, Vector3.one, ref minAngles, ref maxAngles)},
{"Sphere Bounds Handle", () => HandlesExtended.SphereBoundsHandle(Vector3.zero, Quaternion.identity, Vector3.one, ref radius)},
};

[MenuItem("Window/Handles Examples")]
Expand Down

0 comments on commit f70443e

Please sign in to comment.