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); }
public static Vector3 DoCenter(int id, Vector3 value, Vector3 position, Quaternion rotation, float size, CSGHandles.CapFunction capFunction, bool snapping, float snap, CSGHandles.InitFunction initFunction, CSGHandles.InitFunction shutdownFunction) { size *= 0.5f; var evt = Event.current; switch (evt.GetTypeForControl(id)) { case EventType.Layout: { capFunction(id, position, rotation, size, EventType.Layout); 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; s_StartScale3 = value; s_ValueDrag = 0; evt.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } break; } case EventType.MouseDrag: { if (GUIUtility.hotControl == id) { s_ValueDrag += HandleUtility.niceMouseDelta * .01f; var oldScaleValue = s_StartScale3.x; var newScaleValue = (SnapValue(s_ValueDrag, snap, snapping) + 1.0f) * s_StartScale3.x; float dif = newScaleValue / oldScaleValue; if (!RealtimeCSG.CSGSettings.LockAxisX) { value.x = newScaleValue; } if (!RealtimeCSG.CSGSettings.LockAxisY) { value.y = s_StartScale3.y * dif; } if (!RealtimeCSG.CSGSettings.LockAxisZ) { value.z = s_StartScale3.z * dif; } s_ScaleDrawLength = value.x / s_StartScale3.x; GUI.changed = true; evt.Use(); } break; } case EventType.KeyDown: { if (GUIUtility.hotControl == id) { if (evt.keyCode == KeyCode.Escape) { value = s_StartScale3; s_ScaleDrawLength = 1.0f; GUIUtility.hotControl = 0; GUI.changed = true; evt.Use(); } } break; } case EventType.MouseUp: { if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2)) { GUIUtility.hotControl = 0; s_ScaleDrawLength = 1.0f; evt.Use(); if (shutdownFunction != null) { shutdownFunction(); } EditorGUIUtility.SetWantsMouseJumping(0); } break; } case EventType.Repaint: { 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, size, EventType.Repaint); Handles.color = originalColor; break; } } return(value); }
public static Vector3 Do(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) { bool orgGuiChanged = GUI.changed; GUI.changed = false; var delta = CalcDeltaAlongDirections(id, handlePos, offset, handleDir, slideDir1, slideDir2, handleSize, capFunction, snapping, snapVertices, initFunction, shutdownFunction); if (GUI.changed) { handlePos = s_StartPosition + slideDir1 * delta.x + slideDir2 * delta.y; if (s_SnapVertices == null) { if (snapVertices != null) { s_SnapVertices = snapVertices; } else { s_SnapVertices = new Vector3[] { s_StartPosition } }; } if (snapping) { handlePos = RealtimeCSG.CSGGrid.SnapDeltaToGrid(handlePos - s_StartPosition, s_SnapVertices, snapToGridPlane: false, snapToSelf: true) + s_StartPosition; } else { handlePos = RealtimeCSG.CSGGrid.HandleLockedAxi(handlePos - s_StartPosition) + s_StartPosition; } } GUI.changed |= orgGuiChanged; return(handlePos); }
public static Vector3 Do(int id, Vector3 position, Quaternion rotation, float size, CSGHandles.CapFunction handleFunction, bool snapping, Vector3[] snapVertices) { 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; handleFunction(id, position, rotation, size, EventType.Layout); Handles.matrix = origMatrix; break; } case EventType.MouseDown: { if ((HandleUtility.nearestControl == id && evt.button == 0) || (GUIUtility.keyboardControl == id && evt.button == 2)) { GUIUtility.hotControl = GUIUtility.keyboardControl = id; s_CurrentMousePosition = s_StartMousePosition = evt.mousePosition; s_StartPosition = position; if (snapVertices != null) { s_SnapVertices = snapVertices; } else { s_SnapVertices = new Vector3[] { s_StartPosition } }; evt.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } break; } case EventType.MouseDrag: { if (GUIUtility.hotControl == id) { s_CurrentMousePosition += new Vector2(evt.delta.x, -evt.delta.y); var screenPos = Camera.current.WorldToScreenPoint(Handles.matrix.MultiplyPoint(s_StartPosition)); screenPos += (Vector3)(s_CurrentMousePosition - s_StartMousePosition); position = Handles.inverseMatrix.MultiplyPoint(Camera.current.ScreenToWorldPoint(screenPos)); if (snapping) { position = RealtimeCSG.CSGGrid.SnapDeltaToGrid(position - s_StartPosition, s_SnapVertices, snapToGridPlane: false, snapToSelf: true) + s_StartPosition; } else { position = RealtimeCSG.CSGGrid.HandleLockedAxi(position - s_StartPosition) + s_StartPosition; } 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: { var temp = Color.white; if (id == GUIUtility.keyboardControl) { temp = Handles.color; Handles.color = Handles.selectedColor; } Handles.matrix = Matrix4x4.identity; handleFunction(id, worldPosition, Camera.current.transform.rotation, size, EventType.Repaint); Handles.matrix = origMatrix; if (id == GUIUtility.keyboardControl) { Handles.color = temp; } break; } } return(position); }
internal static Vector3 Do(int id, Vector3 position, Vector3 direction, float size, CSGHandles.CapFunction capFunction, SnapMode snapMode, Vector3[] snapVertices, CSGHandles.InitFunction initFunction = null, CSGHandles.InitFunction shutdownFunction = null) { var evt = Event.current; switch (evt.GetTypeForControl(id)) { case EventType.Layout: { if (capFunction != null) { capFunction(id, position, Quaternion.LookRotation(direction), size, EventType.Layout); } else { HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(position, size * .2f)); } 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(); } GUIUtility.hotControl = GUIUtility.keyboardControl = id; s_CurrentMousePosition = s_StartMousePosition = evt.mousePosition; s_StartPosition = position; evt.Use(); EditorGUIUtility.SetWantsMouseJumping(1); } break; } case EventType.MouseDrag: { if (GUIUtility.hotControl == id) { if (s_SnapVertices == null) { if (snapVertices != null) { s_SnapVertices = snapVertices.ToArray(); } else { s_SnapVertices = new Vector3[] { s_StartPosition } }; } s_CurrentMousePosition += evt.delta; var dist = HandleUtility.CalcLineTranslation(s_StartMousePosition, s_CurrentMousePosition, s_StartPosition, direction); var worldDirection = Handles.matrix.MultiplyVector(direction); var worldPosition = Handles.matrix.MultiplyPoint(s_StartPosition) + (worldDirection * dist); position = Handles.inverseMatrix.MultiplyPoint(worldPosition); switch (snapMode) { case SnapMode.GridSnapping: { var delta = RealtimeCSG.CSGGrid.SnapDeltaToGrid(position - s_StartPosition, s_SnapVertices, snapToGridPlane: false, snapToSelf: true); position = delta + s_StartPosition; break; } case SnapMode.RelativeSnapping: { var delta = RealtimeCSG.CSGGrid.SnapDeltaRelative(position - s_StartPosition, snapToGridPlane: false); position = delta + s_StartPosition; break; } default: case SnapMode.None: { position = RealtimeCSG.CSGGrid.HandleLockedAxi(position - s_StartPosition) + s_StartPosition; break; } } GUI.changed = true; evt.Use(); } break; } case EventType.MouseUp: { if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2)) { GUIUtility.hotControl = 0; if (shutdownFunction != null) { shutdownFunction(); } evt.Use(); EditorGUIUtility.SetWantsMouseJumping(0); } break; } case EventType.Repaint: { var originalColor = Handles.color; Handles.color = CSGHandles.StateColor(originalColor, CSGHandles.disabled, CSGHandles.FocusControl == id); if (capFunction != null) { capFunction(id, position, Quaternion.LookRotation(direction), size, EventType.Repaint); } Handles.color = originalColor; break; } } return(position); }
internal static Vector3 Do(int id, Vector3 position, Vector3 direction, float size, CSGHandles.CapFunction capFunction, bool snapping, Vector3[] snapVertices, CSGHandles.InitFunction initFunction, CSGHandles.InitFunction shutdownFunction) { return(Do(id, position, direction, direction, size, capFunction, snapping, snapVertices, initFunction, shutdownFunction)); }
public static Vector3 Do(Camera camera, int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, CSGHandles.CapFunction capFunction, SnapMode snapMode, Vector3[] snapVertices, CSGHandles.InitFunction initFunction, CSGHandles.InitFunction shutdownFunction) { bool orgGuiChanged = GUI.changed; GUI.changed = false; var delta = CalcDeltaAlongDirections(id, handlePos, offset, handleDir, slideDir1, slideDir2, handleSize, capFunction, snapMode, snapVertices, initFunction, shutdownFunction); if (GUI.changed) { handlePos = s_StartPosition + slideDir1 * delta.x + slideDir2 * delta.y; if (s_SnapVertices == null) { if (snapVertices != null) { s_SnapVertices = snapVertices; } else { s_SnapVertices = new Vector3[] { s_StartPosition } }; } switch (snapMode) { case SnapMode.GridSnapping: { handlePos = RealtimeCSG.CSGGrid.SnapDeltaToGrid(camera, handlePos - s_StartPosition, s_SnapVertices, snapToGridPlane: false, snapToSelf: true) + s_StartPosition; break; } case SnapMode.RelativeSnapping: { handlePos = RealtimeCSG.CSGGrid.SnapDeltaRelative(camera, handlePos - s_StartPosition, snapToGridPlane: false) + s_StartPosition; break; } default: case SnapMode.None: { handlePos = RealtimeCSG.CSGGrid.HandleLockedAxi(handlePos - s_StartPosition) + s_StartPosition; break; } } } GUI.changed |= orgGuiChanged; return(handlePos); }