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

时间:2015-01-31 01:17:18

标签: c# monogame

我正在创建一个Tooltip类。通常,将在鼠标位置的顶部绘制一个矩形。如果矩形击中顶侧或右侧,则矩形将在相对侧绘制。这是我的代码:

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

这样做会导致矩形上下跳动,我想这是因为当矩形撞到边缘时,它会被移动,移动后它会检测到它没有碰到边缘然后返回到前一个位置,这一切都重演了。

注意: 我写这个:(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因此在开始再次检测到碰撞之前有一个空格

1 个答案:

答案 0 :(得分:0)

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

尝试设置工具提示的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";
}