Example #1
0
            protected override bool OnTryBeginModify()
            {
                if (!Owner.IsAllowScale())
                {
                    return(false);
                }

                Axes axes = GetAxesByMousePosition();

                if (axes.TrueCount != 0)
                {
                    modifyPosition = Owner.GetPosition();
                    Vector2 screenPosition;
                    if (CameraSettings.ProjectToScreenCoordinates(modifyPosition, out screenPosition))
                    {
                        startMouseOffset   = Viewport.MousePosition - screenPosition;
                        startMousePosition = Viewport.MousePosition;
                        //modifyScale = new Vec3( 1, 1, 1 );
                        selectedAxes            = axes;
                        helpText                = "";
                        initialObjectsTransform = new TransformOfObject[Owner.Objects.Count];
                        for (int n = 0; n < Owner.Objects.Count; n++)
                        {
                            TransformToolObject obj = Owner.Objects[n];
                            initialObjectsTransform[n] = new TransformOfObject(obj.Position, obj.Rotation, obj.Scale);
                        }
                        return(true);
                    }
                }

                return(base.OnTryBeginModify());
            }
Example #2
0
            public bool IsMouseOverTriangle(Vector3 p1, Vector3 p2, Vector3 p3)
            {
                Vector2 screenPosition1;

                if (!CameraSettings.ProjectToScreenCoordinates(p1, out screenPosition1))
                {
                    return(false);
                }
                Vector2 screenPosition2;

                if (!CameraSettings.ProjectToScreenCoordinates(p2, out screenPosition2))
                {
                    return(false);
                }
                Vector2 screenPosition3;

                if (!CameraSettings.ProjectToScreenCoordinates(p3, out screenPosition3))
                {
                    return(false);
                }

                Vector2 mouse = Viewport.MousePosition;

                return(PointInTriangle(mouse, screenPosition1, screenPosition2, screenPosition3));
            }
Example #3
0
            public bool IsMouseNearLine(Vector3 start, Vector3 end, out Vector2 projectedScreenPointInPixels, out Radian projectedScreenAngle, out double distance)
            {
                projectedScreenPointInPixels = Vector2.Zero;
                projectedScreenAngle         = 0;
                distance = 0;

                Vector2 viewportSize  = Viewport.SizeInPixels.ToVector2();
                Vector2 mouseInPixels = Viewport.MousePosition * viewportSize;

                Vector2 screenStart;

                if (!CameraSettings.ProjectToScreenCoordinates(start, out screenStart))
                {
                    return(false);
                }
                Vector2 screenEnd;

                if (!CameraSettings.ProjectToScreenCoordinates(end, out screenEnd))
                {
                    return(false);
                }

                Vector2 screenStartInPixels = screenStart * viewportSize;
                Vector2 screenEndInPixels   = screenEnd * viewportSize;

                Rectangle rect = new Rectangle(screenStartInPixels);

                rect.Add(screenEndInPixels);
                rect.Expand(SelectNearPixels);

                if (!rect.Contains(mouseInPixels))
                {
                    return(false);
                }

                projectedScreenPointInPixels = MathAlgorithms.ProjectPointToLine(screenStartInPixels, screenEndInPixels, mouseInPixels);

                /*double */
                distance = (mouseInPixels - projectedScreenPointInPixels).Length();
                if (distance > SelectNearPixels)
                {
                    return(false);
                }

                Vector2 screenDiff = screenEndInPixels - screenStartInPixels;

                projectedScreenAngle = Math.Atan2(screenDiff.Y, screenDiff.X);

                return(true);
            }
Example #4
0
            public override void OnRenderUI()
            {
                base.OnRenderUI();

                if (!Owner.IsAllowRotate())
                {
                    return;
                }

                var     renderer     = Viewport.CanvasRenderer;
                Vector2 viewportSize = Owner.ViewportControl.Viewport.SizeInPixels.ToVector2();

                Axis axis = Axis.None;

                if (!Viewport.MouseRelativeMode)
                {
                    if (!modify_Activated && !modify_Prepare)
                    {
                        Vector2 screenLinePointInPixels;
                        Radian  screenLineAngle;
                        axis = GetAxisByMousePosition(out screenLinePointInPixels, out screenLineAngle);
                    }
                    else
                    {
                        axis = selectedAxis;
                    }
                }

                //update cursor
                if (rotateCursor != null && axis != Axis.None)
                {
                    ViewportControl.OneFrameChangeCursor = rotateCursor;
                }

                //draw help text
                if (modify_Activated && !string.IsNullOrEmpty(helpText))
                {
                    Vector2 screenPosition;
                    if (CameraSettings.ProjectToScreenCoordinates(Owner.GetPosition(), out screenPosition))
                    {
                        double  offsetY = (ArrowPixelSize / viewportSize.Y) * 1.2f;
                        Vector2 pos     = new Vector2(screenPosition.X, screenPosition.Y - offsetY);
                        AddTextWithShadow(renderer, helpText, pos, EHorizontalAlignment.Center, EVerticalAlignment.Bottom,
                                          new ColorValue(1, 1, 0), 2);
                    }
                }
            }
