简体   繁体   English

在Monogame中用鼠标实现拖放

[英]Implement drag and drop with mouse in Monogame

I'm trying to implement some windows in my test app, and now I'm trying to make these windows able to be dragged. 我正在尝试在测试应用程序中实现一些窗口,现在我正在尝试使这些窗口能够拖动。

Check that awesome illustration to see what I'm trying to do, and what actually happens: 检查那个很棒的插图,看看我要做什么,以及实际发生了什么: 在此处输入图片说明

First, I check if the mouse position is on top of the window and if the left mouse button is pressed and held, here: 首先,我检查鼠标位置是否在窗口顶部,以及是否按住鼠标左键,在这里:

if (mouseOver && LastLeftPressed && MouseLeftPressed)
{
     onHoldClick(Events);
}

This is what I've tried: 这是我尝试过的:

private static void onHold(object sender, EventArgs e)
{
    var LastMouse = mousePosition;
    mousePosition = Mouse.GetState().Position;
    var window = sender as Window;
    if (LastMouse != mousePosition)
    {
        var deltaX = mousePosition.X - LastMouse.X;
        var deltaY = mousePosition.Y - LastMouse.Y;
        window.Rectangle = new Rectangle(mousePosition.X - deltaX, mousePosition.Y - deltaY, Width, Height);
    }
}

Sometimes the results are really weird, such as the window jumps to random positions, but usually the results are as the illustration above. 有时结果确实很奇怪,例如窗口跳到随机位置,但通常结果如上图所示。 When I click'n'hold and move the mouse really slowly, the window will be instantly moved to the mouse's position. 当我单击并按住并非常缓慢地移动鼠标时,窗口将立即移动到鼠标的位置。

I can't figure this out. 我不知道这个。

I would have done something like that... 我会做那样的事...

Class fields: 类字段:

private object grabbedObject;
private Vector2 grabOffset;
private MouseState mouseState; // update each Update() call

When mouse button down: 当鼠标按下时:

Vector2 mousePosition = new Vector2(this.mouseState.X, this.mouseState.Y);
Vector2 objectPosition = Vector2.Zero;
this.grabbedObject = GetClickedObject(mousePosition, out objectPosition);
if (this.grabbedObject != null)
{
    this.grabOffset = new Vector2(
        mousePosition.X - objectPosition.X,
        mousePosition.Y - objectPosition.Y);
}

private object GetClickedObject(Vector2 pMousePosition, out Vector2 pObjectPosition)
{
    Object clickedObject = null;
    // search first object you clicked on and set to clickedObject  
    pObjectPosition = clickedObject == null ? Vector2.Zero : clickedObject.Position;

    return clickedObject;
}

When mouse button up: 按下鼠标按钮时:

if (this.grabbedObject != null)
{
    // process drop action

    this.grabbedObject == null;
    this.grabOffset = Vector2.Zero;
}

When drawing: 绘制时:

if (this.grabbedObject != null)
{
    Vector2 position = new Vector2(
        this.mouseState.X - this.grabOffset.X,
        this.mouseState.Y - this.grabOffset.Y);
    // and so on...
}

I found where my problem was. 我找到了问题所在。 I was mistakenly moving the window to mousePosition.X - deltaX but instead I should have done window.Rectangle.X - deltaX . 我错误地将窗口移到mousePosition.X - deltaX但是我应该将window.Rectangle.X - deltaX

Basically, I was offsetting the window based on the mouse's position; 基本上,我是根据鼠标的位置偏移窗口。 however, the correct way was to offset the window based on its own position. 但是,正确的方法是根据窗口的位置来偏移窗口。

Here is the fixed line. 这是固定电话。

window.Rectangle = new Rectangle(window.Rectangle.X + deltaX, window.Rectangle.Y + deltaY, Width, Height);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM