创建一个工具提示矩形并检查矩形何时离开屏幕

Creating a Tooltip rectangle and checking when the rectangle leaves the screen

我正在创建工具提示 class。通常,会在鼠标位置上方绘制一个矩形。如果矩形碰到顶部或右侧,则矩形将绘制在相反的一侧。这是我的代码:

private int LeavesScreen(Rectangle rectangle)
        {
            const int None = 0;
            const int Top = 1;
            const int Right = 2;

            if (rectangle.Y < Viewport.Y)
                return Top;

            else if (rectangle.Right > Viewport.Right)
                return Right;

            return None;
        }
public void Update(GameTime gameTime)
{
    switch (LeavesScreen(Rect))
        {
            case 1: //Hits the top side of the screen
                Destination = new Vector2(Rect.X, Rect.Y + Rect.Height);
                Direction = Destination - new Vector2(Rect.X, Rect.Y);
                Direction.Normalize();
                break;
            case 2: //Hits the right side of the screen
                Destination = new Vector2(Rect.X - Rect.Width, Rect.Y);
                Direction = Destination - new Vector2(Rect.X, Rect.Y);
                Direction.Normalize();
                break;
            case 0:
                Direction = Vector2.Zero;
                break;
        }
        Position += Direction;
        Rect = new Rectangle((int)Position.X, (int)Position.Y - Size.Width, Size.Width, Size.Height);
}

这样做会导致矩形上下跳动,我猜这是因为当矩形碰到边缘时,它会移动,移动后检测到它没有碰到边缘然后returns 到之前的位置,再次重复。

注意: 我这样写:(int)Position.Y - Size.Width 以便工具提示显示在鼠标上方,而不是下方。

我尝试在 case:0 中添加这个,但是,它在屏幕的右上半部分有效:

if (Viewport.Right - Rect.Right > Rect.Width + Viewport.Width * 0.02f)
                    Direction = Vector2.Zero;
if (Viewport.Top - Rect.Top < -Rect.Height - Viewport.Height * 0.02f)
                    Direction = Vector2.Zero;

编辑:如果我在屏幕的右上角或右下角尝试有什么区别?上侧有效,下侧无效。 注 2:使用 0.02f 所以在它再次开始检测碰撞之前有一个空 space

通过采用不同的方法解决了该问题。我没有尝试将矩形重新定位到鼠标的另一侧,而是完全阻止矩形离开屏幕。

尝试设置工具提示的 Vector2 Position 时,它会检查它是否离开边界。 像这样:

public void SetPosition(Vector2 newPosition)
{
    //We remove Size.Height to make the tooltip appear above the mouse, not on top of it
    newPosition = new Vector2(newPosition.X, newPosition.Y - Size.Height);
    string LeavesBoundary = LeavesScreen(new Rectangle((int)newPosition.X, (int)newPosition.Y, (int)this.Size.Width, (int)this.Size.Height));
    if (LeavesBoundary == "top")
    {
        this.Position = new Vector2(newPosition.X, Viewport.Top);
    }
    else if (LeavesBoundary == "right")
    {
        this.Position = new Vector2(Viewport.Right - this.Size.Width, newPosition.Y);
    }
    else if (LeavesBoundary == "topRight")
    {
        this.Position = new Vector2(Viewport.Right - this.Size.Width, Viewport.Top);
    }
    else
        this.Position = newPosition;
}

这里是更新后的 LeavesScreen 方法:

private string LeavesScreen(Rectangle rectangle)
{
    if (rectangle.Top < Viewport.Top)
    {
        //If the rectangle leaves the top part, create a new rectangle and see if it leaves the right part too. If it does, return "topRight"
        Rectangle x = new Rectangle(rectangle.X, rectangle.Y + rectangle.Height, rectangle.Width, rectangle.Height);
        if (x.Right > Viewport.Right)
            return "topRight";
        return "top";
    }

    if (rectangle.Right > Viewport.Right)
    {
        //Same as above, if it leaves right side, check if it leaves top side too.
        Rectangle x = new Rectangle(rectangle.X - rectangle.Width, rectangle.Y, rectangle.Width, rectangle.Height);
        if (x.Y < Viewport.Y)
            return "topRight";
        return "right";
    }
    return "none";
}