Example #5
0
            protected override bool OnTryBeginModify()
            {
                if (!Owner.IsAllowRotate())
                {
                    return(false);
                }

                Vector2 screenLinePointInPixels;
                Radian  screenLineAngle;
                Axis    axis = GetAxisByMousePosition(out screenLinePointInPixels, out screenLineAngle);

                if (axis != Axis.None)
                {
                    modifyPosition = Owner.GetPosition();

                    Vector2 screenPosition;
                    if (CameraSettings.ProjectToScreenCoordinates(modifyPosition, out screenPosition))
                    {
                        Vector2 viewportSize = Owner.ViewportControl.Viewport.SizeInPixels.ToVector2();

                        selectedAxis = axis;
                        selectedScreenLinePointInPixels = screenLinePointInPixels;
                        selectedScreenLineAngle         = screenLineAngle;
                        mouseStartPosition = Viewport.MousePosition;
                        //mouseStartPositionInPixels = EngineApp.Instance.MousePosition * viewportSize;
                        helpText = "";
                        initialObjectsTransform = new TransformOfObject[Owner.Objects.Count];
                        for (int n = 0; n < Owner.Objects.Count; n++)
                        {
                            TransformToolObject obj = Owner.Objects[n];
                            initialObjectsTransform[n] = new TransformOfObject(obj.Position, obj.Rotation, obj.Scale);
                        }
                        return(true);
                    }
                }

                return(base.OnTryBeginModify());
            }
Example #6
0
            ///////////////

            Axis GetAxisByMousePosition(out Vector2 screenLinePointInPixels, out Radian screenLineAngle)
            {
                screenLinePointInPixels = Vector2.Zero;
                screenLineAngle         = 0;

                Vector2 viewportSize = Owner.ViewportControl.Viewport.SizeInPixels.ToVector2();

                Trace.Assert(Owner.Objects.Count != 0);

                Vector3 position = Owner.GetPosition();
                double  radius   = GetSize();

                if (radius == 0)
                {
                    return(Axis.None);
                }

                const double step = MathEx.PI / 64;

                double innerRadius    = radius * .75f;
                double cameraDistance = GetCameraDistance(position);

                Vector3 oldPos = Vector3.Zero;

                Quaternion startRotation = GetStartObjectsRotation();

                //X
                for (double angle = 0; angle <= MathEx.PI * 2 + step * .5f; angle += step)
                {
                    Vector3 pos = position + startRotation * (new Vector3(0, Math.Sin(angle), Math.Cos(angle)) * innerRadius);
                    if (angle != 0 && GetCameraDistance(pos) <= cameraDistance &&
                        IsMouseNearLine(oldPos, pos, out screenLinePointInPixels, out screenLineAngle, out _))
                    {
                        return(Axis.X);
                    }
                    oldPos = pos;
                }

                //Y
                for (double angle = 0; angle <= MathEx.PI * 2 + step * .5f; angle += step)
                {
                    Vector3 pos = position + startRotation * (new Vector3(Math.Sin(angle), 0, Math.Cos(angle)) * innerRadius);
                    if (angle != 0 && GetCameraDistance(pos) <= cameraDistance &&
                        IsMouseNearLine(oldPos, pos, out screenLinePointInPixels, out screenLineAngle, out _))
                    {
                        return(Axis.Y);
                    }
                    oldPos = pos;
                }

                //Z
                for (double angle = 0; angle <= MathEx.PI * 2 + step * .5f; angle += step)
                {
                    Vector3 pos = position + startRotation * (new Vector3(Math.Sin(angle),
                                                                          Math.Cos(angle), 0) * innerRadius);
                    if (angle != 0 && (GetCameraDistance(pos) <= cameraDistance || Owner.SceneMode2D) &&
                        IsMouseNearLine(oldPos, pos, out screenLinePointInPixels, out screenLineAngle, out _))
                    {
                        return(Axis.Z);
                    }
                    oldPos = pos;
                }

                Vector2 mouseInPixels = Owner.viewportControl.Viewport.MousePosition * viewportSize;

                Vector2 screenPosition;

                if (CameraSettings.ProjectToScreenCoordinates(position, out screenPosition))
                {
                    Vector2 screenPositionInPixels = screenPosition * viewportSize;
                    double  mouseDistanceInPixels  = (mouseInPixels - screenPositionInPixels).Length();

                    //Radius
                    if (Math.Abs(mouseDistanceInPixels - ArrowPixelSize) < SelectNearPixels && !Owner.SceneMode2D)
                    {
                        Vector2 diffPixels = mouseInPixels - screenPositionInPixels;
                        Vector2 direction  = diffPixels.GetNormalize();

                        screenLinePointInPixels = (screenPositionInPixels + direction * ArrowPixelSize);
                        screenLineAngle         = Math.Atan2(diffPixels.Y, diffPixels.X) + MathEx.PI / 2;

                        return(Axis.Radius);
                    }

                    //Inner circle
                    if (mouseDistanceInPixels < (ArrowPixelSize) * .75f + SelectNearPixels)
                    {
                        return(Axis.InnerCircle);
                    }
                }

                return(Axis.None);
            }
