【发布时间】:2011-02-14 21:13:51
【问题描述】:
所以我有这个面板类。它有点像一个窗口,您可以在其中调整大小、关闭、添加按钮、滑块等。如果您还记得的话,它就像 Morrowind 中的状态屏幕。我想要的行为是,当一个精灵在面板的边界之外时,它不会被绘制,如果它部分在外面,只有里面的部分会被绘制。
所以它现在要做的是首先获取一个表示面板边界的矩形,以及一个用于精灵的矩形,它找到两者之间的相交矩形,然后将该相交转换为精灵矩形的局部坐标并使用 that 为源矩形。它的工作原理和我觉得代码一样聪明,我无法摆脱这种感觉,即有更好的方法来做到这一点。此外,通过这种设置,我无法为我的 2D 相机使用全局变换矩阵,“世界”中的所有内容都必须通过相机参数进行绘制。无论如何,这是我的代码:
对于十字路口:
public static Rectangle? Intersection(Rectangle rectangle1, Rectangle rectangle2)
{
if (rectangle1.Intersects(rectangle2))
{
if (rectangle1.Contains(rectangle2))
{
return rectangle2;
}
else if (rectangle2.Contains(rectangle1))
{
return rectangle1;
}
else
{
int x = Math.Max(rectangle1.Left, rectangle2.Left);
int y = Math.Max(rectangle1.Top, rectangle2.Top);
int height = Math.Min(rectangle1.Bottom, rectangle2.Bottom) - Math.Max(rectangle1.Top, rectangle2.Top);
int width = Math.Min(rectangle1.Right, rectangle2.Right) - Math.Max(rectangle1.Left, rectangle2.Left);
return new Rectangle(x, y, width, height);
}
}
else
{
return null;
}
}
在面板上实际绘图:
public void DrawOnPanel(IDraw sprite, SpriteBatch spriteBatch)
{
Rectangle panelRectangle = new Rectangle(
(int)_position.X,
(int)_position.Y,
_width,
_height);
Rectangle drawRectangle = new Rectangle();
drawRectangle.X = (int)sprite.Position.X;
drawRectangle.Y = (int)sprite.Position.Y;
drawRectangle.Width = sprite.Width;
drawRectangle.Height = sprite.Height;
if (panelRectangle.Contains(drawRectangle))
{
sprite.Draw(
spriteBatch,
drawRectangle,
null);
}
else if (Intersection(panelRectangle, drawRectangle) == null)
{
return;
}
else if (Intersection(panelRectangle, drawRectangle).HasValue)
{
Rectangle intersection = Intersection(panelRectangle, drawRectangle).Value;
if (Intersection(panelRectangle, drawRectangle) == drawRectangle)
{
sprite.Draw(spriteBatch, intersection, intersection);
}
else
{
sprite.Draw(
spriteBatch,
intersection,
new Rectangle(
intersection.X - drawRectangle.X,
intersection.Y - drawRectangle.Y,
intersection.Width,
intersection.Height));
}
}
}
所以我想我的问题是,有没有更好的方法来做到这一点?
更新:刚刚发现了 ScissorRectangle 属性。这似乎是一种不错的方法;它需要制作一个 RasterizerState 对象并将其传递给接受它的 spritebatch.Begin 重载。似乎这可能是最好的选择。还有我显然可以改变的视口。想法? :)
【问题讨论】:
-
我建议您在问题中使用更明确的表述,类似于“如何在旋转的矩形上执行剪辑?”。
-
哦,哇,我什至没有考虑轮换。谢谢
标签: xna