Texture2D坐标在正确的位置不显示

问题描述 投票:2回答:1

我一直在使用SpriteBatch.Draw()渲染一些自定义“按钮”。然而今天我意识到我应该在我的按钮处绘制的坐标与我的鼠标声称它们被渲染的坐标不一致。这是一个问题,因为如果图形渲染关闭,很难检测是否单击了按钮。另一种可能性是鼠标坐标关闭。怎么了?

如果您需要更多信息,请询问。谢谢!

硬编码按钮位置:

    /// <summary>
    /// The x position at which the left part of the buttons on the main menu begin.
    /// </summary>
    public static readonly int ButtonX = 20;

    /// <summary>
    /// How wide the buttons are.
    /// </summary>
    public static readonly int ButtonWidth = 200;

    /// <summary>
    /// How tall the buttons are.
    /// </summary>
    public static readonly int ButtonHeight = 50;



    /// <summary>
    /// The y position of the top of the new game button.
    /// </summary>
    public static readonly int NewGameButtonY = 100;

    /// <summary>
    /// The y position of the top of the new game button.
    /// </summary>
    public static readonly int QuitButtonY = 200;

获取鼠标位置的方法:

        int x = Mouse.GetState().X;
        int y = Mouse.GetState().Y;

如何呈现按钮:

    private static void DrawButton(MonoButton button, ref SpriteBatch spBatch)
    {
        if (button.Visible)
        {
            spBatch.Draw(button.Image, button.DrawingBounds, colorMask);
            RenderingPipe.DrawString(MainMenuLayout.MainMenuFont, button.Text, button.DrawingBounds, Alignment.Center, colorMask, ref spBatch);
        }
    }

视觉显示SOMETHING是如何关闭:A visual display of how off something is

新游戏按钮的左上角应为(20,100)。

编辑:我的笔记本电脑的原始分辨率是1920x1080,但游戏肯定以较低的分辨率显示,然后在全屏模式下。

编辑#2:后来我意识到,当鼠标偏移工作时,简单地将单一游戏窗口分辨率设置为moniter的原始分辨率要容易得多。这完全解决了没有鼠标偏移的问题。

c# monogame
1个回答
2
投票

您的鼠标将坐标报告给光标的中心,而不是指针的尖端。

要了解原因,请考虑一个人的鼠标是另一个人的触摸屏。

您可以对按钮的hitbox应用轻微偏移,但最好只是偏移从GetState获得的坐标并将其封装到属性中:

int offsetX = 3, offsetY = 2;
MouseState _currentState; // assume this is set by main game loop every call to Update()
int MousePositionX => _currentState.X + offsetX;
int MousePositionY => _currentState.Y + offsetY;

确切的偏移量可能取决于多种因素,包括无意中的坐标转换或现有Button组件中的偏移(检查边界框计算),因此可能需要进行试验和错误以便立即解决。如果你这样做,请确保在不同的分辨率,DPI和HID(人机接口设备,鼠标,触摸,游戏手柄输入)下进行测试

如果确实需要动态计算,您可以查看查询环境以获取有关光标图标(如果有)的信息的方法。毕竟,指针并不是人们所知道的唯一游标!

© www.soinside.com 2019 - 2024. All rights reserved.