internal static AABB Do(AABB bounds, Quaternion worldToLocalRotation, bool showEdgePoints = true) { for (int i = 0; i < sideControlIDs.Length; i++) { sideControlIDs[i] = GUIUtility.GetControlID(BoundsHash, FocusType.Passive); } for (int i = 0; i < edgeControlIDs.Length; i++) { edgeControlIDs[i] = GUIUtility.GetControlID(BoundsHash, FocusType.Passive); } UpdateColors(bounds, worldToLocalRotation, showEdgePoints); var evt = Event.current; switch (evt.type) { case EventType.Repaint: { Render(bounds, worldToLocalRotation, showEdgePoints); break; } } //var isStatic = (!Tools.hidden && EditorApplication.isPlaying && ContainsStatic(Selection.gameObjects)); //var prevDisabled = CSGHandles.disabled; var localToWorldRotation = Quaternion.Inverse(worldToLocalRotation); var localToWorld = Matrix4x4.TRS(Vector3.zero, localToWorldRotation, Vector3.one); var origMatrix = Handles.matrix; Handles.matrix = localToWorld; RealtimeCSG.Helpers.CSGHandles.InitFunction init = delegate { originalBounds = bounds; }; RealtimeCSG.Helpers.CSGHandles.InitFunction shutdown = delegate { originalBounds = bounds; }; var toggleSnapping = (Event.current.modifiers & EventModifiers.Control) == EventModifiers.Control; var doSnapping = RealtimeCSG.CSGSettings.SnapToGrid ^ toggleSnapping; for (int i = 0; i < sidePoints.Length; i++) { Handles.color = sideColors[(i * 2) + 1]; var position = sidePoints[i]; var normal = BoundsUtilities.AABBSideNormal[i]; var size = sideSizes[i]; var id = sideControlIDs[i]; EditorGUI.BeginChangeCheck(); position = CSGSlider1D.Do(id, position, normal, size, CSGHandles.HoverArrowHandleCap, doSnapping, null, init, shutdown); if (EditorGUI.EndChangeCheck()) { var originalPoint = BoundsUtilities.GetBoundsSidePoint(originalBounds, i); bounds = originalBounds; var delta = Vector3.Dot(normal, (position - originalPoint)); if (normal.x < 0) { bounds.MinX -= delta; } else if (normal.x > 0) { bounds.MaxX += delta; } else if (normal.y < 0) { bounds.MinY -= delta; } else if (normal.y > 0) { bounds.MaxY += delta; } else if (normal.z < 0) { bounds.MinZ -= delta; } else if (normal.z > 0) { bounds.MaxZ += delta; } } } if (showEdgePoints) { for (int i = 0; i < edgePoints.Length; i++) { Handles.color = edgeColors[(i * 2) + 1]; var position = edgePoints[i]; var sideIndices = BoundsUtilities.AABBEdgeSides[i]; var normal = BoundsUtilities.AABBEdgeTangents[i]; var direction1 = BoundsUtilities.AABBSideNormal[sideIndices[0]]; var direction2 = BoundsUtilities.AABBSideNormal[sideIndices[1]]; var size = edgeSizes[i] / 20.0f; var id = edgeControlIDs[i]; EditorGUI.BeginChangeCheck(); position = CSGHandles.Slider2D(id, position, Vector3.zero, normal, direction1, direction2, size, null, doSnapping, null, init, shutdown); if (EditorGUI.EndChangeCheck()) { var originalPoint = BoundsUtilities.GetBoundsEdgePoint(originalBounds, i); bounds = originalBounds; var delta = (position - originalPoint); var sides = BoundsUtilities.AABBEdgeCubeSides[i]; switch (sides[0]) { case CubeSide.NegativeX: bounds.MinX += delta.x; break; case CubeSide.PositiveX: bounds.MaxX += delta.x; break; case CubeSide.NegativeY: bounds.MinY += delta.y; break; case CubeSide.PositiveY: bounds.MaxY += delta.y; break; case CubeSide.NegativeZ: bounds.MinZ += delta.z; break; case CubeSide.PositiveZ: bounds.MaxZ += delta.z; break; } switch (sides[1]) { case CubeSide.NegativeX: bounds.MinX += delta.x; break; case CubeSide.PositiveX: bounds.MaxX += delta.x; break; case CubeSide.NegativeY: bounds.MinY += delta.y; break; case CubeSide.PositiveY: bounds.MaxY += delta.y; break; case CubeSide.NegativeZ: bounds.MinZ += delta.z; break; case CubeSide.PositiveZ: bounds.MaxZ += delta.z; break; } } } } Handles.matrix = origMatrix; if (bounds.MinX > bounds.MaxX) { float t = bounds.MinX; bounds.MinX = bounds.MaxX; bounds.MaxX = t; } if (bounds.MinY > bounds.MaxY) { float t = bounds.MinY; bounds.MinY = bounds.MaxY; bounds.MaxY = t; } if (bounds.MinZ > bounds.MaxZ) { float t = bounds.MinZ; bounds.MinZ = bounds.MaxZ; bounds.MaxZ = t; } return(bounds); }
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); }
public static Vector3 PositionHandle(Camera camera, Vector3 position, Quaternion rotation, SnapMode snapMode, Vector3[] snapVertices = null, InitFunction initFunction = null, InitFunction shutdownFunction = null) { GUI.SetNextControlName("xAxis"); var xAxisSlider = GUIUtility.GetControlID(s_xAxisMoveHandleHash, FocusType.Passive); GUI.SetNextControlName("yAxis"); var yAxisSlider = GUIUtility.GetControlID(s_yAxisMoveHandleHash, FocusType.Passive); GUI.SetNextControlName("zAxis"); var zAxisSlider = GUIUtility.GetControlID(s_zAxisMoveHandleHash, FocusType.Passive); GUI.SetNextControlName("xzPlane"); var xzPlaneSlider = GUIUtility.GetControlID(s_xzAxisMoveHandleHash, FocusType.Passive); GUI.SetNextControlName("xyPlane"); var xyPlaneSlider = GUIUtility.GetControlID(s_xyAxisMoveHandleHash, FocusType.Passive); GUI.SetNextControlName("yzPlane"); var yzPlaneSlider = GUIUtility.GetControlID(s_yzAxisMoveHandleHash, FocusType.Passive); var isStatic = (!Tools.hidden && EditorApplication.isPlaying && ContainsStatic(Selection.gameObjects)); var prevDisabled = CSGHandles.disabled; var hotControl = GUIUtility.hotControl; var xAxisIsHot = (xAxisSlider == hotControl); var yAxisIsHot = (yAxisSlider == hotControl); var zAxisIsHot = (zAxisSlider == hotControl); var xzAxisIsHot = (xzPlaneSlider == hotControl); var xyAxisIsHot = (xyPlaneSlider == hotControl); var yzAxisIsHot = (yzPlaneSlider == hotControl); var isControlHot = xAxisIsHot || yAxisIsHot || zAxisIsHot || xzAxisIsHot || xyAxisIsHot || yzAxisIsHot; var size = HandleUtility.GetHandleSize(position); var originalColor = Handles.color; var lockedAxisX = RealtimeCSG.CSGSettings.LockAxisX; var lockedAxisY = RealtimeCSG.CSGSettings.LockAxisY; var lockedAxisZ = RealtimeCSG.CSGSettings.LockAxisZ; var lockedAxisXY = lockedAxisX || lockedAxisY; var lockedAxisXZ = lockedAxisX || lockedAxisZ; var lockedAxisYZ = lockedAxisY || lockedAxisZ; //,.,.., look at 2018.1 how the position handle works w/ colors var xAxisDisabled = isStatic || prevDisabled || lockedAxisX || (isControlHot && !xAxisIsHot && !xzAxisIsHot && !xyAxisIsHot); var yAxisDisabled = isStatic || prevDisabled || lockedAxisY || (isControlHot && !yAxisIsHot && !xyAxisIsHot && !yzAxisIsHot); var zAxisDisabled = isStatic || prevDisabled || lockedAxisZ || (isControlHot && !zAxisIsHot && !xzAxisIsHot && !yzAxisIsHot); var xzPlaneDisabled = isStatic || prevDisabled || lockedAxisXZ || (isControlHot && !xzAxisIsHot); var xyPlaneDisabled = isStatic || prevDisabled || lockedAxisXY || (isControlHot && !xyAxisIsHot); var yzPlaneDisabled = isStatic || prevDisabled || lockedAxisYZ || (isControlHot && !yzAxisIsHot); var currentFocusControl = FocusControl; var xAxisIndirectlyFocused = (currentFocusControl == xyPlaneSlider || currentFocusControl == xzPlaneSlider); var yAxisIndirectlyFocused = (currentFocusControl == xyPlaneSlider || currentFocusControl == yzPlaneSlider); var zAxisIndirectlyFocused = (currentFocusControl == xzPlaneSlider || currentFocusControl == yzPlaneSlider); var xAxisSelected = xAxisIndirectlyFocused || (currentFocusControl == xAxisSlider); var yAxisSelected = yAxisIndirectlyFocused || (currentFocusControl == yAxisSlider); var zAxisSelected = zAxisIndirectlyFocused || (currentFocusControl == zAxisSlider); var xzAxiSelected = (currentFocusControl == xAxisSlider || currentFocusControl == zAxisSlider); var xyAxiSelected = (currentFocusControl == xAxisSlider || currentFocusControl == yAxisSlider); var yzAxiSelected = (currentFocusControl == yAxisSlider || currentFocusControl == zAxisSlider); var xAxisColor = CSGHandles.StateColor(Handles.xAxisColor, xAxisDisabled, xAxisSelected); var yAxisColor = CSGHandles.StateColor(Handles.yAxisColor, yAxisDisabled, yAxisSelected); var zAxisColor = CSGHandles.StateColor(Handles.zAxisColor, zAxisDisabled, zAxisSelected); var xzPlaneColor = CSGHandles.StateColor(Handles.yAxisColor, xzPlaneDisabled, xzAxiSelected); var xyPlaneColor = CSGHandles.StateColor(Handles.zAxisColor, xyPlaneDisabled, xyAxiSelected); var yzPlaneColor = CSGHandles.StateColor(Handles.xAxisColor, yzPlaneDisabled, yzAxiSelected); CSGHandles.disabled = xAxisDisabled; { Handles.color = xAxisColor; position = CSGSlider1D.Do(camera, xAxisSlider, position, rotation * Vector3.right, size, ArrowHandleCap, snapMode, snapVertices, initFunction, shutdownFunction); } CSGHandles.disabled = yAxisDisabled; { Handles.color = yAxisColor; position = CSGSlider1D.Do(camera, yAxisSlider, position, rotation * Vector3.up, size, ArrowHandleCap, snapMode, snapVertices, initFunction, shutdownFunction); } CSGHandles.disabled = zAxisDisabled; { Handles.color = zAxisColor; position = CSGSlider1D.Do(camera, zAxisSlider, position, rotation * Vector3.forward, size, ArrowHandleCap, snapMode, snapVertices, initFunction, shutdownFunction); } CSGHandles.disabled = xzPlaneDisabled; if (!CSGHandles.disabled) { Handles.color = xzPlaneColor; position = DoPlanarHandle(camera, xzPlaneSlider, PrincipleAxis2.XZ, position, rotation, size * 0.25f, snapMode, snapVertices, initFunction, shutdownFunction); } CSGHandles.disabled = xyPlaneDisabled; if (!CSGHandles.disabled) { Handles.color = xyPlaneColor; position = DoPlanarHandle(camera, xyPlaneSlider, PrincipleAxis2.XY, position, rotation, size * 0.25f, snapMode, snapVertices, initFunction, shutdownFunction); } CSGHandles.disabled = yzPlaneDisabled; if (!CSGHandles.disabled) { Handles.color = yzPlaneColor; position = DoPlanarHandle(camera, yzPlaneSlider, PrincipleAxis2.YZ, position, rotation, size * 0.25f, snapMode, snapVertices, initFunction, shutdownFunction); } CSGHandles.disabled = prevDisabled; Handles.color = originalColor; return(position); }