Example #7
0
            public override void OnRenderUI()
            {
                base.OnRenderUI();

                if (!Owner.IsAllowScale())
                {
                    return;
                }

                var     renderer     = Viewport.CanvasRenderer;
                Vector2 viewportSize = Viewport.SizeInPixels.ToVector2();

                Axes axes = new Axes(false, false, false);

                if (!Viewport.MouseRelativeMode)
                {
                    if (!modify_Activated)
                    {
                        axes = GetAxesByMousePosition();
                    }
                    else
                    {
                        axes = selectedAxes;
                    }
                }

                //update cursor
                if (scaleCursor != null && axes.TrueCount != 0)
                {
                    ViewportControl.OneFrameChangeCursor = scaleCursor;
                }

                Vector3 xOffset, yOffset, zOffset;

                GetAxisOffsets(out xOffset, out yOffset, out zOffset);

                Vector3    position = Owner.GetPosition();
                Quaternion rotation = GetRotation();
                double     size     = GetSize();

                if (size != 0)
                {
                    //draw axis names
                    {
                        double offsetY = (ArrowPixelSize / 20.0f) / viewportSize.Y;

                        Vector2 screenPosition;

                        if (CameraSettings.ProjectToScreenCoordinates(position + xOffset, out screenPosition))
                        {
                            Vector2    pos   = new Vector2(screenPosition.X, screenPosition.Y - offsetY);
                            ColorValue color = axes.x ? new ColorValue(1, 1, 0) : new ColorValue(1, 0, 0);
                            AddTextWithShadow(renderer, "x", pos, EHorizontalAlignment.Center, EVerticalAlignment.Bottom, color, 1);
                        }

                        if (CameraSettings.ProjectToScreenCoordinates(position + yOffset, out screenPosition))
                        {
                            Vector2    pos   = new Vector2(screenPosition.X, screenPosition.Y - offsetY);
                            ColorValue color = axes.y ? new ColorValue(1, 1, 0) : new ColorValue(0, 1, 0);
                            AddTextWithShadow(renderer, "y", pos, EHorizontalAlignment.Center, EVerticalAlignment.Bottom, color, 1);
                        }

                        if (CameraSettings.ProjectToScreenCoordinates(position + zOffset, out screenPosition))
                        {
                            Vector2    pos   = new Vector2(screenPosition.X, screenPosition.Y - offsetY);
                            ColorValue color = axes.z ? new ColorValue(1, 1, 0) : new ColorValue(0, 0, 1);
                            AddTextWithShadow(renderer, "z", pos, EHorizontalAlignment.Center, EVerticalAlignment.Bottom, color, 1);
                        }
                    }

                    //draw help text
                    if (modify_Activated && !string.IsNullOrEmpty(helpText))
                    {
                        Vector2 screenPosition;
                        if (CameraSettings.ProjectToScreenCoordinates(Owner.GetPosition(), out screenPosition))
                        {
                            double  offsetY = (ArrowPixelSize / viewportSize.Y) * 1.2f;
                            Vector2 pos     = new Vector2(screenPosition.X, screenPosition.Y - offsetY);
                            AddTextWithShadow(renderer, helpText, pos, EHorizontalAlignment.Center, EVerticalAlignment.Bottom, new ColorValue(1, 1, 0), 2);
                        }
                    }
                }
            }
