1

テクスチャ、タイトル、および長方形を保持する 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;
}

今回は、ドラッグしようとしたときにウィンドウが静止したままになりました。ドラッグダウンの基本的な概念があると確信しています。私は何か間違ったことをしていますか?

4

2 に答える 2

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 のコードがどのようになっているかわからないため、これをアプリケーションに直接コピーすることはできませんが、必要に応じて簡単に変換できるはずです。お役に立てれば :)

于 2012-11-16T23:00:37.097 に答える
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
于 2012-11-16T21:23:06.167 に答える