在 Monogame 中实现鼠标拖放
Implement drag and drop with mouse in Monogame
我正在尝试在我的测试应用程序中实现一些 windows,现在我正在尝试使这些 windows 能够被拖动。
查看那个很棒的插图,看看我正在尝试做什么,以及实际发生了什么:
首先,我检查鼠标位置是否在 window 上方以及是否按住鼠标左键,此处:
if (mouseOver && LastLeftPressed && MouseLeftPressed)
{
onHoldClick(Events);
}
这是我试过的:
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);
}
}
有时结果很奇怪,比如window跳到随机位置,但通常结果如上图。当我单击'n'hold 并非常缓慢地移动鼠标时,window 将立即移动到鼠标的位置。
我想不通。
我会做那样的事...
Class 个字段:
private object grabbedObject;
private Vector2 grabOffset;
private MouseState mouseState; // update each Update() call
按下鼠标按钮时:
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;
}
当鼠标按钮向上时:
if (this.grabbedObject != null)
{
// process drop action
this.grabbedObject == null;
this.grabOffset = Vector2.Zero;
}
绘图时:
if (this.grabbedObject != null)
{
Vector2 position = new Vector2(
this.mouseState.X - this.grabOffset.X,
this.mouseState.Y - this.grabOffset.Y);
// and so on...
}
我找到了我的问题所在。我错误地将 window 移动到 mousePosition.X - deltaX
但我应该这样做 window.Rectangle.X - deltaX
.
基本上,我是根据鼠标的位置来偏移 window;然而,正确的方法是根据自己的位置偏移window。
这里是固话
window.Rectangle = new Rectangle(window.Rectangle.X + deltaX, window.Rectangle.Y + deltaY, Width, Height);
我正在尝试在我的测试应用程序中实现一些 windows,现在我正在尝试使这些 windows 能够被拖动。
查看那个很棒的插图,看看我正在尝试做什么,以及实际发生了什么:
首先,我检查鼠标位置是否在 window 上方以及是否按住鼠标左键,此处:
if (mouseOver && LastLeftPressed && MouseLeftPressed)
{
onHoldClick(Events);
}
这是我试过的:
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);
}
}
有时结果很奇怪,比如window跳到随机位置,但通常结果如上图。当我单击'n'hold 并非常缓慢地移动鼠标时,window 将立即移动到鼠标的位置。
我想不通。
我会做那样的事...
Class 个字段:
private object grabbedObject;
private Vector2 grabOffset;
private MouseState mouseState; // update each Update() call
按下鼠标按钮时:
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;
}
当鼠标按钮向上时:
if (this.grabbedObject != null)
{
// process drop action
this.grabbedObject == null;
this.grabOffset = Vector2.Zero;
}
绘图时:
if (this.grabbedObject != null)
{
Vector2 position = new Vector2(
this.mouseState.X - this.grabOffset.X,
this.mouseState.Y - this.grabOffset.Y);
// and so on...
}
我找到了我的问题所在。我错误地将 window 移动到 mousePosition.X - deltaX
但我应该这样做 window.Rectangle.X - deltaX
.
基本上,我是根据鼠标的位置来偏移 window;然而,正确的方法是根据自己的位置偏移window。
这里是固话
window.Rectangle = new Rectangle(window.Rectangle.X + deltaX, window.Rectangle.Y + deltaY, Width, Height);