Example #8
0
            public override void OnMouseMove(Vector2 mouse)
            {
                base.OnMouseMove(mouse);

                if (modify_Activated && !Viewport.MouseRelativeMode)
                {
                    UpdateCursorTransitionOnScreenBorder();

                    Vector2 viewportSize  = Owner.ViewportControl.Viewport.SizeInPixels.ToVector2();
                    Vector2 mousePosition = mouse + cursorTransitionOnScreenBorderOffset / viewportSize;

                    Vector2 screenPosition = mousePosition - startMouseOffset;
                    Ray     ray            = CameraSettings.GetRayByScreenCoordinates(screenPosition);

                    Vector3 position = modifyPosition;
                    double  size     = GetSize();

                    if (size == 0)
                    {
                        return;
                    }

                    Vector3 xOffset, yOffset, zOffset;
                    GetAxisOffsets(out xOffset, out yOffset, out zOffset);

                    double offset = 0;

                    if (!double.IsNaN(ray.Direction.X))
                    {
                        Vector2 pixelOffset      = (mousePosition - startMousePosition) * viewportSize;
                        double  mouseOffsetAngle = Math.Atan2(-pixelOffset.Y, pixelOffset.X);

                        if (selectedAxes.TrueCount == 3)
                        {
                            offset  = mousePosition.Y - startMousePosition.Y;
                            offset *= -1;
                            offset *= viewportSize.Y;
                        }
                        else
                        {
                            Vector2 screenModifyPosition;
                            if (CameraSettings.ProjectToScreenCoordinates(modifyPosition, out screenModifyPosition))
                            {
                                screenModifyPosition *= viewportSize;

                                offset = 0;

                                if (selectedAxes.x)
                                {
                                    Vector2 screenXPosition;
                                    CameraSettings.ProjectToScreenCoordinates(position + xOffset, out screenXPosition);
                                    if (screenXPosition.X != 0 && screenXPosition.Y != 0)
                                    {
                                        screenXPosition *= viewportSize;
                                        Vector2 diff      = screenXPosition - screenModifyPosition;
                                        double  diffAngle = Math.Atan2(-diff.Y, diff.X);
                                        double  angle     = mouseOffsetAngle - diffAngle;
                                        angle = MathEx.RadianNormalize360(angle);
                                        double off = Math.Cos(angle) * pixelOffset.Length();
                                        offset += off;
                                    }
                                }

                                if (selectedAxes.y)
                                {
                                    Vector2 screenYPosition;
                                    CameraSettings.ProjectToScreenCoordinates(position + yOffset, out screenYPosition);
                                    if (screenYPosition.X != 0 && screenYPosition.Y != 0)
                                    {
                                        screenYPosition *= viewportSize;
                                        Vector2 diff      = screenYPosition - screenModifyPosition;
                                        double  diffAngle = Math.Atan2(-diff.Y, diff.X);
                                        double  angle     = mouseOffsetAngle - diffAngle;
                                        angle = MathEx.RadianNormalize360(angle);
                                        double off = Math.Cos(angle) * pixelOffset.Length();
                                        offset += off;
                                    }
                                }

                                if (selectedAxes.z)
                                {
                                    Vector2 screenZPosition;
                                    CameraSettings.ProjectToScreenCoordinates(position + zOffset, out screenZPosition);
                                    if (screenZPosition.X != 0 && screenZPosition.Y != 0)
                                    {
                                        screenZPosition *= viewportSize;
                                        Vector2 diff      = screenZPosition - screenModifyPosition;
                                        double  diffAngle = Math.Atan2(-diff.Y, diff.X);
                                        double  angle     = mouseOffsetAngle - diffAngle;
                                        angle = MathEx.RadianNormalize360(angle);
                                        double off = Math.Cos(angle) * pixelOffset.Length();
                                        offset += off;
                                    }
                                }
                            }
                        }

                        const double scaleCoefficient = .01f;
                        offset *= scaleCoefficient;


                        double coef;
                        if (offset > 0)
                        {
                            double snap = Owner.GetSnapScale();
                            if (snap != 0)
                            {
                                offset += snap / 2;
                                offset /= snap;
                                offset  = (int)offset;
                                offset *= snap;
                            }

                            coef = offset + 1.0f;
                        }
                        else
                        {
                            double snap = Owner.GetSnapScale();
                            if (snap != 0)
                            {
                                offset -= snap / 2;
                                offset /= snap;
                                offset  = (int)offset;
                                offset *= snap;
                            }

                            coef = 1.0f / (1.0f - offset);
                        }

                        Vector3 scaleOffset = new Vector3(selectedAxes.x ? coef : 1, selectedAxes.y ? coef : 1, selectedAxes.z ? coef : 1);

                        //update objects
                        if (initialObjectsTransform != null && Owner.Objects.Count == initialObjectsTransform.Length)
                        {
                            Owner.OnScaleModeUpdateObjects(initialObjectsTransform, modifyPosition, scaleOffset);
                        }

                        helpText = string.Format("[{0} {1} {2}]", scaleOffset.X.ToString("F2"), scaleOffset.Y.ToString("F2"),
                                                 scaleOffset.Z.ToString("F2"));
                    }
                }
            }