public override void OnMouseDown(MouseEvent3DArgs mouseEvent3D) { InteractionContext.Scene.DrawSelection = false; IObject3D selectedItem = RootSelection; if (mouseEvent3D.info != null && selectedItem != null) { SelectedItemOnMouseDown = selectedItem; angleTextControl.Visible = true; var selectedObject = SelectedItemOnMouseDown; AxisAlignedBoundingBox currentSelectedBounds = selectedObject.GetAxisAlignedBoundingBox(Matrix4X4.Identity); var selectedObjectRotationCenter = currentSelectedBounds.Center; Vector3 cornerForAxis = GetCornerPosition(selectedObject); // move the rotation center to the correct side of the bounding box selectedObjectRotationCenter[RotationAxis] = cornerForAxis[RotationAxis]; mouseDownInfo = new Mouse3DInfo( mouseEvent3D.info.HitPosition, selectedObject.Matrix, selectedObjectRotationCenter, GetControlCenter(selectedObject), RotationAxis ); // Get move data updated lastSnappedRotation = 0; SnappedRotationAngle = 0; rotatingCW = true; mouseMoveInfo = mouseDownInfo; InteractionContext.Scene.ShowSelectionShadow = false; mouseMoveInfo = new Mouse3DInfo( mouseEvent3D.info.HitPosition, selectedObject.Matrix, selectedObjectRotationCenter, GetControlCenter(selectedObject), RotationAxis ); } base.OnMouseDown(mouseEvent3D); }
public override void CancelOpperation() { IObject3D selectedItem = RootSelection; if (selectedItem != null && MouseDownOnControl && mouseDownInfo != null) { selectedItem.Matrix = mouseDownInfo.SelectedObjectTransform; MouseDownOnControl = false; MouseOver = false; mouseDownInfo = null; mouseMoveInfo = null; InteractionContext.Scene.DrawSelection = true; InteractionContext.Scene.ShowSelectionShadow = true; } base.CancelOpperation(); }
public RotateCornerControl(IInteractionVolumeContext context, int axisIndex) : base(context) { angleTextControl = new InlineEditControl() { ForceHide = ForceHideAngle, GetDisplayString = (value) => "{0:0.0#}°".FormatWith(value) }; angleTextControl.VisibleChanged += (s, e) => { mouseDownInfo = null; Invalidate(); }; InteractionContext.GuiSurface.AddChild(angleTextControl); angleTextControl.Visible = false; angleTextControl.EditComplete += (s, e) => { var selectedItem = RootSelection; if (selectedItem != null && mouseDownInfo != null) { if (mouseMoveInfo != null) { SnappedRotationAngle = MathHelper.DegreesToRadians(angleTextControl.Value); mouseMoveInfo.AngleOfHit = mouseDownInfo.AngleOfHit + SnappedRotationAngle; rotatingCW = DeltaAngle(0, SnappedRotationAngle) < 0; // undo the last rotation RotateAroundAxis(selectedItem, -lastSnappedRotation); // rotate it RotateAroundAxis(selectedItem, SnappedRotationAngle); Invalidate(); lastSnappedRotation = SnappedRotationAngle; } InteractionContext.Scene.AddTransformSnapshot(mouseDownInfo.SelectedObjectTransform); } }; this.RotationAxis = axisIndex; DrawOnTop = true; rotationHandle = PlatonicSolids.CreateCube(selectCubeSize); RectangleDouble bounds = arrows.GetBounds(); if (rotationImageWhite == null) { rotationImageWhite = new ImageBuffer(64, 64, 32, new BlenderBGRA()); } VertexSourceApplyTransform arrows2 = new VertexSourceApplyTransform(arrows, Affine.NewTranslation(-bounds.Center) * Affine.NewScaling(rotationImageWhite.Width / bounds.Width, rotationImageWhite.Height / bounds.Height) * Affine.NewTranslation(rotationImageWhite.Width / 2, rotationImageWhite.Height / 2)); Graphics2D imageGraphics = rotationImageWhite.NewGraphics2D(); imageGraphics.Clear(new Color(Color.White, 0)); imageGraphics.Render(new FlattenCurves(arrows2), Color.White); ImageBuffer clearImage = new ImageBuffer(2, 2, 32, new BlenderBGRA()); for (int i = 0; i < rotationHandle.Faces.Count; i++) { if (i == 0 || i == 1) { rotationHandle.PlaceTextureOnFace(i, rotationImageWhite); } else { rotationHandle.PlaceTextureOnFace(i, clearImage); } } CollisionVolume = rotationHandle.CreateTraceData(); InteractionContext.GuiSurface.AfterDraw += InteractionLayer_AfterDraw; }
public override void OnMouseMove(MouseEvent3DArgs mouseEvent3D) { IObject3D selectedItem = RootSelection; if (selectedItem != null) { var controlCenter = GetControlCenter(selectedItem); if (mouseDownInfo != null) { controlCenter = mouseDownInfo.ControlCenter; } var hitPlane = new PlaneShape(RotationPlanNormal, Vector3Ex.Dot(RotationPlanNormal, controlCenter), null); IntersectInfo hitOnRotationPlane = hitPlane.GetClosestIntersection(mouseEvent3D.MouseRay); if (hitOnRotationPlane != null) { AxisAlignedBoundingBox currentSelectedBounds = selectedItem.GetAxisAlignedBoundingBox(Matrix4X4.Identity); var selectedObjectRotationCenter = currentSelectedBounds.Center; if (mouseDownInfo != null) { selectedObjectRotationCenter = mouseDownInfo.SelectedObjectRotationCenter; } Vector3 cornerForAxis = GetCornerPosition(selectedItem); // move the rotation center to the correct side of the bounding box selectedObjectRotationCenter[RotationAxis] = cornerForAxis[RotationAxis]; mouseMoveInfo = new Mouse3DInfo( hitOnRotationPlane.HitPosition, selectedItem.Matrix, selectedObjectRotationCenter, controlCenter, RotationAxis ); if (MouseDownOnControl && mouseDownInfo != null) { double rawDeltaRotationAngle = mouseMoveInfo.AngleOfHit - mouseDownInfo.AngleOfHit; double snapRadians = MathHelper.DegreesToRadians(1); // snap this position to the grid if (rawDeltaRotationAngle > 0) { SnappedRotationAngle = ((int)((rawDeltaRotationAngle / snapRadians) + .5)) * snapRadians; } else { SnappedRotationAngle = ((int)((rawDeltaRotationAngle / snapRadians) - .5)) * snapRadians; } int snappingIndex = GetSnapIndex(selectedItem, numSnapPoints); if (snappingIndex != -1) { SnappedRotationAngle = snappingIndex * MathHelper.Tau / numSnapPoints; } else if (InteractionContext.GuiSurface.ModifierKeys == Keys.Shift) { snapRadians = MathHelper.DegreesToRadians(45); if (rawDeltaRotationAngle > 0) { SnappedRotationAngle = ((int)((rawDeltaRotationAngle / snapRadians) + .5)) * snapRadians; } else { SnappedRotationAngle = ((int)((rawDeltaRotationAngle / snapRadians) - .5)) * snapRadians; } } if (SnappedRotationAngle < 0) { SnappedRotationAngle += MathHelper.Tau; } // check if this move crosses zero degrees if (lastSnappedRotation == 0 && SnappedRotationAngle != 0) { rotatingCW = DeltaAngle(0, SnappedRotationAngle) < 0; } else if ((DeltaAngle(0, SnappedRotationAngle) < 0 && DeltaAngle(0, lastSnappedRotation) > 0 && Math.Abs(DeltaAngle(0, lastSnappedRotation)) < 1) || (DeltaAngle(0, SnappedRotationAngle) > 0 && DeltaAngle(0, lastSnappedRotation) < 0 && Math.Abs(DeltaAngle(0, lastSnappedRotation)) < 1)) { // let's figure out which way we are going rotatingCW = (DeltaAngle(0, SnappedRotationAngle) < 0 && DeltaAngle(0, lastSnappedRotation) > 0); } // undo the last rotation RotateAroundAxis(selectedItem, -lastSnappedRotation); // rotate it RotateAroundAxis(selectedItem, SnappedRotationAngle); lastSnappedRotation = SnappedRotationAngle; Invalidate(); } } } base.OnMouseMove(mouseEvent3D); }