Mouse SetNativePosition Method Delta Engine Documentation

Namespace: DeltaEngine.Input
Assembly: DeltaEngine.Input (in DeltaEngine.Input.dll) Version: 1.1.1.0 (1.1.1)
Syntax

public abstract void SetNativePosition(
	Vector2D position
)
Remarks

Tests: DeltaEngine.Input.Tests.MouseDragDropTriggerTests
Examples

2 unit tests call DeltaEngine.Input.Mouse.SetNativePosition(DeltaEngine.Datatypes.Vector2D)
    [Test, CloseAfterFirstFrame]
    public void CreateFromAttributes()
    {
        var trigger = new MouseDragDropTrigger(new Dictionary<string, string>
            {
                { "StartArea", "1.1 2.2 3.3 4.4" },
                { "Button", "Right" }
            });
        Assert.AreEqual(new Rectangle(1.1f, 2.2f, 3.3f, 4.4f), trigger.StartArea);
        Assert.AreEqual(MouseButton.Right, trigger.Button);
        Assert.AreEqual(Vector2D.Unused, trigger.StartDragPosition);
        Assert.Throws<MouseDragDropTrigger.CannotCreateMouseDragDropTriggerWithoutStartArea>(
            () => new MouseDragDropTrigger(new Dictionary<string, string>()));
    }

    [Test, CloseAfterFirstFrame]
    public void DragDropOutsideStartArea()
    {
        if (mouse == null)
            return; //ncrunch: no coverage
        Vector2D startPoint = -Vector2D.One;
        new Command(position => { startPoint = position; }).Add(
            new MouseDragDropTrigger(Rectangle.HalfCentered, MouseButton.Left));
        SetMouseState(State.Pressing, Vector2D.Zero);
        SetMouseState(State.Pressed, Vector2D.One);
        SetMouseState(State.Releasing, Vector2D.One);
        SetMouseState(State.Released, Vector2D.One);
        Assert.AreEqual(-Vector2D.One, startPoint);
    }

    private void SetMouseState(State state, Vector2D position)
    {
        if (mouse == null)
            return; //ncrunch: no coverage
        mouse.SetButtonState(MouseButton.Left, state);
        mouse.SetNativePosition(position);
        AdvanceTimeAndUpdateEntities();
    }

    [Test, CloseAfterFirstFrame]
    public void DragDropInsideStartArea()
    {
        if (mouse == null)
            return; //ncrunch: no coverage
        Vector2D startPoint = -Vector2D.One;
        new Command(position => { startPoint = position; }).Add(
            new MouseDragDropTrigger(Rectangle.HalfCentered, MouseButton.Left));
        SetMouseState(State.Pressing, Vector2D.Half);
        SetMouseState(State.Pressed, Vector2D.One);
        SetMouseState(State.Releasing, Vector2D.One);
        Assert.AreEqual(Vector2D.Half, startPoint);
    }

    [Test, CloseAfterFirstFrame]
    public void DragDropCloseToStartPointWillDoNothing()
    {
        if (mouse == null)
            return; //ncrunch: no coverage
        Vector2D startPoint = -Vector2D.One;
        new Command(position => { startPoint = position; }).Add(
            new MouseDragDropTrigger(Rectangle.HalfCentered, MouseButton.Left));
        SetMouseState(State.Pressing, Vector2D.Half);
        SetMouseState(State.Pressed, Vector2D.Half);
        SetMouseState(State.Releasing, Vector2D.Half);
        Assert.AreEqual(-Vector2D.One, startPoint);
    }
}
    [Test, CloseAfterFirstFrame]
    public void CreateFromAttributes()
    {
        var trigger = new MouseHoldTrigger(new Dictionary<string, string>
            {
                { "HoldArea", "1 2 3 4" },
                { "HoldTime", "5.5" },
                { "Button", "Right" }
            });
        Assert.AreEqual(new Rectangle(1, 2, 3, 4), trigger.HoldArea);
        Assert.AreEqual(5.5f, trigger.HoldTime);
        Assert.AreEqual(MouseButton.Right, trigger.Button);
        Assert.Throws<MouseHoldTrigger.CannotCreateMouseHoldTriggerWithoutHoldArea>(
            () => new MouseHoldTrigger(new Dictionary<string, string>()));
    }

    [Test, CloseAfterFirstFrame]
    public void HoldMouseOutsideHoldArea()
    {
        if (mouse == null)
            return; //ncrunch: no coverage
        Vector2D mousePosition = -Vector2D.One;
        new Command(position => { mousePosition = position; }).Add(
            new MouseHoldTrigger(Rectangle.HalfCentered));
        SetMouseState(State.Pressing, Vector2D.Zero);
        SetMouseState(State.Pressed, Vector2D.Zero);
        AdvanceTimeAndUpdateEntities(1.05f);
        Assert.AreEqual(-Vector2D.One, mousePosition);
    }

    private void SetMouseState(State state, Vector2D position)
    {
        if (mouse == null)
            return; //ncrunch: no coverage
        mouse.SetNativePosition(position);
        mouse.SetButtonState(MouseButton.Left, state);
        AdvanceTimeAndUpdateEntities();
    }

    [Test, CloseAfterFirstFrame]
    public void HoldMouseHovering()
    {
        var drawArea = new Rectangle(0.25f, 0.25f, 0.5f, 0.25f);
        var rect = new FilledRect(drawArea, Color.Blue);
        var trigger = new MouseHoldTrigger(drawArea);
        trigger.Invoked += () => rect.Color = Color.Red;
        trigger.Movement = new Vector2D(0.3f, 0.3f);
        Assert.IsFalse(trigger.IsHovering());
        AdvanceTimeAndUpdateEntities(1.05f);
        Assert.IsFalse(trigger.IsHovering());
    }

    [Test, CloseAfterFirstFrame]
    public void MoveMouseInsideHoldArea()
    {
        if (mouse == null)
            return; //ncrunch: no coverage
        Vector2D mousePosition = -Vector2D.One;
        new Command(position => { mousePosition = position; }).Add(
            new MouseHoldTrigger(Rectangle.HalfCentered));
        SetMouseState(State.Pressing, Vector2D.Half);
        SetMouseState(State.Pressed, Vector2D.Half);
        AdvanceTimeAndUpdateEntities(0.5f);
        SetMouseState(State.Pressed, new Vector2D(0.6f, 0.6f));
        AdvanceTimeAndUpdateEntities(0.5f);
        Assert.AreEqual(Vector2D.Half, mousePosition);
    }

    //ncrunch: no coverage start
    [Test, CloseAfterFirstFrame, Category("Slow")]
    public void HoldMouseInsideHoldArea()
    {
        if (mouse == null)
            return; //ncrunch: no coverage
        Vector2D mousePosition = -Vector2D.One;
        new Command(position => { mousePosition = position; }).Add(
            new MouseHoldTrigger(Rectangle.HalfCentered));
        SetMouseState(State.Pressing, Vector2D.Half);
        SetMouseState(State.Pressed, Vector2D.Half);
        AdvanceTimeAndUpdateEntities(1.05f);
        Assert.AreEqual(Vector2D.Half, mousePosition);
    }
}
See Also