private bool DrawTangentHandle(int id, Vector3 handlePos, Vector3 origin, Vector3 direction, Plane draggingPlane, ref float newDistance) { bool isChanged = false; float size = HandleUtility.GetHandleSize(handlePos); float handleRadius = size * 0.25f; float cursorDistancePx = HandleUtility.DistanceToCircle(handlePos, handleRadius * 0.5f); Event e = Event.current; bool leftClick = e.button == 0; bool isDragging = GUIUtility.hotControl == id && leftClick; bool isHover = HandleUtility.nearestControl == id; switch (e.type) { case EventType.Layout: HandleUtility.AddControl(id, cursorDistancePx); break; case EventType.MouseDown: if (isHover && leftClick) { GUIUtility.hotControl = id; GUIUtility.keyboardControl = id; e.Use(); } break; case EventType.MouseDrag: if (isDragging) { Ray r = HandleUtility.GUIPointToWorldRay(e.mousePosition); if (draggingPlane.Raycast(r, out float dist)) { Vector3 intersectionPt = r.GetPoint(dist); // On bloque a .5 car il faut pas que se soit 0 ou negatif float projectedDistance = Vector3.Dot(intersectionPt - origin, direction).AtLeast(.5f); newDistance = projectedDistance; isChanged = true; } e.Use(); } break; case EventType.MouseUp: if (isDragging) { GUIUtility.hotControl = 0; e.Use(); } break; case EventType.Repaint: Color color = GetHandleColor(isHover, isDragging); using (new TemporaryHandleColor(color)) { Handles.DrawAAPolyLine(origin, handlePos); Quaternion rot = Quaternion.LookRotation(direction); Handles.SphereHandleCap(id, handlePos, rot, handleRadius, EventType.Repaint); } break; } return(isChanged); }
public void OnSceneGUI() { var script = target as GraphUpdateScene; // Make sure the points array is not null script.points = script.points ?? new Vector3[0]; List <Vector3> points = Pathfinding.Util.ListPool <Vector3> .Claim(); points.AddRange(script.points); Matrix4x4 invMatrix = script.useWorldSpace ? Matrix4x4.identity : script.transform.worldToLocalMatrix; if (!script.useWorldSpace) { Matrix4x4 matrix = script.transform.localToWorldMatrix; for (int i = 0; i < points.Count; i++) { points[i] = matrix.MultiplyPoint3x4(points[i]); } } if (Tools.current != Tool.View && Event.current.type == EventType.Layout) { for (int i = 0; i < script.points.Length; i++) { HandleUtility.AddControl(-i - 1, HandleUtility.DistanceToLine(points[i], points[i])); } } if (Tools.current != Tool.View) { HandleUtility.AddDefaultControl(0); } for (int i = 0; i < points.Count; i++) { if (i == selectedPoint && Tools.current == Tool.Move) { Handles.color = PointSelectedColor; Undo.RecordObject(script, "Moved Point"); Handles.SphereCap(-i - 1, points[i], Quaternion.identity, HandleUtility.GetHandleSize(points[i]) * pointGizmosRadius * 2); Vector3 pre = points[i]; Vector3 post = Handles.PositionHandle(points[i], Quaternion.identity); if (pre != post) { script.points[i] = invMatrix.MultiplyPoint3x4(post); } } else { Handles.color = PointColor; Handles.SphereCap(-i - 1, points[i], Quaternion.identity, HandleUtility.GetHandleSize(points[i]) * pointGizmosRadius); } } if (Event.current.type == EventType.MouseDown) { int pre = selectedPoint; selectedPoint = -(HandleUtility.nearestControl + 1); if (pre != selectedPoint) { GUI.changed = true; } } if (Event.current.type == EventType.MouseDown && Event.current.shift && Tools.current == Tool.Move) { if (((int)Event.current.modifiers & (int)EventModifiers.Alt) != 0) { if (selectedPoint >= 0 && selectedPoint < points.Count) { Undo.RecordObject(script, "Removed Point"); var arr = new List <Vector3>(script.points); arr.RemoveAt(selectedPoint); points.RemoveAt(selectedPoint); script.points = arr.ToArray(); script.RecalcConvex(); GUI.changed = true; } } else if (((int)Event.current.modifiers & (int)EventModifiers.Control) != 0 && points.Count > 1) { int minSeg = 0; float minDist = float.PositiveInfinity; for (int i = 0; i < points.Count; i++) { float dist = HandleUtility.DistanceToLine(points[i], points[(i + 1) % points.Count]); if (dist < minDist) { minSeg = i; minDist = dist; } } System.Object hit = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(Event.current.mousePosition)); if (hit != null) { var rayhit = (RaycastHit)hit; Undo.RecordObject(script, "Added Point"); var arr = Pathfinding.Util.ListPool <Vector3> .Claim(); arr.AddRange(script.points); points.Insert(minSeg + 1, rayhit.point); if (!script.useWorldSpace) { rayhit.point = invMatrix.MultiplyPoint3x4(rayhit.point); } arr.Insert(minSeg + 1, rayhit.point); script.points = arr.ToArray(); script.RecalcConvex(); Pathfinding.Util.ListPool <Vector3> .Release(arr); GUI.changed = true; } } else { System.Object hit = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(Event.current.mousePosition)); if (hit != null) { var rayhit = (RaycastHit)hit; Undo.RecordObject(script, "Added Point"); var arr = new Vector3[script.points.Length + 1]; for (int i = 0; i < script.points.Length; i++) { arr[i] = script.points[i]; } points.Add(rayhit.point); if (!script.useWorldSpace) { rayhit.point = invMatrix.MultiplyPoint3x4(rayhit.point); } arr[script.points.Length] = rayhit.point; script.points = arr; script.RecalcConvex(); GUI.changed = true; } } Event.current.Use(); } // Make sure the convex hull stays up to date script.RecalcConvex(); Pathfinding.Util.ListPool <Vector3> .Release(points); if (GUI.changed) { HandleUtility.Repaint(); EditorUtility.SetDirty(target); } }
private static Vector2 CalcDeltaAlongDirections(int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.CapFunction capFunction, Vector2 snap, bool drawHelper) { Vector3 vector = handlePos + offset; Quaternion rotation = Quaternion.LookRotation(handleDir, slideDir1); Vector2 vector2 = new Vector2(0f, 0f); Event current = Event.current; switch (current.GetTypeForControl(id)) { case EventType.MouseDown: if (((HandleUtility.nearestControl == id && current.button == 0) || (GUIUtility.keyboardControl == id && current.button == 2)) && GUIUtility.hotControl == 0) { bool flag = true; Vector3 a = Handles.s_InverseMatrix.MultiplyPoint(Slider2D.GetMousePosition(handleDir, handlePos, ref flag)); if (flag) { GUIUtility.keyboardControl = id; GUIUtility.hotControl = id; Slider2D.s_CurrentMousePosition = current.mousePosition; Slider2D.s_StartPosition = handlePos; Vector3 lhs = a - handlePos; Slider2D.s_StartPlaneOffset.x = Vector3.Dot(lhs, slideDir1); Slider2D.s_StartPlaneOffset.y = Vector3.Dot(lhs, slideDir2); current.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } } break; case EventType.MouseUp: if (GUIUtility.hotControl == id && (current.button == 0 || current.button == 2)) { GUIUtility.hotControl = 0; current.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { Slider2D.s_CurrentMousePosition += current.delta; bool flag2 = true; Vector3 point = Handles.s_InverseMatrix.MultiplyPoint(Slider2D.GetMousePosition(handleDir, handlePos, ref flag2)); if (flag2) { vector2.x = HandleUtility.PointOnLineParameter(point, Slider2D.s_StartPosition, slideDir1); vector2.y = HandleUtility.PointOnLineParameter(point, Slider2D.s_StartPosition, slideDir2); vector2 -= Slider2D.s_StartPlaneOffset; if (snap.x > 0f || snap.y > 0f) { vector2.x = Handles.SnapValue(vector2.x, snap.x); vector2.y = Handles.SnapValue(vector2.y, snap.y); } GUI.changed = true; } current.Use(); } break; case EventType.Repaint: if (capFunction != null) { Color color = Color.white; if (id == GUIUtility.keyboardControl) { color = Handles.color; Handles.color = Handles.selectedColor; } capFunction(id, vector, rotation, handleSize, EventType.Repaint); if (id == GUIUtility.keyboardControl) { Handles.color = color; } if (drawHelper && GUIUtility.hotControl == id) { Vector3[] array = new Vector3[4]; float d = handleSize * 10f; array[0] = vector + (slideDir1 * d + slideDir2 * d); array[1] = array[0] - slideDir1 * d * 2f; array[2] = array[1] - slideDir2 * d * 2f; array[3] = array[2] + slideDir1 * d * 2f; Color color2 = Handles.color; Handles.color = Color.white; float num = 0.6f; Handles.DrawSolidRectangleWithOutline(array, new Color(1f, 1f, 1f, 0.05f), new Color(num, num, num, 0.4f)); Handles.color = color2; } } break; case EventType.Layout: if (capFunction != null) { capFunction(id, vector, rotation, handleSize, EventType.Layout); } else { HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(handlePos + offset, handleSize * 0.5f)); } break; } return(vector2); }
public static Vector3 Do(int id, Vector3 position, Quaternion rotation, float size, Vector3 snap, Handles.DrawCapFunction capFunc) { bool flag; Vector3 vector = Handles.matrix.MultiplyPoint(position); Matrix4x4 matrix = Handles.matrix; VertexSnapping.HandleKeyAndMouseMove(id); Event current = Event.current; switch (current.GetTypeForControl(id)) { case EventType.MouseDown: if (((HandleUtility.nearestControl == id) && (current.button == 0)) || ((GUIUtility.keyboardControl == id) && (current.button == 2))) { int num3 = id; GUIUtility.keyboardControl = num3; GUIUtility.hotControl = num3; s_CurrentMousePosition = s_StartMousePosition = current.mousePosition; s_StartPosition = position; HandleUtility.ignoreRaySnapObjects = null; current.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } return(position); case EventType.MouseUp: if ((GUIUtility.hotControl == id) && ((current.button == 0) || (current.button == 2))) { GUIUtility.hotControl = 0; HandleUtility.ignoreRaySnapObjects = null; current.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } return(position); case EventType.MouseMove: case EventType.KeyDown: case EventType.KeyUp: case EventType.ScrollWheel: return(position); case EventType.MouseDrag: if (GUIUtility.hotControl != id) { return(position); } flag = EditorGUI.actionKey && current.shift; if (flag) { if (HandleUtility.ignoreRaySnapObjects == null) { Handles.SetupIgnoreRaySnapObjects(); } object obj2 = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(current.mousePosition)); if (obj2 == null) { flag = false; break; } RaycastHit hit = (RaycastHit)obj2; float num = 0f; if (Tools.pivotMode == PivotMode.Center) { float num2 = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, hit.normal); if (num2 != float.PositiveInfinity) { num = Vector3.Dot(position, hit.normal) - num2; } } position = Handles.s_InverseMatrix.MultiplyPoint(hit.point + ((Vector3)(hit.normal * num))); } break; case EventType.Repaint: { Color white = Color.white; if (id == GUIUtility.keyboardControl) { white = Handles.color; Handles.color = Handles.selectedColor; } Handles.matrix = Matrix4x4.identity; capFunc(id, vector, Camera.current.transform.rotation, size); Handles.matrix = matrix; if (id == GUIUtility.keyboardControl) { Handles.color = white; } return(position); } case EventType.Layout: Handles.matrix = Matrix4x4.identity; HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(vector, size * 1.2f)); Handles.matrix = matrix; return(position); default: return(position); } if (!flag) { s_CurrentMousePosition += new Vector2(current.delta.x, -current.delta.y); Vector3 vector2 = Camera.current.WorldToScreenPoint(Handles.s_Matrix.MultiplyPoint(s_StartPosition)) + (s_CurrentMousePosition - s_StartMousePosition); position = Handles.s_InverseMatrix.MultiplyPoint(Camera.current.ScreenToWorldPoint(vector2)); if ((Camera.current.transform.forward == Vector3.forward) || (Camera.current.transform.forward == -Vector3.forward)) { position.z = s_StartPosition.z; } if ((Camera.current.transform.forward == Vector3.up) || (Camera.current.transform.forward == -Vector3.up)) { position.y = s_StartPosition.y; } if ((Camera.current.transform.forward == Vector3.right) || (Camera.current.transform.forward == -Vector3.right)) { position.x = s_StartPosition.x; } if (Tools.vertexDragging) { Vector3 vector3; if (HandleUtility.ignoreRaySnapObjects == null) { Handles.SetupIgnoreRaySnapObjects(); } if (HandleUtility.FindNearestVertex(current.mousePosition, null, out vector3)) { position = Handles.s_InverseMatrix.MultiplyPoint(vector3); } } if (EditorGUI.actionKey && !current.shift) { Vector3 vector4 = position - s_StartPosition; vector4.x = Handles.SnapValue(vector4.x, snap.x); vector4.y = Handles.SnapValue(vector4.y, snap.y); vector4.z = Handles.SnapValue(vector4.z, snap.z); position = s_StartPosition + vector4; } } GUI.changed = true; current.Use(); return(position); }
public void OnSceneGUI() { if (this._target.line == null) { return; } if (PrefabStageUtility.GetCurrentPrefabStage() != null && this._target.TrainHandler == null) { return; } if (PrefabStageUtility.GetCurrentPrefabStage() == null && !this._target.line.useWorldSpace) { return; } Vector3 offset1 = Vector3.up * 10; //get the position of the line point Vector3 positionStart = _target.line.GetPosition(0); Vector3 positionStop = _target.line.GetPosition(_target.line.positionCount - 1); //if the line is object space (!= worldspace) then we have to calculte the world space Vector3 calculatedWorldSpacePositionStart = Vector3.zero; Vector3 calculatedWorldSpacePositionStop = Vector3.zero; if (!this._target.line.useWorldSpace) { calculatedWorldSpacePositionStart = _target.TrainHandler.transform.TransformPoint(positionStart); calculatedWorldSpacePositionStop = _target.TrainHandler.transform.TransformPoint(positionStop); } if (_target.line.positionCount > 1) { Vector3 positionHandlePositionStart; Vector3 positionHandlePositionStop; if (_target.line.useWorldSpace) { positionHandlePositionStart = positionStart; positionHandlePositionStop = positionStop; } else { positionHandlePositionStart = calculatedWorldSpacePositionStart; positionHandlePositionStop = calculatedWorldSpacePositionStop; } //get the new world space position of the handle Vector3 newPositionHandlePositionStart = Handles.PositionHandle(positionHandlePositionStart, Quaternion.identity); Vector3 newPositionHandlePositionStop = Handles.PositionHandle(positionHandlePositionStop, Quaternion.identity); //if the line is object space (!= worldspace) then we have to calculte the object space to set the new position Vector3 newCalculatedObjectSpacePositionStart = Vector3.zero; Vector3 newCalculatedObjectSpacePositionStop = Vector3.zero; if (!this._target.line.useWorldSpace) { newCalculatedObjectSpacePositionStart = _target.TrainHandler.transform.InverseTransformPoint(newPositionHandlePositionStart); newCalculatedObjectSpacePositionStop = _target.TrainHandler.transform.InverseTransformPoint(newPositionHandlePositionStop); } Vector3 newPositionStart; Vector3 newPositionStop; if (_target.line.useWorldSpace) { newPositionStart = newPositionHandlePositionStart; newPositionStop = newPositionHandlePositionStop; } else { newPositionStart = newCalculatedObjectSpacePositionStart; newPositionStop = newCalculatedObjectSpacePositionStop; } HandleUtility.AddControl(0, 1); HandleUtility.AddControl(1, 1); Handles.color = Color.magenta; Handles.SphereHandleCap(0, positionHandlePositionStart, Quaternion.identity, Board.Instance.EditorPreset.TrackBallSize, EventType.Repaint); Handles.SphereHandleCap(1, positionHandlePositionStop, Quaternion.identity, Board.Instance.EditorPreset.TrackBallSize, EventType.Repaint); Vector3 diff = newPositionStop - _target.line.GetPosition(_target.line.positionCount - 2); if (newPositionStart != positionStart) { this._target.Disconnect(true); bool snapped = false; //detect if another handle is nearby ? if (this._target.TrainHandler != null) { if (this._target.TrainHandler.TrainHandlerType == TrainHandlerType.Card) { //if the trainhandler is a card, it means that we should stick to the hand of the card only ! foreach (Hand hand in this._target.TrainHandler.AllHands) { //if the the hand is not already occupied if (hand.TrainHandler != null) { float dist; dist = ((Vector2)hand.transform.position - (Vector2)newPositionHandlePositionStart).magnitude; if (dist < Board.Instance.EditorPreset.HandSnapDistance) { Undo.RecordObject(this._target, "TrackSnap change"); //Debug.Log(dist + " " + hand); this._target.ConnectToHand(hand, true, true); snapped = true; /*SerializedObject obj = new SerializedObject(this._target); * obj.FindProperty("_handAtBeginning").objectReferenceValue = hand; * obj.ApplyModifiedProperties(); * Undo.RecordObject(this._target, "Changing the component on myObject");*/ } } } } } else { //if there is not train handler, it means that we should stick to the hand of a box only ! foreach (Box box in Board.Instance.Boxes) { foreach (Hand hand in box.AllHands) { //if the the hand is not already occupied if (hand.TrainHandler != null) { float dist = ((Vector2)hand.transform.position - (Vector2)newPositionStart).magnitude; if (dist < Board.Instance.EditorPreset.HandSnapDistance) { Undo.RecordObject(this._target, "TrackSnap change"); //Debug.Log(dist + " " + hand); this._target.ConnectToHand(hand, true, true); snapped = true; } } } } } if (!snapped) { this._target.line.SetPosition(0, newPositionStart); } else { EditorUtility.SetDirty(this._target.gameObject); PrefabUtility.RecordPrefabInstancePropertyModifications(this._target); if (PrefabStageUtility.GetCurrentPrefabStage() == null) { //EditorSceneManager.MarkSceneDirty(this._target.gameObject.scene); //This used to happen automatically from SetDirty } } } if (newPositionStop != positionStop) { this._target.Disconnect(false); bool snapped = false; //detect if another handle is nearby ? if (this._target.TrainHandler != null) { if (this._target.TrainHandler.TrainHandlerType == TrainHandlerType.Card) { //if the trainhandler is a card, it means that we should stick to the hand of the card only ! foreach (Hand hand in this._target.TrainHandler.AllHands) { //if the the hand is not already occupied if (hand.TrainHandler != null) { float dist; dist = ((Vector2)hand.transform.position - (Vector2)newPositionHandlePositionStop).magnitude; if (dist < Board.Instance.EditorPreset.HandSnapDistance) { Undo.RecordObject(this._target, "TrackSnap change"); //Debug.Log(dist + " " + hand); this._target.ConnectToHand(hand, false, true); snapped = true; } } } } } else { //if there is not train handler, it means that we should stick to the hand of a box only ! foreach (Box box in Board.Instance.Boxes) { if (box != null) { foreach (Hand hand in box.AllHands) { //if the the hand is not already occupied if (hand.TrainHandler != null) { float dist = ((Vector2)hand.transform.position - (Vector2)newPositionStop).magnitude; if (dist < Board.Instance.EditorPreset.HandSnapDistance) { Undo.RecordObject(this._target, "TrackSnap change"); //Debug.Log(dist + " " + hand); this._target.ConnectToHand(hand, false, true); snapped = true; } } } } } } if (!snapped) { this._target.line.SetPosition(this._target.line.positionCount - 1, newPositionStop); } else { EditorUtility.SetDirty(this._target.gameObject); PrefabUtility.RecordPrefabInstancePropertyModifications(this._target); if (PrefabStageUtility.GetCurrentPrefabStage() == null) { //EditorSceneManager.MarkSceneDirty(this._target.gameObject.scene); //This used to happen automatically from SetDirty } } } } }
private static Vector2 CalcDeltaAlongDirections(int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.DrawCapFunction drawFunc, Vector2 snap, bool drawHelper) { Vector2 vector = new Vector2(0f, 0f); Event current = Event.current; switch (current.GetTypeForControl(id)) { case EventType.MouseDown: if ((((HandleUtility.nearestControl == id) && (current.button == 0)) || ((GUIUtility.keyboardControl == id) && (current.button == 2))) && (GUIUtility.hotControl == 0)) { Plane plane = new Plane(Handles.matrix.MultiplyVector(handleDir), Handles.matrix.MultiplyPoint(handlePos)); Ray ray = HandleUtility.GUIPointToWorldRay(current.mousePosition); float enter = 0f; plane.Raycast(ray, out enter); int num5 = id; GUIUtility.keyboardControl = num5; GUIUtility.hotControl = num5; s_CurrentMousePosition = current.mousePosition; s_StartPosition = handlePos; Vector3 lhs = Handles.s_InverseMatrix.MultiplyPoint(ray.GetPoint(enter)) - handlePos; s_StartPlaneOffset.x = Vector3.Dot(lhs, slideDir1); s_StartPlaneOffset.y = Vector3.Dot(lhs, slideDir2); current.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } return(vector); case EventType.MouseUp: if ((GUIUtility.hotControl == id) && ((current.button == 0) || (current.button == 2))) { GUIUtility.hotControl = 0; current.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } return(vector); case EventType.MouseMove: case EventType.KeyDown: case EventType.KeyUp: case EventType.ScrollWheel: return(vector); case EventType.MouseDrag: if (GUIUtility.hotControl == id) { s_CurrentMousePosition += current.delta; Vector3 a = Handles.matrix.MultiplyPoint(handlePos); Vector3 normalized = Handles.matrix.MultiplyVector(slideDir1).normalized; Vector3 vector6 = Handles.matrix.MultiplyVector(slideDir2).normalized; Ray ray2 = HandleUtility.GUIPointToWorldRay(s_CurrentMousePosition); Plane plane2 = new Plane(a, a + normalized, a + vector6); float num2 = 0f; if (plane2.Raycast(ray2, out num2)) { Vector3 point = Handles.s_InverseMatrix.MultiplyPoint(ray2.GetPoint(num2)); vector.x = HandleUtility.PointOnLineParameter(point, s_StartPosition, slideDir1); vector.y = HandleUtility.PointOnLineParameter(point, s_StartPosition, slideDir2); vector -= s_StartPlaneOffset; if ((snap.x > 0f) || (snap.y > 0f)) { vector.x = Handles.SnapValue(vector.x, snap.x); vector.y = Handles.SnapValue(vector.y, snap.y); } GUI.changed = true; } current.Use(); } return(vector); case EventType.Repaint: if (drawFunc != null) { Vector3 position = handlePos + offset; Quaternion rotation = Quaternion.LookRotation(handleDir, slideDir1); Color white = Color.white; if (id == GUIUtility.keyboardControl) { white = Handles.color; Handles.color = Handles.selectedColor; } drawFunc(id, position, rotation, handleSize); if (id == GUIUtility.keyboardControl) { Handles.color = white; } if (drawHelper && (GUIUtility.hotControl == id)) { Vector3[] verts = new Vector3[4]; float num3 = handleSize * 10f; verts[0] = position + ((Vector3)((slideDir1 * num3) + (slideDir2 * num3))); verts[1] = verts[0] - ((Vector3)((slideDir1 * num3) * 2f)); verts[2] = verts[1] - ((Vector3)((slideDir2 * num3) * 2f)); verts[3] = verts[2] + ((Vector3)((slideDir1 * num3) * 2f)); Color color = Handles.color; Handles.color = Color.white; float r = 0.6f; Handles.DrawSolidRectangleWithOutline(verts, new Color(1f, 1f, 1f, 0.05f), new Color(r, r, r, 0.4f)); Handles.color = color; } return(vector); } return(vector); case EventType.Layout: if (drawFunc != new Handles.DrawCapFunction(Handles.ArrowCap)) { if (drawFunc == new Handles.DrawCapFunction(Handles.RectangleCap)) { HandleUtility.AddControl(id, HandleUtility.DistanceToRectangle(handlePos + offset, Quaternion.LookRotation(handleDir, slideDir1), handleSize)); return(vector); } HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(handlePos + offset, handleSize * 0.5f)); return(vector); } HandleUtility.AddControl(id, HandleUtility.DistanceToLine(handlePos + offset, handlePos + ((Vector3)(handleDir * handleSize)))); HandleUtility.AddControl(id, HandleUtility.DistanceToCircle((handlePos + offset) + ((Vector3)(handleDir * handleSize)), handleSize * 0.2f)); return(vector); } return(vector); }
#pragma warning disable 618 private static Vector2 CalcDeltaAlongDirections( int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.DrawCapFunction drawFunc, Vector2 snap, bool drawHelper) #pragma warning restore 618 { Vector2 deltaDistanceAlongDirections = new Vector2(0, 0); Event evt = Event.current; switch (evt.GetTypeForControl(id)) { case EventType.Layout: // This is an ugly hack. It would be better if the drawFunc can handle it's own layout. if (drawFunc == Handles.ArrowCap) { HandleUtility.AddControl(id, HandleUtility.DistanceToLine(handlePos + offset, handlePos + handleDir * handleSize)); HandleUtility.AddControl(id, HandleUtility.DistanceToCircle((handlePos + offset) + handleDir * handleSize, handleSize * .2f)); } else if (drawFunc == Handles.RectangleCap) { HandleUtility.AddControl(id, HandleUtility.DistanceToRectangle(handlePos + offset, Quaternion.LookRotation(handleDir, slideDir1), handleSize)); } else { HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(handlePos + offset, handleSize * .5f)); } break; case EventType.MouseDown: // am I closest to the thingy? if (HandleUtility.nearestControl == id && evt.button == 0 && GUIUtility.hotControl == 0) { Plane plane = new Plane(Handles.matrix.MultiplyVector(handleDir), Handles.matrix.MultiplyPoint(handlePos)); Ray mouseRay = HandleUtility.GUIPointToWorldRay(evt.mousePosition); float dist = 0.0f; plane.Raycast(mouseRay, out dist); GUIUtility.hotControl = id; // Grab mouse focus s_CurrentMousePosition = evt.mousePosition; s_StartPosition = handlePos; Vector3 localMousePoint = Handles.inverseMatrix.MultiplyPoint(mouseRay.GetPoint(dist)); Vector3 clickOffset = localMousePoint - handlePos; s_StartPlaneOffset.x = Vector3.Dot(clickOffset, slideDir1); s_StartPlaneOffset.y = Vector3.Dot(clickOffset, slideDir2); evt.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { s_CurrentMousePosition += evt.delta; Vector3 worldPosition = Handles.matrix.MultiplyPoint(handlePos); Vector3 worldSlideDir1 = Handles.matrix.MultiplyVector(slideDir1).normalized; Vector3 worldSlideDir2 = Handles.matrix.MultiplyVector(slideDir2).normalized; // Detect hit with plane (ray from campos to cursor) Ray mouseRay = HandleUtility.GUIPointToWorldRay(s_CurrentMousePosition); Plane plane = new Plane(worldPosition, worldPosition + worldSlideDir1, worldPosition + worldSlideDir2); float dist = 0.0f; if (plane.Raycast(mouseRay, out dist)) { Vector3 hitpos = Handles.inverseMatrix.MultiplyPoint(mouseRay.GetPoint(dist)); // Determine hitpos projection onto slideDirs deltaDistanceAlongDirections.x = HandleUtility.PointOnLineParameter(hitpos, s_StartPosition, slideDir1); deltaDistanceAlongDirections.y = HandleUtility.PointOnLineParameter(hitpos, s_StartPosition, slideDir2); deltaDistanceAlongDirections -= s_StartPlaneOffset; if (snap.x > 0 || snap.y > 0) { deltaDistanceAlongDirections.x = Handles.SnapValue(deltaDistanceAlongDirections.x, snap.x); deltaDistanceAlongDirections.y = Handles.SnapValue(deltaDistanceAlongDirections.y, snap.y); } GUI.changed = true; } evt.Use(); } break; case EventType.MouseUp: if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2)) { GUIUtility.hotControl = 0; evt.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } break; case EventType.MouseMove: if (id == HandleUtility.nearestControl) { HandleUtility.Repaint(); } break; case EventType.Repaint: { if (drawFunc == null) { break; } Vector3 position = handlePos + offset; Quaternion rotation = Quaternion.LookRotation(handleDir, slideDir1); Color temp = Color.white; if (id == GUIUtility.hotControl) { temp = Handles.color; Handles.color = Handles.selectedColor; } else if (id == HandleUtility.nearestControl && GUIUtility.hotControl == 0) { temp = Handles.color; Handles.color = Handles.preselectionColor; } drawFunc(id, position, rotation, handleSize); if (id == GUIUtility.hotControl || id == HandleUtility.nearestControl && GUIUtility.hotControl == 0) { Handles.color = temp; } // Draw a helper rectangle to show what plane we are dragging in if (drawHelper && GUIUtility.hotControl == id) { Vector3[] verts = new Vector3[4]; float helperSize = handleSize * 10.0f; verts[0] = position + (slideDir1 * helperSize + slideDir2 * helperSize); verts[1] = verts[0] - slideDir1 * helperSize * 2.0f; verts[2] = verts[1] - slideDir2 * helperSize * 2.0f; verts[3] = verts[2] + slideDir1 * helperSize * 2.0f; Color prevColor = Handles.color; Handles.color = Color.white; float outline = 0.6f; Handles.DrawSolidRectangleWithOutline(verts, new Color(1, 1, 1, 0.05f), new Color(outline, outline, outline, 0.4f)); Handles.color = prevColor; } } break; } return(deltaDistanceAlongDirections); }
protected override void HandleEditShapeEvents(Rect sceneRect) { if (!SceneDragToolManager.IsDraggingObjectInScene && Event.current.type == EventType.Repaint) { if (visualSnappedEdges != null) { PaintUtility.DrawLines(visualSnappedEdges.ToArray(), GUIConstants.oldThickLineScale, ColorSettings.SnappedEdges); } if (visualSnappedGrid != null) { var _origMatrix = Handles.matrix; Handles.matrix = MathConstants.identityMatrix; PaintUtility.DrawDottedLines(visualSnappedGrid.ToArray(), ColorSettings.SnappedEdges); Handles.matrix = _origMatrix; } if (visualSnappedBrush != null) { if (visualSnappedBrush.compareTransformation != null && visualSnappedBrush.ChildData != null && visualSnappedBrush.ChildData.ModelTransform) { var brush_transformation = visualSnappedBrush.compareTransformation.localToWorldMatrix; CSGRenderer.DrawOutlines(visualSnappedBrush.brushNodeID, brush_transformation, ColorSettings.SelectedOutlines, ColorSettings.SelectedOutlines, ColorSettings.SelectedOutlines, ColorSettings.SelectedOutlines); } } var origMatrix = Handles.matrix; Handles.matrix = MathConstants.identityMatrix; /* * bool isValid; * var realVertices = settings.GetVertices(buildPlane, worldPosition, gridTangent, gridBinormal, out isValid); * if (editMode == EditMode.EditShape) * shapeIsValid = isValid; * * if (realVertices.Length > 0)*/ { PaintSquare(); PaintRadiusMessage(); } Handles.matrix = origMatrix; } HandleHeightHandles(sceneRect, false); for (int i = 1; i < settings.vertices.Length; i++) { var id = settings.vertexIDs[i]; var point_type = Event.current.GetTypeForControl(id); switch (point_type) { case EventType.Repaint: { if (SceneDragToolManager.IsDraggingObjectInScene) { break; } bool isSelected = id == GUIUtility.keyboardControl; var temp = Handles.color; var origMatrix = Handles.matrix; Handles.matrix = MathConstants.identityMatrix; var rotation = Camera.current.transform.rotation; if (isSelected) { Handles.color = ColorSettings.PointInnerStateColor[3]; } else if (HandleUtility.nearestControl == id) { Handles.color = ColorSettings.PointInnerStateColor[1]; } else { Handles.color = ColorSettings.PointInnerStateColor[0]; } float handleSize = CSG_HandleUtility.GetHandleSize(settings.vertices[i]); float scaledHandleSize = handleSize * GUIConstants.handleScale; PaintUtility.SquareDotCap(id, settings.vertices[i], rotation, scaledHandleSize); Handles.matrix = origMatrix; Handles.color = temp; break; } case EventType.Layout: { if ((Tools.viewTool != ViewTool.None && Tools.viewTool != ViewTool.Pan)) { break; } var origMatrix = Handles.matrix; Handles.matrix = MathConstants.identityMatrix; float handleSize = CSG_HandleUtility.GetHandleSize(settings.vertices[i]); float scaledHandleSize = handleSize * GUIConstants.handleScale; HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(settings.vertices[i], scaledHandleSize)); Handles.matrix = origMatrix; break; } case EventType.ValidateCommand: case EventType.KeyDown: { if (GUIUtility.hotControl == id) { if (Keys.CancelActionKey.IsKeyPressed()) { Event.current.Use(); break; } } break; } case EventType.KeyUp: { if (GUIUtility.hotControl == id) { if (Keys.CancelActionKey.IsKeyPressed()) { GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; EditorGUIUtility.editingTextField = false; Event.current.Use(); break; } } break; } case EventType.MouseDown: { if (!sceneRect.Contains(Event.current.mousePosition)) { break; } if (Tools.viewTool != ViewTool.None && Tools.viewTool != ViewTool.Pan) { break; } if (GUIUtility.hotControl == 0 && HandleUtility.nearestControl == id && Event.current.button == 0) { GUIUtility.hotControl = id; GUIUtility.keyboardControl = id; EditorGUIUtility.editingTextField = false; EditorGUIUtility.SetWantsMouseJumping(1); Event.current.Use(); break; } break; } case EventType.MouseDrag: { if (Tools.viewTool != ViewTool.None && Tools.viewTool != ViewTool.Pan) { break; } if (GUIUtility.hotControl == id && Event.current.button == 0) { Undo.RecordObject(this, "Modify shape"); var mouseRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); var alignedPlane = new CSGPlane(RealtimeCSG.CSGGrid.CurrentWorkGridPlane.normal, settings.vertices[0]); var worldPosition = buildPlane.Project(alignedPlane.RayIntersection(mouseRay)); if (float.IsInfinity(worldPosition.x) || float.IsNaN(worldPosition.x) || float.IsInfinity(worldPosition.y) || float.IsNaN(worldPosition.y) || float.IsInfinity(worldPosition.z) || float.IsNaN(worldPosition.z)) { worldPosition = settings.vertices[i]; } ResetVisuals(); if (snapFunction != null) { CSGBrush snappedOnBrush; worldPosition = snapFunction(worldPosition, buildPlane, ref base.visualSnappedEdges, out snappedOnBrush, generatedBrushes); } base.visualSnappedGrid = RealtimeCSG.CSGGrid.FindAllGridEdgesThatTouchPoint(worldPosition); settings.vertices[i] = worldPosition; if (editMode == EditMode.ExtrudeShape) { StartExtrudeMode(); } UpdateBaseShape(); UpdateExtrudedShape(); GUI.changed = true; Event.current.Use(); break; } break; } case EventType.MouseUp: { if (GUIUtility.hotControl != id) { break; } if (Tools.viewTool != ViewTool.None && Tools.viewTool != ViewTool.Pan) { break; } if (Event.current.button == 0) { GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; EditorGUIUtility.editingTextField = false; EditorGUIUtility.SetWantsMouseJumping(0); Event.current.Use(); ResetVisuals(); if (RadiusA == 0) { Cancel(); } break; } break; } } } }
private static Vector2 CalcDeltaAlongDirections(int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.DrawCapFunction drawFunc, Vector2 snap, bool drawHelper) { Vector2 vector2 = new Vector2(0.0f, 0.0f); Event current = Event.current; switch (current.GetTypeForControl(id)) { case EventType.MouseDown: if ((HandleUtility.nearestControl == id && current.button == 0 || GUIUtility.keyboardControl == id && current.button == 2) && GUIUtility.hotControl == 0) { Plane plane = new Plane(Handles.matrix.MultiplyVector(handleDir), Handles.matrix.MultiplyPoint(handlePos)); Ray worldRay = HandleUtility.GUIPointToWorldRay(current.mousePosition); float enter = 0.0f; plane.Raycast(worldRay, out enter); int num = id; GUIUtility.keyboardControl = num; GUIUtility.hotControl = num; Slider2D.s_CurrentMousePosition = current.mousePosition; Slider2D.s_StartPosition = handlePos; Vector3 lhs = Handles.s_InverseMatrix.MultiplyPoint(worldRay.GetPoint(enter)) - handlePos; Slider2D.s_StartPlaneOffset.x = Vector3.Dot(lhs, slideDir1); Slider2D.s_StartPlaneOffset.y = Vector3.Dot(lhs, slideDir2); current.Use(); EditorGUIUtility.SetWantsMouseJumping(1); break; } break; case EventType.MouseUp: if (GUIUtility.hotControl == id && (current.button == 0 || current.button == 2)) { GUIUtility.hotControl = 0; current.Use(); EditorGUIUtility.SetWantsMouseJumping(0); break; } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { Slider2D.s_CurrentMousePosition += current.delta; Vector3 a = Handles.matrix.MultiplyPoint(handlePos); Vector3 normalized1 = Handles.matrix.MultiplyVector(slideDir1).normalized; Vector3 normalized2 = Handles.matrix.MultiplyVector(slideDir2).normalized; Ray worldRay = HandleUtility.GUIPointToWorldRay(Slider2D.s_CurrentMousePosition); Plane plane = new Plane(a, a + normalized1, a + normalized2); float enter = 0.0f; if (plane.Raycast(worldRay, out enter)) { Vector3 point = Handles.s_InverseMatrix.MultiplyPoint(worldRay.GetPoint(enter)); vector2.x = HandleUtility.PointOnLineParameter(point, Slider2D.s_StartPosition, slideDir1); vector2.y = HandleUtility.PointOnLineParameter(point, Slider2D.s_StartPosition, slideDir2); vector2 -= Slider2D.s_StartPlaneOffset; if ((double)snap.x > 0.0 || (double)snap.y > 0.0) { vector2.x = Handles.SnapValue(vector2.x, snap.x); vector2.y = Handles.SnapValue(vector2.y, snap.y); } GUI.changed = true; } current.Use(); break; } break; case EventType.Repaint: if (drawFunc != null) { Vector3 position = handlePos + offset; Quaternion rotation = Quaternion.LookRotation(handleDir, slideDir1); Color color1 = Color.white; if (id == GUIUtility.keyboardControl) { color1 = Handles.color; Handles.color = Handles.selectedColor; } drawFunc(id, position, rotation, handleSize); if (id == GUIUtility.keyboardControl) { Handles.color = color1; } if (drawHelper && GUIUtility.hotControl == id) { Vector3[] verts = new Vector3[4]; float num1 = handleSize * 10f; verts[0] = position + slideDir1 * num1 + slideDir2 * num1; verts[1] = verts[0] - slideDir1 * num1 * 2f; verts[2] = verts[1] - slideDir2 * num1 * 2f; verts[3] = verts[2] + slideDir1 * num1 * 2f; Color color2 = Handles.color; Handles.color = Color.white; float num2 = 0.6f; Handles.DrawSolidRectangleWithOutline(verts, new Color(1f, 1f, 1f, 0.05f), new Color(num2, num2, num2, 0.4f)); Handles.color = color2; break; } break; } break; case EventType.Layout: if ((MulticastDelegate)drawFunc == (MulticastDelegate) new Handles.DrawCapFunction(Handles.ArrowCap)) { HandleUtility.AddControl(id, HandleUtility.DistanceToLine(handlePos + offset, handlePos + handleDir * handleSize)); HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(handlePos + offset + handleDir * handleSize, handleSize * 0.2f)); break; } if ((MulticastDelegate)drawFunc == (MulticastDelegate) new Handles.DrawCapFunction(Handles.RectangleCap)) { HandleUtility.AddControl(id, HandleUtility.DistanceToRectangle(handlePos + offset, Quaternion.LookRotation(handleDir, slideDir1), handleSize)); break; } HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(handlePos + offset, handleSize * 0.5f)); break; } return(vector2); }
private static bool SurfaceToolBase(int id, SelectionType selectionType, Rect dragArea) { // we only do tools when we do not use a modifier (shift, control etc.) if (selectionType != SelectionType.Replace) { return(false); } var evt = Event.current; switch (evt.GetTypeForControl(id)) { case EventType.Layout: { // Unless something else is closer, make sure our tool is selected HandleUtility.AddControl(id, kMaxControlDistance); break; } case EventType.ValidateCommand: { if (IsToolEnabled(id)) { if (evt.keyCode == KeyCode.Escape) { evt.Use(); break; } } if (!EditorGUIUtility.editingTextField) { if (evt.keyCode == KeyCode.V) { evt.Use(); break; } } break; } case EventType.KeyDown: { if (IsToolEnabled(id)) { if (evt.keyCode == KeyCode.Escape) { evt.Use(); break; } } if (!EditorGUIUtility.editingTextField) { if (evt.keyCode == KeyCode.V) { forceVertexSnapping = true; evt.Use(); break; } } break; } case EventType.KeyUp: { if (IsToolEnabled(id)) { if (evt.keyCode == KeyCode.Escape) { CancelTool(); break; } } if (forceVertexSnapping && evt.keyCode == KeyCode.V) { forceVertexSnapping = false; if (!EditorGUIUtility.editingTextField) { evt.Use(); } break; } break; } case EventType.MouseMove: { // In case we somehow missed a MouseUp event, we reset this bool MouseIsDown = false; break; } case EventType.MouseDown: { // We can only use a tool when the mouse cursor is inside the draggable scene area if (!dragArea.Contains(evt.mousePosition)) { return(false); } // We can only use a tool when we're hovering over a surfaces if (hoverSurfaces == null || hoverSurfaces.Count == 0) { return(false); } if (!CanEnableTool(id)) { break; } // We want to be able to tell the difference between dragging and clicking, // so we keep track if we dragged or not. In this case we haven't started dragging yet. ToolIsDragging = false; MouseIsDown = true; EnableTool(id); break; } case EventType.MouseDrag: { if (!IsToolEnabled(id)) { break; } if (!ToolIsDragging) { // If we haven't dragged the tool yet, check if the surface underneath // the mouse is selected or not, if it isn't: select it exclusively if (!ChiselSurfaceSelectionManager.IsAnySelected(hoverSurfaces)) { ClickSelection(dragArea, selectionType); } } // In the tool specific code, calling StartToolDragging will set ToolIsDragging to true, // which will allow us to tell the difference between clicking and dragging. break; } case EventType.MouseUp: { if (!IsToolEnabled(id)) { break; } MouseIsDown = false; // We want to be able to tell the difference between clicking and dragging, // so we use ToolIsDragging here to determine if we clicked. if (!ToolIsDragging) { // If we clicked on the surface, instead of dragged it, just click select it ClickSelection(dragArea, selectionType); } ToolIsDragging = false; DisableTool(); ResetSelection(); break; } case EventType.Repaint: { RenderIntersection(); break; } } return(true); }
public void OnSceneGUI() { GraphUpdateScene script = target as GraphUpdateScene; if (script.points == null) { script.points = new Vector3[0]; } List <Vector3> points = Pathfinding.Util.ListPool <Vector3> .Claim(); points.AddRange(script.points); Matrix4x4 invMatrix = script.useWorldSpace ? Matrix4x4.identity : script.transform.worldToLocalMatrix; if (!script.useWorldSpace) { Matrix4x4 matrix = script.transform.localToWorldMatrix; for (int i = 0; i < points.Count; i++) { points[i] = matrix.MultiplyPoint3x4(points[i]); } } if (Tools.current != Tool.View && Event.current.type == EventType.Layout) { for (int i = 0; i < script.points.Length; i++) { HandleUtility.AddControl(-i - 1, HandleUtility.DistanceToLine(points[i], points[i])); } } if (Tools.current != Tool.View) { HandleUtility.AddDefaultControl(0); } for (int i = 0; i < points.Count; i++) { if (i == selectedPoint && Tools.current == Tool.Move) { Handles.color = PointSelectedColor; #if UNITY_LE_4_3 Undo.SetSnapshotTarget(script, "Moved Point"); #else Undo.RecordObject(script, "Moved Point"); #endif Handles.SphereCap(-i - 1, points[i], Quaternion.identity, HandleUtility.GetHandleSize(points[i]) * pointGizmosRadius * 2); Vector3 pre = points[i]; Vector3 post = Handles.PositionHandle(points[i], Quaternion.identity); if (pre != post) { script.points[i] = invMatrix.MultiplyPoint3x4(post); } } else { Handles.color = PointColor; Handles.SphereCap(-i - 1, points[i], Quaternion.identity, HandleUtility.GetHandleSize(points[i]) * pointGizmosRadius); } } #if UNITY_LE_4_3 if (Input.GetMouseButtonDown(0)) { // Register the undos when we press the Mouse button. Undo.CreateSnapshot(); Undo.RegisterSnapshot(); } #endif if (Event.current.type == EventType.MouseDown) { int pre = selectedPoint; selectedPoint = -(HandleUtility.nearestControl + 1); if (pre != selectedPoint) { GUI.changed = true; } } if (Event.current.type == EventType.MouseDown && Event.current.shift && Tools.current == Tool.Move) { if (((int)Event.current.modifiers & (int)EventModifiers.Alt) != 0) { //int nearestControl = -(HandleUtility.nearestControl+1); if (selectedPoint >= 0 && selectedPoint < points.Count) { #if UNITY_LE_4_3 Undo.RegisterUndo(script, "Removed Point"); #else Undo.RecordObject(script, "Removed Point"); #endif List <Vector3> arr = new List <Vector3>(script.points); arr.RemoveAt(selectedPoint); points.RemoveAt(selectedPoint); script.points = arr.ToArray(); script.RecalcConvex(); GUI.changed = true; } } else if (((int)Event.current.modifiers & (int)EventModifiers.Control) != 0 && points.Count > 1) { int minSeg = 0; float minDist = float.PositiveInfinity; for (int i = 0; i < points.Count; i++) { float dist = HandleUtility.DistanceToLine(points[i], points[(i + 1) % points.Count]); if (dist < minDist) { minSeg = i; minDist = dist; } } System.Object hit = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(Event.current.mousePosition)); if (hit != null) { RaycastHit rayhit = (RaycastHit)hit; #if UNITY_LE_4_3 Undo.RegisterUndo(script, "Added Point"); #else Undo.RecordObject(script, "Added Point"); #endif List <Vector3> arr = Pathfinding.Util.ListPool <Vector3> .Claim(); arr.AddRange(script.points); points.Insert(minSeg + 1, rayhit.point); if (!script.useWorldSpace) { rayhit.point = invMatrix.MultiplyPoint3x4(rayhit.point); } arr.Insert(minSeg + 1, rayhit.point); script.points = arr.ToArray(); script.RecalcConvex(); Pathfinding.Util.ListPool <Vector3> .Release(arr); GUI.changed = true; } } else { System.Object hit = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(Event.current.mousePosition)); if (hit != null) { RaycastHit rayhit = (RaycastHit)hit; #if UNITY_LE_4_3 Undo.RegisterUndo(script, "Added Point"); #else Undo.RecordObject(script, "Added Point"); #endif Vector3[] arr = new Vector3[script.points.Length + 1]; for (int i = 0; i < script.points.Length; i++) { arr[i] = script.points[i]; } points.Add(rayhit.point); if (!script.useWorldSpace) { rayhit.point = invMatrix.MultiplyPoint3x4(rayhit.point); } arr[script.points.Length] = rayhit.point; script.points = arr; script.RecalcConvex(); GUI.changed = true; } } Event.current.Use(); } if (Event.current.shift && Event.current.type == EventType.MouseDrag) { //Event.current.Use (); } #if !UNITY_LE_4_3 if (lastUndoGroup != Undo.GetCurrentGroup()) { script.RecalcConvex(); } #endif Pathfinding.Util.ListPool <Vector3> .Release(points); if (GUI.changed) { HandleUtility.Repaint(); EditorUtility.SetDirty(target); } }
static bool SurfaceSelection(Rect dragArea, SelectionType selectionType) { var id = GUIUtility.GetControlID(kSurfaceDragSelectionHash, FocusType.Keyboard, dragArea); bool repaint = false; var evt = Event.current; if (evt.type == EventType.MouseMove || evt.type == EventType.MouseDown) { if (UpdateHoverSurfaces(evt.mousePosition, dragArea, selectionType, true)) { repaint = true; } if (!InEditCameraMode) { return(repaint); } } if (InEditCameraMode && !ToolIsDragging) { if (MouseIsDown) { ChiselOutlineRenderer.VisualizationMode = VisualizationMode.None; } else { ChiselOutlineRenderer.VisualizationMode = VisualizationMode.Surface | VisualizationMode.Outline; } } else { ChiselOutlineRenderer.VisualizationMode = VisualizationMode.None; } switch (evt.GetTypeForControl(id)) { case EventType.Layout: { // we only do drag selection when we use a modifier (shift, control etc.) if (selectionType == SelectionType.Replace) { break; } if (hoverSurfaces != null && hoverSurfaces.Count > 0) { HandleUtility.AddControl(id, 3.0f); } break; } case EventType.MouseDown: { if (GUIUtility.hotControl != 0) { break; } // we only do drag selection when we use a modifier (shift, control etc.) if (selectionType == SelectionType.Replace) { break; } if ((UnityEditor.HandleUtility.nearestControl != id || evt.button != 0) && (GUIUtility.keyboardControl != id || evt.button != 2)) { break; } GUIUtility.hotControl = GUIUtility.keyboardControl = id; evt.Use(); break; } case EventType.MouseDrag: { if (GUIUtility.hotControl != id) { break; } UpdateHoverSurfaces(evt.mousePosition, dragArea, selectionType, false); evt.Use(); break; } case EventType.MouseUp: { if (GUIUtility.hotControl != id || evt.button != 0) { break; } GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; evt.Use(); if (ChiselSurfaceSelectionManager.UpdateSelection(selectionType, hoverSurfaces)) { repaint = true; } if (UpdateHoverSurfaces(evt.mousePosition, dragArea, selectionType, true)) { repaint = true; } break; } } return(repaint); }
internal static Vector3 Do(int id, Vector3 position, Vector3 offset, Vector3 handleDirection, Vector3 slideDirection, float size, Handles.CapFunction capFunction, float snap) { Event evt = Event.current; var eventType = evt.GetTypeForControl(id); switch (eventType) { case EventType.Layout: case EventType.MouseMove: if (capFunction != null) { capFunction(id, position + offset, Quaternion.LookRotation(handleDirection), size, eventType); } else { HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(position + offset, size * .2f)); } break; case EventType.MouseDown: // am I closest to the thingy? if (HandleUtility.nearestControl == id && evt.button == 0 && GUIUtility.hotControl == 0 && !evt.alt) { GUIUtility.hotControl = id; // Grab mouse focus s_StartMousePosition = evt.mousePosition; s_ConstraintOrigin = Handles.matrix.MultiplyPoint3x4(position); s_StartPosition = position; s_ConstraintDirection = Handles.matrix.MultiplyVector(slideDirection); s_HandleOffset = HandleUtility.CalcPositionOnConstraint(Camera.current, evt.mousePosition, s_ConstraintOrigin, s_ConstraintDirection, out Vector3 point) ? s_ConstraintOrigin - point : Vector3.zero; evt.Use(); s_StartHandleSize = HandleUtility.GetHandleSize(point); } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { // First try to calculate the translation by casting a mouse ray against a world position plane // oriented towards the camera. This gives more accurate results than doing the line translation // in 2D space, but is more prone towards skewing extreme values when the ray is near parallel // to the plane. To address this, CalcPositionOnConstraint will fail if the mouse ray is close // to parallel (see HandleUtility.k_MinRayConstraintDot) and fall back to 2D based movement. if (HandleUtility.CalcPositionOnConstraint(Camera.current, evt.mousePosition, s_ConstraintOrigin, s_ConstraintDirection, out Vector3 worldPosition)) { var handleOffset = s_HandleOffset * (HandleUtility.GetHandleSize(worldPosition) / s_StartHandleSize); worldPosition += handleOffset; if (EditorSnapSettings.incrementalSnapActive) { Vector3 dir = worldPosition - s_ConstraintOrigin; float dist = Handles.SnapValue(dir.magnitude, snap) * Mathf.Sign(Vector3.Dot(s_ConstraintDirection, dir)); worldPosition = s_ConstraintOrigin + s_ConstraintDirection.normalized * dist; } else if (EditorSnapSettings.gridSnapActive) { worldPosition = Snapping.Snap(worldPosition, GridSettings.size, (SnapAxis) new SnapAxisFilter(s_ConstraintDirection)); } position = Handles.inverseMatrix.MultiplyPoint(worldPosition); s_StartPosition = position; s_StartMousePosition = evt.mousePosition; } else { // Unlike HandleUtility.CalcPositionOnConstraint, CalcLineTranslation _does_ multiply constraint // origin and direction by Handles.matrix, so make sure to pass in unmodified vectors here float dist = HandleUtility.CalcLineTranslation(s_StartMousePosition, evt.mousePosition, s_StartPosition, slideDirection); dist = Handles.SnapValue(dist, snap); worldPosition = Handles.matrix.MultiplyPoint(s_StartPosition) + s_ConstraintDirection * dist; if (EditorSnapSettings.gridSnapActive) { worldPosition = Snapping.Snap(worldPosition, GridSettings.size, (SnapAxis) new SnapAxisFilter(s_ConstraintDirection)); } position = Handles.inverseMatrix.MultiplyPoint(worldPosition); } GUI.changed = true; evt.Use(); } break; case EventType.MouseUp: if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2)) { GUIUtility.hotControl = 0; evt.Use(); } break; case EventType.Repaint: Color temp = Color.white; if (id == GUIUtility.hotControl) { temp = Handles.color; Handles.color = Handles.selectedColor; } else if (id == HandleUtility.nearestControl && GUIUtility.hotControl == 0 && !evt.alt) { temp = Handles.color; Handles.color = Handles.preselectionColor; } capFunction(id, position + offset, Quaternion.LookRotation(handleDirection), size, EventType.Repaint); if (id == GUIUtility.hotControl || id == HandleUtility.nearestControl && GUIUtility.hotControl == 0) { Handles.color = temp; } break; } return(position); }
public static Quaternion Do(int id, Quaternion rotation, Vector3 position, Vector3 axis, float size, bool cutoffPlane, float snap) { if (Mathf.Abs(Vector3.Dot(Camera.current.transform.forward, axis)) > 0.999f) { cutoffPlane = false; } Event current = Event.current; switch (current.GetTypeForControl(id)) { case EventType.MouseDown: if ((HandleUtility.nearestControl == id && current.button == 0) || (GUIUtility.keyboardControl == id && current.button == 2)) { GUIUtility.keyboardControl = id; GUIUtility.hotControl = id; Tools.LockHandlePosition(); if (cutoffPlane) { Vector3 normalized = Vector3.Cross(axis, Camera.current.transform.forward).normalized; Disc.s_StartPosition = HandleUtility.ClosestPointToArc(position, axis, normalized, 180f, size); } else { Disc.s_StartPosition = HandleUtility.ClosestPointToDisc(position, axis, size); } Disc.s_RotationDist = 0f; Disc.s_StartRotation = rotation; Disc.s_StartAxis = axis; Disc.s_CurrentMousePosition = (Disc.s_StartMousePosition = Event.current.mousePosition); current.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } break; case EventType.MouseUp: if (GUIUtility.hotControl == id && (current.button == 0 || current.button == 2)) { Tools.UnlockHandlePosition(); GUIUtility.hotControl = 0; current.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { bool flag = EditorGUI.actionKey && current.shift; if (flag) { if (HandleUtility.ignoreRaySnapObjects == null) { Handles.SetupIgnoreRaySnapObjects(); } object obj = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(current.mousePosition)); if (obj != null && (double)Vector3.Dot(axis.normalized, rotation * Vector3.forward) < 0.999) { Vector3 vector = ((RaycastHit)obj).point - position; Vector3 forward = vector - Vector3.Dot(vector, axis.normalized) * axis.normalized; rotation = Quaternion.LookRotation(forward, rotation * Vector3.up); } } else { Vector3 normalized2 = Vector3.Cross(axis, position - Disc.s_StartPosition).normalized; Disc.s_CurrentMousePosition += current.delta; Disc.s_RotationDist = HandleUtility.CalcLineTranslation(Disc.s_StartMousePosition, Disc.s_CurrentMousePosition, Disc.s_StartPosition, normalized2) / size * 30f; Disc.s_RotationDist = Handles.SnapValue(Disc.s_RotationDist, snap); rotation = Quaternion.AngleAxis(Disc.s_RotationDist * -1f, Disc.s_StartAxis) * Disc.s_StartRotation; } GUI.changed = true; current.Use(); } break; case EventType.KeyDown: if (current.keyCode == KeyCode.Escape && GUIUtility.hotControl == id) { Tools.UnlockHandlePosition(); EditorGUIUtility.SetWantsMouseJumping(0); } break; case EventType.Repaint: { Color color = Color.white; if (id == GUIUtility.keyboardControl) { color = Handles.color; Handles.color = Handles.selectedColor; } if (GUIUtility.hotControl == id) { Color color2 = Handles.color; Vector3 normalized3 = (Disc.s_StartPosition - position).normalized; Handles.color = Handles.secondaryColor; Handles.DrawLine(position, position + normalized3 * size * 1.1f); float angle = Mathf.Repeat(-Disc.s_RotationDist - 180f, 360f) - 180f; Vector3 a = Quaternion.AngleAxis(angle, axis) * normalized3; Handles.DrawLine(position, position + a * size * 1.1f); Handles.color = Handles.secondaryColor * new Color(1f, 1f, 1f, 0.2f); Handles.DrawSolidArc(position, axis, normalized3, angle, size); Handles.color = color2; } if (cutoffPlane) { Vector3 normalized4 = Vector3.Cross(axis, Camera.current.transform.forward).normalized; Handles.DrawWireArc(position, axis, normalized4, 180f, size); } else { Handles.DrawWireDisc(position, axis, size); } if (id == GUIUtility.keyboardControl) { Handles.color = color; } break; } case EventType.Layout: { float distance; if (cutoffPlane) { Vector3 normalized5 = Vector3.Cross(axis, Camera.current.transform.forward).normalized; distance = HandleUtility.DistanceToArc(position, axis, normalized5, 180f, size) / 2f; } else { distance = HandleUtility.DistanceToDisc(position, axis, size) / 2f; } HandleUtility.AddControl(id, distance); break; } } return(rotation); }
public static float DoAxis(int id, float scale, Vector3 position, Vector3 direction, Quaternion rotation, float size, float snap) { Event current = Event.current; switch (current.GetTypeForControl(id)) { case EventType.MouseDown: if (((HandleUtility.nearestControl == id) && (current.button == 0)) || ((GUIUtility.keyboardControl == id) && (current.button == 2))) { int num3 = id; GUIUtility.keyboardControl = num3; GUIUtility.hotControl = num3; s_CurrentMousePosition = s_StartMousePosition = current.mousePosition; s_StartScale = scale; current.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } return(scale); case EventType.MouseUp: if ((GUIUtility.hotControl == id) && ((current.button == 0) || (current.button == 2))) { GUIUtility.hotControl = 0; current.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } return(scale); case EventType.MouseMove: case EventType.KeyDown: case EventType.KeyUp: case EventType.ScrollWheel: return(scale); case EventType.MouseDrag: if (GUIUtility.hotControl == id) { s_CurrentMousePosition += current.delta; float val = 1f + (HandleUtility.CalcLineTranslation(s_StartMousePosition, s_CurrentMousePosition, position, direction) / size); val = Handles.SnapValue(val, snap); scale = s_StartScale * val; GUI.changed = true; current.Use(); } return(scale); case EventType.Repaint: { Color white = Color.white; if (id == GUIUtility.keyboardControl) { white = Handles.color; Handles.color = Handles.selectedColor; } float num2 = size; if (GUIUtility.hotControl == id) { num2 = (size * scale) / s_StartScale; } Handles.CubeCap(id, position + ((Vector3)((direction * num2) * s_ScaleDrawLength)), rotation, size * 0.1f); Handles.DrawLine(position, position + ((Vector3)(direction * ((num2 * s_ScaleDrawLength) - (size * 0.05f))))); if (id == GUIUtility.keyboardControl) { Handles.color = white; } return(scale); } case EventType.Layout: HandleUtility.AddControl(id, HandleUtility.DistanceToLine(position, position + ((Vector3)(direction * size)))); HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(position + ((Vector3)(direction * size)), size * 0.2f)); return(scale); } return(scale); }
private static Vector3 DoDragHandleAxis(DragAxisInfo axisInfo, Vector3 position, ref DragHandleResult result) { // Must request a control ID for each interactible control in the GUI that can respond to events int id = GUIUtility.GetControlID(axisInfo._handleHash, FocusType.Passive); float handleSize = HandleUtility.GetHandleSize(position); Camera camera = Camera.current; Event currentEvent = Event.current; Vector2 mousePos = HEU_EditorUI.GetMousePosition(ref currentEvent, camera); Vector3 handlePosition = Handles.matrix.MultiplyPoint(position); Matrix4x4 cachedHandleMatrix = Handles.matrix; int mouseButtonID = Event.current.button; // Process events (using GetTypeForControl to filter events relevant to this control) switch (currentEvent.GetTypeForControl(id)) { case EventType.MouseDown: { if (HandleUtility.nearestControl == id && (mouseButtonID == 0 || mouseButtonID == 1)) { GUIUtility.hotControl = id; axisInfo._dragMouseCurrent = axisInfo._dragMouseStart = mousePos; axisInfo._dragWorldStart = position; axisInfo._handleHasMoved = false; currentEvent.Use(); EditorGUIUtility.SetWantsMouseJumping(1); if (mouseButtonID == 0) { result = DragHandleResult.LMB_PRESS; } else if (mouseButtonID == 1) { result = DragHandleResult.RMB_PRESS; } } break; } case EventType.MouseUp: { if (GUIUtility.hotControl == id && (mouseButtonID == 0 || mouseButtonID == 1)) { GUIUtility.hotControl = 0; currentEvent.Use(); EditorGUIUtility.SetWantsMouseJumping(0); if (mouseButtonID == 0) { result = DragHandleResult.LMB_RELEASE; } else if (mouseButtonID == 1) { result = DragHandleResult.RMB_RELEASE; } // Double-click if (mousePos == axisInfo._dragMouseStart) { bool doubleClick = (axisInfo._handleClickID == id) && (Time.realtimeSinceStartup - axisInfo._handleClickTime < _handleDoubleClikcInterval); axisInfo._handleClickID = id; axisInfo._handleClickTime = Time.realtimeSinceStartup; if (mouseButtonID == 0) { result = doubleClick ? DragHandleResult.LMB_DOUBLECLICK : DragHandleResult.LMB_CLICK; } else if (mouseButtonID == 1) { result = doubleClick ? DragHandleResult.RMB_DOUBLECLICK : DragHandleResult.RMB_CLICK; } } } break; } case EventType.MouseDrag: { if (GUIUtility.hotControl == id) { if (axisInfo._dragAxis == DragAxis.ALL_AXIS) { // Free movement - (all axis) // Flip y because Unity is inverted axisInfo._dragMouseCurrent += new Vector2(currentEvent.delta.x, -currentEvent.delta.y); Vector3 position2 = camera.WorldToScreenPoint(Handles.matrix.MultiplyPoint(axisInfo._dragWorldStart)) + (Vector3)(axisInfo._dragMouseCurrent - axisInfo._dragMouseStart); position = Handles.matrix.inverse.MultiplyPoint(camera.ScreenToWorldPoint(position2)); } else { // Linear movement (constraint to current axis) axisInfo._dragMouseCurrent += new Vector2(currentEvent.delta.x, currentEvent.delta.y); float mag = HandleUtility.CalcLineTranslation(axisInfo._dragMouseStart, axisInfo._dragMouseCurrent, axisInfo._dragWorldStart, axisInfo._direction); position = axisInfo._dragWorldStart + axisInfo._direction * mag; } if (mouseButtonID == 0) { result = DragHandleResult.LMB_DRAG; } else if (mouseButtonID == 1) { result = DragHandleResult.RMB_DRAG; } axisInfo._handleHasMoved = true; GUI.changed = true; currentEvent.Use(); } break; } case EventType.MouseMove: case EventType.Repaint: { Color handleColor = Handles.color; if ((GUIUtility.hotControl == id && axisInfo._handleHasMoved) || (HandleUtility.nearestControl == id)) { Handles.color = Color.yellow; } else { Handles.color = axisInfo._axisColor; } Handles.matrix = Matrix4x4.identity; if (axisInfo._dragAxis == DragAxis.ALL_AXIS) { HEU_EditorUI.DrawCubeCap(id, handlePosition, Quaternion.identity, handleSize * 0.25f); } else { HEU_EditorUI.DrawArrowCap(id, handlePosition, Quaternion.LookRotation(axisInfo._direction), handleSize); } Handles.matrix = cachedHandleMatrix; Handles.color = handleColor; // This forces a Repaint. We want this when we change the axis color due to being cursor being nearest. if (currentEvent.type == EventType.MouseMove && HandleUtility.nearestControl == id) { SceneView.RepaintAll(); } break; } case EventType.Layout: { // AddControl tells Unity where each Handle is relative to the current mouse position Handles.matrix = Matrix4x4.identity; if (axisInfo._dragAxis == DragAxis.ALL_AXIS) { float distance = handleSize * 0.3f; HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(handlePosition, distance)); } else { HandleUtility.AddControl(id, HandleUtility.DistanceToLine(handlePosition, handlePosition + axisInfo._direction * handleSize) * 0.4f); } Handles.matrix = cachedHandleMatrix; break; } } return(position); }
public static float DoCenter(int id, float value, Vector3 position, Quaternion rotation, float size, Handles.DrawCapFunction capFunc, float snap) { Event current = Event.current; switch (current.GetTypeForControl(id)) { case EventType.MouseDown: if (((HandleUtility.nearestControl == id) && (current.button == 0)) || ((GUIUtility.keyboardControl == id) && (current.button == 2))) { int num = id; GUIUtility.keyboardControl = num; GUIUtility.hotControl = num; s_StartScale = value; s_ValueDrag = 0f; current.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } return(value); case EventType.MouseUp: if ((GUIUtility.hotControl == id) && ((current.button == 0) || (current.button == 2))) { GUIUtility.hotControl = 0; s_ScaleDrawLength = 1f; current.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } return(value); case EventType.MouseMove: case EventType.KeyUp: case EventType.ScrollWheel: return(value); case EventType.MouseDrag: if (GUIUtility.hotControl == id) { s_ValueDrag += HandleUtility.niceMouseDelta * 0.01f; value = (Handles.SnapValue(s_ValueDrag, snap) + 1f) * s_StartScale; s_ScaleDrawLength = value / s_StartScale; GUI.changed = true; current.Use(); } return(value); case EventType.KeyDown: if ((GUIUtility.hotControl == id) && (current.keyCode == KeyCode.Escape)) { value = s_StartScale; s_ScaleDrawLength = 1f; GUIUtility.hotControl = 0; GUI.changed = true; current.Use(); } return(value); case EventType.Repaint: { Color white = Color.white; if (id == GUIUtility.keyboardControl) { white = Handles.color; Handles.color = Handles.selectedColor; } capFunc(id, position, rotation, size * 0.15f); if (id == GUIUtility.keyboardControl) { Handles.color = white; } return(value); } case EventType.Layout: HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(position, size * 0.15f)); return(value); } return(value); }
internal static Vector3 Do(int id, Vector3 position, Vector3 offset, Vector3 handleDirection, Vector3 slideDirection, float size, Handles.CapFunction capFunction, float snap) { Event evt = Event.current; var eventType = evt.GetTypeForControl(id); switch (eventType) { case EventType.Layout: case EventType.MouseMove: if (capFunction != null) { capFunction(id, position + offset, Quaternion.LookRotation(handleDirection), size, eventType); } else { HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(position + offset, size * .2f)); } break; case EventType.MouseDown: // am I closest to the thingy? if (HandleUtility.nearestControl == id && evt.button == 0 && GUIUtility.hotControl == 0 && !evt.alt) { GUIUtility.hotControl = id; // Grab mouse focus s_CurrentMousePosition = s_StartMousePosition = evt.mousePosition; s_StartPosition = position; evt.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { s_CurrentMousePosition += evt.delta; float dist = HandleUtility.CalcLineTranslation(s_StartMousePosition, s_CurrentMousePosition, s_StartPosition, slideDirection); dist = Handles.SnapValue(dist, snap); Vector3 worldDirection = Handles.matrix.MultiplyVector(slideDirection); Vector3 worldPosition = Handles.matrix.MultiplyPoint(s_StartPosition) + worldDirection * dist; if (EditorSnapSettings.gridSnapActive) { worldPosition = Snapping.Snap(worldPosition, GridSettings.size, (SnapAxis) new SnapAxisFilter(worldDirection)); } position = Handles.inverseMatrix.MultiplyPoint(worldPosition); GUI.changed = true; evt.Use(); } break; case EventType.MouseUp: if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2)) { GUIUtility.hotControl = 0; evt.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } break; case EventType.Repaint: Color temp = Color.white; if (id == GUIUtility.hotControl) { temp = Handles.color; Handles.color = Handles.selectedColor; } else if (id == HandleUtility.nearestControl && GUIUtility.hotControl == 0 && !evt.alt) { temp = Handles.color; Handles.color = Handles.preselectionColor; } capFunction(id, position + offset, Quaternion.LookRotation(handleDirection), size, EventType.Repaint); if (id == GUIUtility.hotControl || id == HandleUtility.nearestControl && GUIUtility.hotControl == 0) { Handles.color = temp; } break; } return(position); }
public static Vector3 DragHandle(Vector3 position, float handleSize, Handles.DrawCapFunction capFunc, Color colorSelected, out DragHandleResult result) { int id = GUIUtility.GetControlID(s_DragHandleHash, FocusType.Passive); lastDragHandleID = id; Vector3 screenPosition = Handles.matrix.MultiplyPoint(position); Matrix4x4 cachedMatrix = Handles.matrix; result = DragHandleResult.none; switch (Event.current.GetTypeForControl(id)) { case EventType.MouseDown: if (HandleUtility.nearestControl == id && (Event.current.button == 0 || Event.current.button == 1)) { GUIUtility.hotControl = id; s_DragHandleMouseCurrent = s_DragHandleMouseStart = Event.current.mousePosition; s_DragHandleWorldStart = position; s_DragHandleHasMoved = false; Event.current.Use(); EditorGUIUtility.SetWantsMouseJumping(1); if (Event.current.button == 0) { result = DragHandleResult.LMBPress; } else if (Event.current.button == 1) { result = DragHandleResult.RMBPress; } } break; case EventType.MouseUp: if (GUIUtility.hotControl == id && (Event.current.button == 0 || Event.current.button == 1)) { GUIUtility.hotControl = 0; Event.current.Use(); EditorGUIUtility.SetWantsMouseJumping(0); if (Event.current.button == 0) { result = DragHandleResult.LMBRelease; } else if (Event.current.button == 1) { result = DragHandleResult.RMBRelease; } if (Event.current.mousePosition == s_DragHandleMouseStart) { bool doubleClick = (s_DragHandleClickID == id) && (Time.realtimeSinceStartup - s_DragHandleClickTime < s_DragHandleDoubleClickInterval); s_DragHandleClickID = id; s_DragHandleClickTime = Time.realtimeSinceStartup; if (Event.current.button == 0) { result = doubleClick ? DragHandleResult.LMBDoubleClick : DragHandleResult.LMBClick; } else if (Event.current.button == 1) { result = doubleClick ? DragHandleResult.RMBDoubleClick : DragHandleResult.RMBClick; } } } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { s_DragHandleMouseCurrent += new Vector2(Event.current.delta.x, -Event.current.delta.y); Vector3 position2 = Camera.current.WorldToScreenPoint(Handles.matrix.MultiplyPoint(s_DragHandleWorldStart)) + (Vector3)(s_DragHandleMouseCurrent - s_DragHandleMouseStart); position = Handles.matrix.inverse.MultiplyPoint(Camera.current.ScreenToWorldPoint(position2)); if (Camera.current.transform.forward == Vector3.forward || Camera.current.transform.forward == -Vector3.forward) { position.z = s_DragHandleWorldStart.z; } if (Camera.current.transform.forward == Vector3.up || Camera.current.transform.forward == -Vector3.up) { position.y = s_DragHandleWorldStart.y; } if (Camera.current.transform.forward == Vector3.right || Camera.current.transform.forward == -Vector3.right) { position.x = s_DragHandleWorldStart.x; } if (Event.current.button == 0) { result = DragHandleResult.LMBDrag; } else if (Event.current.button == 1) { result = DragHandleResult.RMBDrag; } s_DragHandleHasMoved = true; GUI.changed = true; Event.current.Use(); } break; case EventType.Repaint: Color currentColour = Handles.color; if (id == GUIUtility.hotControl && s_DragHandleHasMoved) { Handles.color = colorSelected; } Handles.matrix = Matrix4x4.identity; capFunc(id, screenPosition, Quaternion.identity, handleSize); Handles.matrix = cachedMatrix; Handles.color = currentColour; break; case EventType.Layout: Handles.matrix = Matrix4x4.identity; HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(screenPosition, handleSize)); Handles.matrix = cachedMatrix; break; } return(position); }
#pragma warning disable 618 internal static Vector3 Do(int id, Vector3 position, Vector3 handleDirection, Vector3 slideDirection, float size, Handles.DrawCapFunction drawFunc, float snap) #pragma warning disable 618 { Event evt = Event.current; switch (evt.GetTypeForControl(id)) { case EventType.Layout: case EventType.MouseMove: // This is an ugly hack. It would be better if the drawFunc can handle it's own layout. if (drawFunc == Handles.ArrowCap) { HandleUtility.AddControl(id, HandleUtility.DistanceToLine(position, position + slideDirection * size)); HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(position + slideDirection * size, size * .2f)); } else { HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(position, size * .2f)); } break; case EventType.MouseDown: // am I closest to the thingy? if ((HandleUtility.nearestControl == id && evt.button == 0) && GUIUtility.hotControl == 0 && !evt.alt) { GUIUtility.hotControl = id; // Grab mouse focus s_CurrentMousePosition = s_StartMousePosition = evt.mousePosition; s_StartPosition = position; evt.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { s_CurrentMousePosition += evt.delta; float dist = HandleUtility.CalcLineTranslation(s_StartMousePosition, s_CurrentMousePosition, s_StartPosition, slideDirection); dist = Handles.SnapValue(dist, snap); Vector3 worldDirection = Handles.matrix.MultiplyVector(slideDirection); Vector3 worldPosition = Handles.matrix.MultiplyPoint(s_StartPosition) + worldDirection * dist; position = Handles.inverseMatrix.MultiplyPoint(worldPosition); GUI.changed = true; evt.Use(); } break; case EventType.MouseUp: if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2)) { GUIUtility.hotControl = 0; evt.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } break; case EventType.Repaint: Color temp = Color.white; if (id == GUIUtility.hotControl) { temp = Handles.color; Handles.color = Handles.selectedColor; } else if (id == HandleUtility.nearestControl && GUIUtility.hotControl == 0 && !evt.alt) { temp = Handles.color; Handles.color = Handles.preselectionColor; } drawFunc(id, position, Quaternion.LookRotation(handleDirection), size); if (id == GUIUtility.hotControl || id == HandleUtility.nearestControl && GUIUtility.hotControl == 0) { Handles.color = temp; } break; } return(position); }
// Returns the distance the new position has moved along slideDir1 and slideDir2 private static Vector2 CalcDeltaAlongDirections( int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.CapFunction capFunction, Vector2 snap, bool drawHelper) { Vector3 position = handlePos + offset; Quaternion rotation = Quaternion.LookRotation(handleDir, slideDir1); Vector2 deltaDistanceAlongDirections = new Vector2(0, 0); Event evt = Event.current; switch (evt.GetTypeForControl(id)) { case EventType.Layout: if (capFunction != null) { capFunction(id, position, rotation, handleSize, EventType.Layout); } else { HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(handlePos + offset, handleSize * .5f)); } break; case EventType.MouseDown: // am I closest to the thingy? if (HandleUtility.nearestControl == id && evt.button == 0 && GUIUtility.hotControl == 0) { s_CurrentMousePosition = evt.mousePosition; bool success = true; Vector3 localMousePoint = Handles.inverseMatrix.MultiplyPoint(GetMousePosition(handleDir, handlePos, ref success)); if (success) { GUIUtility.hotControl = id; // Grab mouse focus s_StartPosition = handlePos; Vector3 clickOffset = localMousePoint - handlePos; s_StartPlaneOffset.x = Vector3.Dot(clickOffset, slideDir1); s_StartPlaneOffset.y = Vector3.Dot(clickOffset, slideDir2); evt.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { s_CurrentMousePosition += evt.delta; bool success = true; Vector3 localMousePoint = Handles.inverseMatrix.MultiplyPoint(GetMousePosition(handleDir, handlePos, ref success)); if (success) { // Determine hitpos projection onto slideDirs deltaDistanceAlongDirections.x = HandleUtility.PointOnLineParameter(localMousePoint, s_StartPosition, slideDir1); deltaDistanceAlongDirections.y = HandleUtility.PointOnLineParameter(localMousePoint, s_StartPosition, slideDir2); deltaDistanceAlongDirections -= s_StartPlaneOffset; if (snap.x > 0 || snap.y > 0) { deltaDistanceAlongDirections.x = Handles.SnapValue(deltaDistanceAlongDirections.x, snap.x); deltaDistanceAlongDirections.y = Handles.SnapValue(deltaDistanceAlongDirections.y, snap.y); } GUI.changed = true; } evt.Use(); } break; case EventType.MouseUp: if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2)) { GUIUtility.hotControl = 0; evt.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } break; case EventType.MouseMove: if (id == HandleUtility.nearestControl) { HandleUtility.Repaint(); } break; case EventType.Repaint: { if (capFunction == null) { break; } Color temp = Color.white; if (id == GUIUtility.hotControl) { temp = Handles.color; Handles.color = Handles.selectedColor; } else if (id == HandleUtility.nearestControl && GUIUtility.hotControl == 0) { temp = Handles.color; Handles.color = Handles.preselectionColor; } capFunction(id, position, rotation, handleSize, EventType.Repaint); if (id == GUIUtility.hotControl || id == HandleUtility.nearestControl && GUIUtility.hotControl == 0) { Handles.color = temp; } // Draw a helper rectangle to show what plane we are dragging in if (drawHelper && GUIUtility.hotControl == id) { Vector3[] verts = new Vector3[4]; float helperSize = handleSize * 10.0f; verts[0] = position + (slideDir1 * helperSize + slideDir2 * helperSize); verts[1] = verts[0] - slideDir1 * helperSize * 2.0f; verts[2] = verts[1] - slideDir2 * helperSize * 2.0f; verts[3] = verts[2] + slideDir1 * helperSize * 2.0f; Color prevColor = Handles.color; Handles.color = Color.white; float outline = 0.6f; Handles.DrawSolidRectangleWithOutline(verts, new Color(1, 1, 1, 0.05f), new Color(outline, outline, outline, 0.4f)); Handles.color = prevColor; } } break; } return(deltaDistanceAlongDirections); }
internal static Quaternion Do(int id, Quaternion rotation, Vector3 position, float size, bool drawCircle) { Vector3 vector = Handles.matrix.MultiplyPoint(position); Matrix4x4 matrix = Handles.matrix; Event current = Event.current; switch (current.GetTypeForControl(id)) { case EventType.MouseDown: if (HandleUtility.nearestControl == id && current.button == 0) { GUIUtility.hotControl = id; Tools.LockHandlePosition(); FreeRotate.s_CurrentMousePosition = current.mousePosition; HandleUtility.ignoreRaySnapObjects = null; current.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } break; case EventType.MouseUp: if (GUIUtility.hotControl == id && (current.button == 0 || current.button == 2)) { Tools.UnlockHandlePosition(); GUIUtility.hotControl = 0; current.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } break; case EventType.MouseMove: if (id == HandleUtility.nearestControl) { HandleUtility.Repaint(); } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { bool flag = EditorGUI.actionKey && current.shift; if (flag) { if (HandleUtility.ignoreRaySnapObjects == null) { Handles.SetupIgnoreRaySnapObjects(); } object obj = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(current.mousePosition)); if (obj != null) { Quaternion quaternion = Quaternion.LookRotation(((RaycastHit)obj).point - position); if (Tools.pivotRotation == PivotRotation.Global) { Transform activeTransform = Selection.activeTransform; if (activeTransform) { Quaternion rhs = Quaternion.Inverse(activeTransform.rotation) * rotation; quaternion *= rhs; } } rotation = quaternion; } } else { FreeRotate.s_CurrentMousePosition += current.delta; Vector3 vector2 = Camera.current.transform.TransformDirection(new Vector3(-current.delta.y, -current.delta.x, 0f)); rotation = Quaternion.AngleAxis(current.delta.magnitude, vector2.normalized) * rotation; } GUI.changed = true; current.Use(); } break; case EventType.KeyDown: if (current.keyCode == KeyCode.Escape && GUIUtility.hotControl == id) { Tools.UnlockHandlePosition(); EditorGUIUtility.SetWantsMouseJumping(0); } break; case EventType.Repaint: { Color color = Color.white; bool flag2 = id == GUIUtility.hotControl; bool flag3 = id == HandleUtility.nearestControl && GUIUtility.hotControl == 0; if (flag2) { color = Handles.color; Handles.color = Handles.selectedColor; } else if (flag3) { color = Handles.color; Handles.color = Handles.preselectionColor; } Handles.matrix = Matrix4x4.identity; if (drawCircle) { Handles.DrawWireDisc(vector, Camera.current.transform.forward, size); } if (flag3 || flag2) { Handles.color = FreeRotate.s_DimmingColor; Handles.DrawSolidDisc(vector, Camera.current.transform.forward, size); } Handles.matrix = matrix; if (flag2 || flag3) { Handles.color = color; } break; } case EventType.Layout: Handles.matrix = Matrix4x4.identity; HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(vector, size) + 5f); Handles.matrix = matrix; break; } return(rotation); }
static bool DoOrientationHandle(FaceData face, ProBuilderShape proBuilderShape) { Event evt = Event.current; bool hasRotated = false; switch (evt.type) { case EventType.MouseDown: if (k_OrientationControlIDs.Contains(HandleUtility.nearestControl) && evt.button == 0) { s_CurrentId = HandleUtility.nearestControl; GUIUtility.hotControl = s_CurrentId; evt.Use(); } break; case EventType.MouseUp: if (k_OrientationControlIDs.Contains(HandleUtility.nearestControl) && evt.button == 0) { GUIUtility.hotControl = 0; evt.Use(); if (s_CurrentId == HandleUtility.nearestControl) { //Execute rotation Vector3 targetedNormal = Vector3.zero; for (int i = 0; i < k_OrientationControlIDs.Length; i++) { if (k_OrientationControlIDs[i] == s_CurrentId) { targetedNormal = (s_ArrowsLines[i][1] - face.CenterPosition).normalized; break; } } var currentNormal = face.Normal; currentNormal.Scale(Math.Sign(proBuilderShape.size)); targetedNormal.Scale(Math.Sign(proBuilderShape.size)); Vector3 rotationAxis = Vector3.Cross(currentNormal, targetedNormal); var angle = Vector3.SignedAngle(currentNormal, targetedNormal, rotationAxis); s_ShapeRotation = Quaternion.AngleAxis(angle, rotationAxis); s_CurrentAngle = (s_CurrentAngle + angle) % 360; hasRotated = true; } s_CurrentId = -1; } break; case EventType.Layout: for (int i = 0; i < 4; i++) { var rectPos = 0.8f * s_ArrowsLines[i][1] + 0.2f * face.CenterPosition; float dist = HandleUtility.DistanceToRectangle(rectPos, Quaternion.LookRotation(face.Normal), HandleUtility.GetHandleSize(face.CenterPosition) * s_DefaultMidpointSquareSize / 2f); HandleUtility.AddControl(k_OrientationControlIDs[i], dist); } break; case EventType.Repaint: if (s_CurrentArrowHovered != HandleUtility.nearestControl) { s_CurrentAngle = 0f; } int pointsCount = face.Points.Length; s_CurrentArrowHovered = -1; for (int i = 0; i < pointsCount; i++) { var rectHandleSize = HandleUtility.GetHandleSize(face.CenterPosition) * s_DefaultMidpointSquareSize; var sideDirection = (face.Points[(i + 1) % pointsCount] - face.Points[i]).normalized; var arrowDirection = Vector3.Cross(face.Normal.normalized, sideDirection).normalized; var topDirection = 2.5f * rectHandleSize * arrowDirection; var top = face.CenterPosition + topDirection; var A = topDirection.magnitude; var a = 0.33f * Mathf.Sqrt(2f * A * A); var h = 0.5f * Mathf.Sqrt(2f * a * a); s_ArrowsLines[i][0] = top - (h * arrowDirection + h * sideDirection); s_ArrowsLines[i][1] = top; s_ArrowsLines[i][2] = top - (h * arrowDirection - h * sideDirection); bool selected = HandleUtility.nearestControl == k_OrientationControlIDs[i]; Color color = selected ? EditorHandleDrawing.edgeSelectedColor : k_BoundsHandleColor; color.a = 1.0f; using (new Handles.DrawingScope(color)) { Handles.DrawAAPolyLine(5f, s_ArrowsLines[i]); if (selected) { EditorGUIUtility.AddCursorRect(new Rect(0, 0, Screen.width, Screen.height), MouseCursor.RotateArrow); s_CurrentArrowHovered = HandleUtility.nearestControl; Handles.DrawAAPolyLine(3f, new Vector3[] { Vector3.Scale(proBuilderShape.rotation * Vector3.up, proBuilderShape.size / 2f), Vector3.zero, Vector3.Scale(proBuilderShape.rotation * Vector3.forward, proBuilderShape.size / 2f) }); } } } break; case EventType.MouseDrag: if (k_OrientationControlIDs.Contains(s_CurrentId) && HandleUtility.nearestControl != s_CurrentId) { GUIUtility.hotControl = 0; s_CurrentId = -1; } break; } return(hasRotated); }
private static Vector2 CalcDeltaAlongDirections(int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, CSGHandles.CapFunction capFunction, bool snapping, Vector3[] snapVertices, CSGHandles.InitFunction initFunction, CSGHandles.InitFunction shutdownFunction) { var position = handlePos + offset; var rotation = Quaternion.LookRotation(handleDir, slideDir1); var deltaDistanceAlongDirections = new Vector2(0, 0); var evt = Event.current; switch (evt.GetTypeForControl(id)) { case EventType.Layout: { if (capFunction != null) { capFunction(id, position, rotation, handleSize, EventType.Layout); } else { HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(handlePos + offset, handleSize * .5f)); } break; } case EventType.MouseDown: { if (CSGHandles.disabled) { break; } if (((HandleUtility.nearestControl == id && evt.button == 0) || (GUIUtility.keyboardControl == id && evt.button == 2)) && GUIUtility.hotControl == 0) { s_SnapVertices = null; if (initFunction != null) { initFunction(); } var plane = new Plane(Handles.matrix.MultiplyVector(handleDir), Handles.matrix.MultiplyPoint(handlePos)); var mouseRay = HandleUtility.GUIPointToWorldRay(evt.mousePosition); var dist = 0.0f; plane.Raycast(mouseRay, out dist); GUIUtility.hotControl = GUIUtility.keyboardControl = id; s_CurrentMousePosition = evt.mousePosition; s_StartPosition = handlePos; var localMousePoint = Handles.inverseMatrix.MultiplyPoint(mouseRay.GetPoint(dist)); var clickOffset = localMousePoint - handlePos; s_StartPlaneOffset.x = Vector3.Dot(clickOffset, slideDir1); s_StartPlaneOffset.y = Vector3.Dot(clickOffset, slideDir2); evt.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } break; } case EventType.MouseDrag: { if (GUIUtility.hotControl == id) { s_CurrentMousePosition += evt.delta; var worldPosition = Handles.matrix.MultiplyPoint(handlePos); var worldSlideDir1 = Handles.matrix.MultiplyVector(slideDir1).normalized; var worldSlideDir2 = Handles.matrix.MultiplyVector(slideDir2).normalized; var mouseRay = HandleUtility.GUIPointToWorldRay(s_CurrentMousePosition); var plane = new Plane(worldPosition, worldPosition + worldSlideDir1, worldPosition + worldSlideDir2); var dist = 0.0f; if (plane.Raycast(mouseRay, out dist)) { var hitpos = Handles.inverseMatrix.MultiplyPoint(mouseRay.GetPoint(dist)); deltaDistanceAlongDirections.x = HandleUtility.PointOnLineParameter(hitpos, s_StartPosition, slideDir1); deltaDistanceAlongDirections.y = HandleUtility.PointOnLineParameter(hitpos, s_StartPosition, slideDir2); deltaDistanceAlongDirections -= s_StartPlaneOffset; GUI.changed = true; } evt.Use(); } break; } case EventType.MouseUp: { if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2)) { GUIUtility.hotControl = 0; evt.Use(); if (shutdownFunction != null) { shutdownFunction(); } EditorGUIUtility.SetWantsMouseJumping(0); } break; } case EventType.Repaint: { if (capFunction == null) { break; } var originalColor = Handles.color; if (id == GUIUtility.keyboardControl) { Handles.color = Handles.selectedColor; } else if (CSGHandles.disabled) { Handles.color = Color.Lerp(originalColor, Handles.secondaryColor, 0.75f); } capFunction(id, position, rotation, handleSize, EventType.Repaint); Handles.color = originalColor; break; } } return(deltaDistanceAlongDirections); }
static Rect ResizeHandlesGUI(Rect rect, ref Vector3 position, Quaternion rotation, bool anchorPivot = false) { if (Event.current.type == EventType.MouseDown) { s_StartRect = rect; s_CurrentRect = rect; s_StartPosition = position; s_StartPivot = GetNormalizedPivot(rect); } Vector3 scale = Vector3.one; Quaternion inverseRotation = Quaternion.Inverse(rotation); for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { if (i != 1 || j != 1) { Vector3 startWorldPoint = GetRectPointInWorld(s_StartRect, s_StartPosition, rotation, i, j); Vector3 currentWorldPoint = GetRectPointInWorld(s_CurrentRect, s_StartPosition, rotation, i, j); Vector3 rectWorldPoint = GetRectPointInWorld(rect, position, rotation, i, j); int controlID = GUIUtility.GetControlID("RectResizeHandles".GetHashCode(), FocusType.Passive); EventType eventType = Event.current.GetTypeForControl(controlID); if (GUI.color.a > 0f || GUIUtility.hotControl == controlID) { EditorGUI.BeginChangeCheck(); Vector3 newPosition = Vector3.zero; MouseCursor cursor = MouseCursor.Arrow; if (i == 1 || j == 1) { Vector3 sideVector = (i != 1) ? (rotation * Vector3.up * rect.height) : (rotation * Vector3.right * rect.width); Vector3 direction = (i != 1) ? (rotation * Vector3.right) : (rotation * Vector3.up); newPosition = SideSlider(controlID, currentWorldPoint, sideVector, direction, null); if (!Event.current.alt && eventType == EventType.Layout) { Vector3 normalized2 = sideVector.normalized; Vector3 p1 = rectWorldPoint + sideVector * 0.5f; Vector3 p2 = rectWorldPoint - sideVector * 0.5f; Vector3 offset = -normalized2 *HandleUtility.GetHandleSize(p1) / 20f; Vector3 offset2 = normalized2 * HandleUtility.GetHandleSize(p2) / 20f; HandleUtility.AddControl(controlID, HandleUtility.DistanceToLine(p1 + offset, p2 + offset2)); } cursor = GetScaleCursor(direction); } else { HandlesExtra.DotCap(controlID, rectWorldPoint, Quaternion.identity, 1f, eventType); newPosition = HandlesExtra.Slider2D(controlID, currentWorldPoint, null); if (!Event.current.alt && eventType == EventType.Layout) { HandleUtility.AddControl(controlID, HandleUtility.DistanceToCircle(rectWorldPoint, HandleUtility.GetHandleSize(rectWorldPoint) / 20f)); } Vector3 outwardsDir = rotation * Vector3.right * (float)(i - 1); Vector3 outwardsDir2 = rotation * Vector3.up * (float)(j - 1); cursor = GetScaleCursor(outwardsDir + outwardsDir2); } if (eventType == EventType.Repaint) { if ((HandleUtility.nearestControl == controlID && GUIUtility.hotControl == 0) || GUIUtility.hotControl == controlID) { Rect cursorRect = new Rect(0, 0, 20f, 20f); cursorRect.center = Event.current.mousePosition; EditorGUIUtility.AddCursorRect(cursorRect, cursor, controlID); } } if (EditorGUI.EndChangeCheck()) { Vector3 scalePivot = Vector3.zero; Vector2 scalePivotLocal = Vector2.one; bool alt = Event.current.alt; bool actionKey = EditorGUI.actionKey; bool shiftDown = Event.current.shift && !actionKey; if (!alt) { scalePivot = GetRectPointInWorld(s_StartRect, s_StartPosition, rotation, 2 - i, 2 - j); scalePivotLocal = inverseRotation * (scalePivot - s_StartPosition); } Vector3 localRectPoint = inverseRotation * (startWorldPoint - scalePivot); Vector3 localNewPosition = inverseRotation * (newPosition - scalePivot); if (i != 1) { scale.x = localNewPosition.x / localRectPoint.x; } if (j != 1) { scale.y = localNewPosition.y / localRectPoint.y; } if (shiftDown) { float d = (i != 1) ? scale.x : scale.y; scale = Vector3.one * d; } if (actionKey && i == 1) { if (Event.current.shift) { scale.x = (scale.z = 1f / Mathf.Sqrt(Mathf.Max(scale.y, 0.0001f))); } else { scale.x = 1f / Mathf.Max(scale.y, 0.0001f); } } if (shiftDown) { float d2 = (i != 1) ? scale.x : scale.y; scale = Vector3.one * d2; } if (actionKey && i == 1) { if (Event.current.shift) { scale.x = (scale.z = 1f / Mathf.Sqrt(Mathf.Max(scale.y, 0.0001f))); } else { scale.x = 1f / Mathf.Max(scale.y, 0.0001f); } } if (actionKey && j == 1) { if (Event.current.shift) { scale.y = (scale.z = 1f / Mathf.Sqrt(Mathf.Max(scale.x, 0.0001f))); } else { scale.y = 1f / Mathf.Max(scale.x, 0.0001f); } } s_CurrentRect.min = Vector2.Scale(scale, s_StartRect.min - scalePivotLocal) + scalePivotLocal; s_CurrentRect.max = Vector2.Scale(scale, s_StartRect.max - scalePivotLocal) + scalePivotLocal; if (anchorPivot) { rect.min = s_CurrentRect.min; rect.max = s_CurrentRect.max; } else { rect.position = Vector2.Scale(scale, s_StartRect.position); rect.size = Vector2.Scale(scale, s_StartRect.size); Vector2 newPivot = new Vector2(s_CurrentRect.xMin + (s_CurrentRect.xMax - s_CurrentRect.xMin) * s_StartPivot.x, s_CurrentRect.yMin + (s_CurrentRect.yMax - s_CurrentRect.yMin) * s_StartPivot.y); position = s_StartPosition + rotation * newPivot; } } } } } } return(rect); }
public static void OnSceneGUI(SceneView sceneview) { for (int i = 0; i < s_Bones.Count; i++) { Bone2D bone = s_Bones[i]; if (bone && IsVisible(bone)) { int controlID = GUIUtility.GetControlID("BoneHandle".GetHashCode(), FocusType.Passive); EventType eventType = Event.current.GetTypeForControl(controlID); if (!IsLocked(bone)) { if (eventType == EventType.MouseDown) { if (HandleUtility.nearestControl == controlID && Event.current.button == 0) { GUIUtility.hotControl = controlID; Event.current.Use(); } } if (eventType == EventType.MouseUp) { if (GUIUtility.hotControl == controlID && Event.current.button == 0) { if (EditorGUI.actionKey) { List <Object> objects = new List <Object>(Selection.objects); objects.Add(bone.gameObject); Selection.objects = objects.ToArray(); } else { Selection.activeObject = bone; } GUIUtility.hotControl = 0; Event.current.Use(); } } if (eventType == EventType.MouseDrag) { if (GUIUtility.hotControl == controlID && Event.current.button == 0) { Handles.matrix = bone.transform.localToWorldMatrix; Vector3 position = HandlesExtra.GUIToWorld(Event.current.mousePosition); BoneUtils.OrientToLocalPosition(bone, position, Event.current.shift, "Rotate", true); EditorUpdater.SetDirty("Rotate"); GUI.changed = true; Event.current.Use(); } } } if (eventType == EventType.Repaint) { if ((HandleUtility.nearestControl == controlID && GUIUtility.hotControl == 0) || GUIUtility.hotControl == controlID || Selection.gameObjects.Contains(bone.gameObject)) { Color color = Color.yellow; float outlineSize = HandleUtility.GetHandleSize(bone.transform.position) * 0.015f * bone.color.a; BoneUtils.DrawBoneOutline(bone, outlineSize, color); Bone2D outlineBone = bone.child; color.a *= 0.5f; while (outlineBone) { if (Selection.gameObjects.Contains(outlineBone.gameObject)) { outlineBone = null; } else { if (outlineBone.color.a == 0f) { outlineSize = HandleUtility.GetHandleSize(outlineBone.transform.position) * 0.015f * outlineBone.color.a; BoneUtils.DrawBoneOutline(outlineBone, outlineSize, color); outlineBone = outlineBone.child; color.a *= 0.5f; } else { outlineBone = null; } } } } if (bone.parentBone && !bone.linkedParentBone) { Color color = bone.color; color.a *= 0.25f; Handles.matrix = Matrix4x4.identity; BoneUtils.DrawBoneBody(bone.transform.position, bone.parentBone.transform.position, BoneUtils.GetBoneRadius(bone), color); } BoneUtils.DrawBoneBody(bone); Color innerColor = bone.color * 0.25f; if (bone.attachedIK && bone.attachedIK.isActiveAndEnabled) { innerColor = new Color(0f, 0.75f, 0.75f, 1f); } innerColor.a = bone.color.a; BoneUtils.DrawBoneCap(bone, innerColor); } if (!IsLocked(bone) && eventType == EventType.Layout) { HandleUtility.AddControl(controlID, HandleUtility.DistanceToLine(bone.transform.position, bone.endPosition)); } } } foreach (Control control in s_Controls) { if (control && control.isActiveAndEnabled && IsVisible(control.gameObject)) { Transform transform = control.transform; if (Selection.activeTransform != transform) { if (!control.bone || (control.bone && !Selection.transforms.Contains(control.bone.transform))) { Handles.matrix = Matrix4x4.identity; Handles.color = control.color; if (Tools.current == Tool.Move) { EditorGUI.BeginChangeCheck(); Quaternion cameraRotation = Camera.current.transform.rotation; if (Event.current.type == EventType.Repaint) { Camera.current.transform.rotation = transform.rotation; } float size = HandleUtility.GetHandleSize(transform.position) / 5f; //Handles.DrawLine(transform.position + transform.rotation * new Vector3(size,0f,0f), transform.position + transform.rotation * new Vector3(-size,0f,0f)); //Handles.DrawLine(transform.position + transform.rotation * new Vector3(0f,size,0f), transform.position + transform.rotation * new Vector3(0f,-size,0f)); bool guiEnabled = GUI.enabled; GUI.enabled = !IsLocked(control.gameObject); #if UNITY_5_6_OR_NEWER Vector3 newPosition = Handles.FreeMoveHandle(transform.position, transform.rotation, size, Vector3.zero, Handles.RectangleHandleCap); #else Vector3 newPosition = Handles.FreeMoveHandle(transform.position, transform.rotation, size, Vector3.zero, Handles.RectangleCap); #endif GUI.enabled = guiEnabled; if (Event.current.type == EventType.Repaint) { Camera.current.transform.rotation = cameraRotation; } if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(transform, "Move"); transform.position = newPosition; if (control.bone) { Undo.RecordObject(control.bone.transform, "Move"); control.bone.transform.position = newPosition; BoneUtils.OrientToChild(control.bone.parentBone, Event.current.shift, "Move", true); EditorUpdater.SetDirty("Move"); } } } else if (Tools.current == Tool.Rotate) { EditorGUI.BeginChangeCheck(); float size = HandleUtility.GetHandleSize(transform.position) * 0.5f; //Handles.DrawLine(transform.position + transform.rotation * new Vector3(size,0f,0f), transform.position + transform.rotation * new Vector3(-size,0f,0f)); //Handles.DrawLine(transform.position + transform.rotation * new Vector3(0f,size,0f), transform.position + transform.rotation * new Vector3(0f,-size,0f)); bool guiEnabled = GUI.enabled; GUI.enabled = !IsLocked(control.gameObject); Quaternion newRotation = Handles.Disc(transform.rotation, transform.position, transform.forward, size, false, 0f); GUI.enabled = guiEnabled; if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(transform, "Rotate"); transform.rotation = newRotation; if (control.bone) { Undo.RecordObject(control.bone.transform, "Rotate"); control.bone.transform.rotation = newRotation; EditorUpdater.SetDirty("Rotate"); } } } } } } } }
#pragma warning disable 618 public static Vector3 Do(int id, Vector3 position, Quaternion rotation, float size, Vector3 snap, Handles.DrawCapFunction capFunc) #pragma warning restore 618 { Vector3 worldPosition = Handles.matrix.MultiplyPoint(position); Matrix4x4 origMatrix = Handles.matrix; VertexSnapping.HandleMouseMove(id); Event evt = Event.current; switch (evt.GetTypeForControl(id)) { case EventType.Layout: // We only want the position to be affected by the Handles.matrix. Handles.matrix = Matrix4x4.identity; HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(worldPosition, size * 1.2f)); Handles.matrix = origMatrix; break; case EventType.MouseDown: // am I closest to the thingy? if (HandleUtility.nearestControl == id && evt.button == 0) { GUIUtility.hotControl = id; // Grab mouse focus s_CurrentMousePosition = s_StartMousePosition = evt.mousePosition; s_StartPosition = position; HandleUtility.ignoreRaySnapObjects = null; evt.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { bool rayDrag = EditorGUI.actionKey && evt.shift; if (rayDrag) { if (HandleUtility.ignoreRaySnapObjects == null) { Handles.SetupIgnoreRaySnapObjects(); } object hit = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(evt.mousePosition)); if (hit != null) { RaycastHit rh = (RaycastHit)hit; float offset = 0; if (Tools.pivotMode == PivotMode.Center) { float geomOffset = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, rh.normal); if (geomOffset != Mathf.Infinity) { offset = Vector3.Dot(position, rh.normal) - geomOffset; } } position = Handles.inverseMatrix.MultiplyPoint(rh.point + (rh.normal * offset)); } else { rayDrag = false; } } if (!rayDrag) { // normal drag s_CurrentMousePosition += new Vector2(evt.delta.x, -evt.delta.y) * EditorGUIUtility.pixelsPerPoint; Vector3 screenPos = Camera.current.WorldToScreenPoint(Handles.matrix.MultiplyPoint(s_StartPosition)); screenPos += (Vector3)(s_CurrentMousePosition - s_StartMousePosition); position = Handles.inverseMatrix.MultiplyPoint(Camera.current.ScreenToWorldPoint(screenPos)); // Due to floating point inaccuracies, the back-and-forth transformations used may sometimes introduce // tiny unintended movement in wrong directions. People notice when using a straight top/left/right ortho camera. // In that case, just restrain the movement to the plane. if (Camera.current.transform.forward == Vector3.forward || Camera.current.transform.forward == -Vector3.forward) { position.z = s_StartPosition.z; } if (Camera.current.transform.forward == Vector3.up || Camera.current.transform.forward == -Vector3.up) { position.y = s_StartPosition.y; } if (Camera.current.transform.forward == Vector3.right || Camera.current.transform.forward == -Vector3.right) { position.x = s_StartPosition.x; } if (Tools.vertexDragging) { if (HandleUtility.ignoreRaySnapObjects == null) { Handles.SetupIgnoreRaySnapObjects(); } Vector3 near; if (HandleUtility.FindNearestVertex(evt.mousePosition, null, out near)) { position = Handles.inverseMatrix.MultiplyPoint(near); } } if (EditorGUI.actionKey && !evt.shift) { Vector3 delta = position - s_StartPosition; delta.x = Handles.SnapValue(delta.x, snap.x); delta.y = Handles.SnapValue(delta.y, snap.y); delta.z = Handles.SnapValue(delta.z, snap.z); position = s_StartPosition + delta; } } GUI.changed = true; evt.Use(); } break; case EventType.MouseUp: if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2)) { GUIUtility.hotControl = 0; HandleUtility.ignoreRaySnapObjects = null; evt.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } break; case EventType.MouseMove: if (id == HandleUtility.nearestControl) { HandleUtility.Repaint(); } break; case EventType.Repaint: Color temp = Color.white; if (id == GUIUtility.hotControl) { temp = Handles.color; Handles.color = Handles.selectedColor; } else if (id == HandleUtility.nearestControl && GUIUtility.hotControl == 0) { temp = Handles.color; Handles.color = Handles.preselectionColor; } // We only want the position to be affected by the Handles.matrix. Handles.matrix = Matrix4x4.identity; capFunc(id, worldPosition, Camera.current.transform.rotation, size); Handles.matrix = origMatrix; if (id == GUIUtility.hotControl || id == HandleUtility.nearestControl && GUIUtility.hotControl == 0) { Handles.color = temp; } break; } return(position); }
public static Quaternion Do(Camera camera, int id, Quaternion rotation, Vector3 position, float size, bool snapping, CSGHandles.InitFunction initFunction, CSGHandles.InitFunction shutdownFunction) { var worldPosition = Handles.matrix.MultiplyPoint(position); var origMatrix = Handles.matrix; var evt = Event.current; switch (evt.GetTypeForControl(id)) { case EventType.Layout: { Handles.matrix = Matrix4x4.identity; HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(worldPosition, size) + kPickDistance); Handles.matrix = origMatrix; break; } case EventType.MouseDown: { if (CSGHandles.disabled) { break; } if (((HandleUtility.nearestControl == id && evt.button == 0) || (GUIUtility.keyboardControl == id && evt.button == 2)) && GUIUtility.hotControl == 0) { if (initFunction != null) { initFunction(); } GUIUtility.hotControl = GUIUtility.keyboardControl = id; // Grab mouse focus //Tools.LockHandlePosition(); s_CurrentMousePosition = evt.mousePosition; evt.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } break; } case EventType.MouseDrag: { if (GUIUtility.hotControl == id) { s_CurrentMousePosition += evt.delta; var rotDir = camera.transform.TransformDirection(new Vector3(-evt.delta.y, -evt.delta.x, 0)); rotation = Quaternion.AngleAxis(evt.delta.magnitude, rotDir.normalized) * rotation; GUI.changed = true; evt.Use(); } break; } case EventType.MouseUp: { if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2)) { //Tools.UnlockHandlePosition(); GUIUtility.hotControl = 0; evt.Use(); if (shutdownFunction != null) { shutdownFunction(); } EditorGUIUtility.SetWantsMouseJumping(0); } break; } case EventType.KeyDown: { if (evt.keyCode == KeyCode.Escape && GUIUtility.hotControl == id) { // We do not use the event nor clear hotcontrol to ensure auto revert value kicks in from native side //Tools.UnlockHandlePosition(); EditorGUIUtility.SetWantsMouseJumping(0); } break; } case EventType.Repaint: { var originalColor = Color.white; if (id == GUIUtility.keyboardControl) { Handles.color = Handles.selectedColor; } else if (CSGHandles.disabled) { Handles.color = Color.Lerp(originalColor, Handles.secondaryColor, 0.75f); } // We only want the position to be affected by the Handles.matrix. //Handles.matrix = Matrix4x4.identity; //Handles.DrawWireDisc(worldPosition, camera.transform.forward, size); //Handles.matrix = origMatrix; Handles.color = originalColor; break; } } return(rotation); }
private static Vector2 CalcDeltaAlongDirections(int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.DrawCapFunction drawFunc, Vector2 snap, bool drawHelper) { Vector2 vector = new Vector2(0f, 0f); Event current = Event.current; switch (current.GetTypeForControl(id)) { case EventType.MouseDown: if (((HandleUtility.nearestControl == id && current.button == 0) || (GUIUtility.keyboardControl == id && current.button == 2)) && GUIUtility.hotControl == 0) { Plane plane = new Plane(Handles.matrix.MultiplyVector(handleDir), Handles.matrix.MultiplyPoint(handlePos)); Ray ray = HandleUtility.GUIPointToWorldRay(current.mousePosition); float distance = 0f; plane.Raycast(ray, out distance); GUIUtility.keyboardControl = id; GUIUtility.hotControl = id; Slider2D.s_CurrentMousePosition = current.mousePosition; Slider2D.s_StartPosition = handlePos; Vector3 a = Handles.s_InverseMatrix.MultiplyPoint(ray.GetPoint(distance)); Vector3 lhs = a - handlePos; Slider2D.s_StartPlaneOffset.x = Vector3.Dot(lhs, slideDir1); Slider2D.s_StartPlaneOffset.y = Vector3.Dot(lhs, slideDir2); current.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } break; case EventType.MouseUp: if (GUIUtility.hotControl == id && (current.button == 0 || current.button == 2)) { GUIUtility.hotControl = 0; current.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { Slider2D.s_CurrentMousePosition += current.delta; Vector3 a2 = Handles.matrix.MultiplyPoint(handlePos); Vector3 normalized = Handles.matrix.MultiplyVector(slideDir1).normalized; Vector3 normalized2 = Handles.matrix.MultiplyVector(slideDir2).normalized; Ray ray2 = HandleUtility.GUIPointToWorldRay(Slider2D.s_CurrentMousePosition); Plane plane2 = new Plane(a2, a2 + normalized, a2 + normalized2); float distance2 = 0f; if (plane2.Raycast(ray2, out distance2)) { Vector3 point = Handles.s_InverseMatrix.MultiplyPoint(ray2.GetPoint(distance2)); vector.x = HandleUtility.PointOnLineParameter(point, Slider2D.s_StartPosition, slideDir1); vector.y = HandleUtility.PointOnLineParameter(point, Slider2D.s_StartPosition, slideDir2); vector -= Slider2D.s_StartPlaneOffset; if (snap.x > 0f || snap.y > 0f) { vector.x = Handles.SnapValue(vector.x, snap.x); vector.y = Handles.SnapValue(vector.y, snap.y); } GUI.changed = true; } current.Use(); } break; case EventType.Repaint: if (drawFunc != null) { Vector3 vector2 = handlePos + offset; Quaternion rotation = Quaternion.LookRotation(handleDir, slideDir1); Color color = Color.white; if (id == GUIUtility.keyboardControl) { color = Handles.color; Handles.color = Handles.selectedColor; } drawFunc(id, vector2, rotation, handleSize); if (id == GUIUtility.keyboardControl) { Handles.color = color; } if (drawHelper && GUIUtility.hotControl == id) { Vector3[] array = new Vector3[4]; float d = handleSize * 10f; array[0] = vector2 + (slideDir1 * d + slideDir2 * d); array[1] = array[0] - slideDir1 * d * 2f; array[2] = array[1] - slideDir2 * d * 2f; array[3] = array[2] + slideDir1 * d * 2f; Color color2 = Handles.color; Handles.color = Color.white; float num = 0.6f; Handles.DrawSolidRectangleWithOutline(array, new Color(1f, 1f, 1f, 0.05f), new Color(num, num, num, 0.4f)); Handles.color = color2; } } break; case EventType.Layout: if (Slider2D.< > f__mg$cache0 == null) { Slider2D.< > f__mg$cache0 = new Handles.DrawCapFunction(Handles.ArrowCap); } if (drawFunc == Slider2D.< > f__mg$cache0) { HandleUtility.AddControl(id, HandleUtility.DistanceToLine(handlePos + offset, handlePos + handleDir * handleSize)); HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(handlePos + offset + handleDir * handleSize, handleSize * 0.2f)); } else { if (Slider2D.< > f__mg$cache1 == null) { Slider2D.< > f__mg$cache1 = new Handles.DrawCapFunction(Handles.RectangleCap); } if (drawFunc == Slider2D.< > f__mg$cache1) { HandleUtility.AddControl(id, HandleUtility.DistanceToRectangle(handlePos + offset, Quaternion.LookRotation(handleDir, slideDir1), handleSize)); } else { HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(handlePos + offset, handleSize * 0.5f)); } } break; } return(vector); }
public void OnSceneGUI() { serializedObject.Update(); Rect r = new Rect(0, Screen.height - 160, 160, 120); Vector2 mouse = Event.current.mousePosition; Rect r2 = r; r2.yMin -= 30; r2.xMin -= 10; r2.xMax += 10; r2.yMax += 10; if (r2.Contains(mouse) && Event.current.type == EventType.Layout) { int controlID = GUIUtility.GetControlID(1024, FocusType.Passive); HandleUtility.AddControl(controlID, 0F); } Handles.BeginGUI(); GUILayout.BeginArea(r, Script.gameObject.name, "Window"); mOpenCaptureWindow = GUILayout.Toggle(mOpenCaptureWindow, "preview capture tool?"); GUI.enabled = false; Object prefab = PrefabUtility.GetPrefabParent(target); if (mOpenCaptureWindow && prefab != null) { GUI.enabled = true; Tools.hidden = true; } else { Tools.hidden = false; } if (GUILayout.Button("capture")) { if (prefab == null) { //string info = target.name + " has no prefab instance, it can't be captured. please make a prefab instance first."; //XEditorTool.ShowNotification(info); //do nothing. } else { string path = AssetDatabase.GetAssetPath(prefab); string imgPath = path.Substring(0, path.LastIndexOf(".prefab")) + "_preview.png"; Debug.Log("Saving captured image to:" + imgPath); CaptureRect(imgPath); } } GUI.enabled = true; EditView.boolValue = GUILayout.Toggle(EditView.boolValue, "update in editor?"); EditorGUIUtility.labelWidth = 110f; PlaybackTime.floatValue = EditorGUILayout.FloatField("Playback time: ", PlaybackTime.floatValue); EditorGUIUtility.labelWidth = 0f; if (PlaybackTime.floatValue < 0f) { PlaybackTime.floatValue = 0f; } if (EditView.boolValue) { Script.EnableEditView(); GUI.enabled = true; } else { Paused.boolValue = false; Script.DisableEditView(); GUI.enabled = false; } //if (EditView.boolValue) { string disp = "Pause"; if (Paused.boolValue) { disp = "Play"; } EditorGUILayout.BeginHorizontal(); if (GUILayout.Button(disp)) { Paused.boolValue = !Paused.boolValue; } if (GUILayout.Button("Reset")) { Paused.boolValue = false; Script.ResetEditScene(); } GUI.enabled = true; EditorGUILayout.EndHorizontal(); } GUILayout.EndArea(); Handles.EndGUI(); if (mOpenCaptureWindow) { HandleInput(); UpdateRect(); DrawCaptureHandles(); CaptureProcess(); } serializedObject.ApplyModifiedProperties(); }