C# XNA 可拖动 UI [英] C# XNA Draggable UI

查看:26
本文介绍了C# XNA 可拖动 UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含纹理、标题和矩形的 GuiWindow 类,以便我可以绘制纹理和标题.我一直试图使它可拖动,尽管我遇到了一些麻烦.最初我将 GuiWindow 的边界矩形锁定在鼠标位置上:

I created a GuiWindow class that held a texture, title, and rectangle so I can draw the texture and title. I've been trying to make it draggable, though I am having a bit of trouble. Originally I had the GuiWindow's bounds rectangle just lock onto the mouse positions:

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;
}

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

which would allow me to drag, though only in a single direction. I then tried

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;
}

这一次,当我尝试拖动时,窗口只是静止不动.我很确定我有拖下的基本概念.我做错了什么吗?

this time the window just stayed still when I tried dragging. I'm pretty sure I have the basic concept of dragging down. Am I doing something wrong?

推荐答案

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

Alright this is some code that I've been using to move objects with the mouse in some of my XNA applications. Hopefully this will help you with your problem.

//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 的代码如何,但应该很容易转换为您的需要.希望这会有所帮助:)

This will make you able to drag the object with the mouse. Now of course this won't be directly copyable to your application, as i don't know how the code of your GuiWindow, but it should be easy to convert to your needs. Hope this helps :)

这篇关于C# XNA 可拖动 UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