C#XNA Draggable UI

时间:2012-11-16 21:18:32

标签: c# xna

我创建了一个GuiWindow类,它包含纹理,标题和矩形,因此我可以绘制纹理和标题。虽然我遇到了一些麻烦,但我一直试图让它变得可拖动。最初我把GuiWindow的边界矩形锁定在鼠标位置上:

if(bounds.contains(MouseHandle.Update()) && MouseHandle.Pressed()) //checks if the bounds rectangle contains the mouse rectangle and the mouse left button is pressed
{
    bounds.X = MouseHandle.Update().X;
    bounds.Y = MouseHandle.Update().Y;
}

这将允许我拖动,但只能在一个方向上。然后我尝试了

if(bounds.contains(MouseHandle.Update()) && MouseHandle.Pressed())
{
    int offx = bounds.X - MouseHandle.Update().X;
    int offy = bounds.Y - MouseHandle.Update().Y;
    bounds.X = MouseHandle.Update().X + offx;
    bounds.Y = MouseHandle.Update().Y + offy;
}

这次我尝试拖动时窗口仍然保持不变。我很确定我有拖拽的基本概念。我做错了吗?

2 个答案:

答案 0 :(得分:2)

好吧,这是我在一些XNA应用程序中使用鼠标移动对象的一些代码。希望这可以帮助您解决问题。

//Fields
Texture2D object;
Vector2 object_position;
Rectangle collisionRectangle;
MouseState preMouse;
bool moving = false;
Vector2 mouseOffset;


//initialize fields in LoadContent method
protected override void LoadContent()
{
    object = Content.Load<Texture2D>("nameOfYourImage");
    object_position = new Vector2((graphics.PreferredBackBufferWidth - object.Width)/2, graphics.PreferredBackBufferHeight - object.Height - 60);
    collisionRectangle = new Rectangle((int)object_position.X, (int)object_position.Y, (int)object.Width, (int)object.Height);
}


//add code to Update method



public void MouseInput(MouseState mouse)
{
    if (collsionRectangle.Contains(mouse.X, mouse.Y) && //mouse is over the object
        //the user is clicking the left mousebutton
        mouse.LeftButton == ButtonState.Pressed && 
        //in the previous Update() call the left mousebutton was released, 
        //meaning the user has just clicked the object
        preMouse.LeftButton == ButtonState.Released)
    {
        moving = true;

        //stores what the objects position should be offset by so it doesn't
        //snap to the mouse's position every time you click on it
        mouseOffset = new Vector2(Position.X - mouse.X, Position.Y - mouse.Y);
    }

    if (moving)
    {
        //if the player stops holding down the mousebutton i.e stops moving the object
        if (mouse.LeftButton == ButtonState.Released)  
            moving = false;

        //modifies the position
        Position.X = mouse.X + mouseOffset.X;
        Position.Y = mouse.Y + mouseOffset.Y;

        //prevents object from going off the screen and getting lost
        if (Convert.ToInt32(object_position.X) < 0)
            object_position.X = 0;
        if (object_position.X + object.Width > graphics.PreferredBackBufferWidth)
            object_position.X = graphics.PreferredBackBufferWidth - object.Width;
        if (Convert.ToInt32(object_position.Y) < 0)
            object_position.Y = 0;
        if (object_position.Y + object.Height > graphics.PreferredBackBufferHeight)
            object_position.Y = graphics.PreferredBackBufferHeight - object.Height;

        //updates the collision rectangle
        collisionRectangle = new Rectangle(Postion.X, Position.Y, WIDTH, HEIGHT);
    }

    preMouse = mouse; //stores the current mouseState for use in the next Update() call
}

这将使您能够使用鼠标拖动对象。当然,这不能直接复制到您的应用程序,因为我不知道您的GuiWindow的代码如何,但它应该很容易转换为您的需求。希望这会有所帮助:)

答案 1 :(得分:0)

这是我的窗口基类的一部分,用于处理XNA中的移动和调整大小。我在VB.NET中的应用,但你应该能够轻松转换它。你可能无法直接使用任何一个,但希望它会指出你正确的方向。

    Public Overrides Sub OnMouseDown(e As Ui.MouseEventArgs)
        MyBase.OnMouseDown(e)

        Dim localLoc As Point = ScreenToWindow(e.Location)
        'If Movable AndAlso localLoc.Y >= 0 AndAlso localLoc.Y <= mBackground.TopMargin Then
        If Closable AndAlso Me.CloseButtonBounds.Contains(e.Location) Then
            Me.OnCloseButton()
        ElseIf Movable AndAlso Me.DragArea.Contains(e.Location) Then
            mMouseAnchor = localLoc
            mDragType = DragType.Move
            ' ElseIf Resizable AndAlso localLoc.X > Me.Bounds.Width - mBackground.RightMargin AndAlso localLoc.Y > Me.Bounds.Height - mBackground.BottomMargin Then
        ElseIf Resizable AndAlso Me.ResizeArea.Contains(e.Location) Then
            mMouseAnchor = New Point(Me.Bounds.Right - e.Location.X, Me.Bounds.Bottom - e.Location.Y)
            mDragType = DragType.Resize
        End If
    End Sub

    Public Overrides Sub OnMouseUp(e As Ui.MouseEventArgs)
        MyBase.OnMouseUp(e)
        mDragType = DragType.None
    End Sub


    Public Overrides Sub OnMouseMove(e As Ui.MouseEventArgs)
        MyBase.OnMouseMove(e)

        If mDragType = DragType.Move Then
            Dim change As New Vector2((e.Location.X - mMouseAnchor.X) - Me.X, (e.Location.Y - mMouseAnchor.Y) - Me.Y)
            Me.Bounds = New Rectangle(e.Location.X - mMouseAnchor.X,
                                      e.Location.Y - mMouseAnchor.Y,
                                      Me.Bounds.Width,
                                      Me.Bounds.Height)
            UpdateControlLocations(change)
        ElseIf mResizable AndAlso mDragType = DragType.Resize Then
            Me.Bounds = New Rectangle(Me.Bounds.X,
                                      Me.Bounds.Y,
                                      Me.Bounds.Width - (Me.Bounds.Right - e.Location.X) + mMouseAnchor.X,
                                      Me.Bounds.Height - (Me.Bounds.Bottom - e.Location.Y) + mMouseAnchor.Y)
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.SizeNWSE
        ElseIf mDragType = DragType.None Then
            If mResizable AndAlso Me.ResizeArea.Contains(e.Location) Then
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.SizeNWSE
            Else
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            End If
        End If
    End Sub