Add UnionRectangles extension method.
This commit is contained in:
@@ -151,6 +151,26 @@ namespace OpenRA
|
|||||||
return xs.ElementAt(r.Next(xs.Count));
|
return xs.ElementAt(r.Next(xs.Count));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Rectangle Union(this IEnumerable<Rectangle> rects)
|
||||||
|
{
|
||||||
|
// PERF: Avoid LINQ.
|
||||||
|
var first = true;
|
||||||
|
var result = Rectangle.Empty;
|
||||||
|
foreach (var rect in rects)
|
||||||
|
{
|
||||||
|
if (first)
|
||||||
|
{
|
||||||
|
first = false;
|
||||||
|
result = rect;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = Rectangle.Union(rect, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public static float Product(this IEnumerable<float> xs)
|
public static float Product(this IEnumerable<float> xs)
|
||||||
{
|
{
|
||||||
return xs.Aggregate(1f, (a, x) => a * x);
|
return xs.Aggregate(1f, (a, x) => a * x);
|
||||||
|
|||||||
@@ -219,31 +219,6 @@ namespace OpenRA.Traits
|
|||||||
return partitionedRenderableFrozenActors[p].InBox(RectWithCorners(a, b)).Where(frozenActorIsValid);
|
return partitionedRenderableFrozenActors[p].InBox(RectWithCorners(a, b)).Where(frozenActorIsValid);
|
||||||
}
|
}
|
||||||
|
|
||||||
Rectangle AggregateBounds(IEnumerable<Rectangle> screenBounds)
|
|
||||||
{
|
|
||||||
if (!screenBounds.Any())
|
|
||||||
return Rectangle.Empty;
|
|
||||||
|
|
||||||
var bounds = screenBounds.First();
|
|
||||||
foreach (var b in screenBounds.Skip(1))
|
|
||||||
bounds = Rectangle.Union(bounds, b);
|
|
||||||
|
|
||||||
return bounds;
|
|
||||||
}
|
|
||||||
|
|
||||||
Rectangle AggregateBounds(IEnumerable<int2> vertices)
|
|
||||||
{
|
|
||||||
if (!vertices.Any())
|
|
||||||
return Rectangle.Empty;
|
|
||||||
|
|
||||||
var first = vertices.First();
|
|
||||||
var rect = new Rectangle(first.X, first.Y, 0, 0);
|
|
||||||
foreach (var v in vertices.Skip(1))
|
|
||||||
rect = Rectangle.Union(rect, new Rectangle(v.X, v.Y, 0, 0));
|
|
||||||
|
|
||||||
return rect;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void TickRender()
|
public void TickRender()
|
||||||
{
|
{
|
||||||
foreach (var a in addOrUpdateActors)
|
foreach (var a in addOrUpdateActors)
|
||||||
@@ -261,7 +236,7 @@ namespace OpenRA.Traits
|
|||||||
else
|
else
|
||||||
partitionedMouseActors.Remove(a);
|
partitionedMouseActors.Remove(a);
|
||||||
|
|
||||||
var screenBounds = AggregateBounds(a.ScreenBounds(worldRenderer));
|
var screenBounds = a.ScreenBounds(worldRenderer).Union();
|
||||||
if (!screenBounds.Size.IsEmpty)
|
if (!screenBounds.Size.IsEmpty)
|
||||||
{
|
{
|
||||||
if (partitionedRenderableActors.Contains(a))
|
if (partitionedRenderableActors.Contains(a))
|
||||||
@@ -298,7 +273,7 @@ namespace OpenRA.Traits
|
|||||||
else
|
else
|
||||||
partitionedMouseFrozenActors[kv.Key].Remove(fa);
|
partitionedMouseFrozenActors[kv.Key].Remove(fa);
|
||||||
|
|
||||||
var screenBounds = AggregateBounds(fa.ScreenBounds);
|
var screenBounds = fa.ScreenBounds.Union();
|
||||||
if (!screenBounds.Size.IsEmpty)
|
if (!screenBounds.Size.IsEmpty)
|
||||||
{
|
{
|
||||||
if (partitionedRenderableFrozenActors[kv.Key].Contains(fa))
|
if (partitionedRenderableFrozenActors[kv.Key].Contains(fa))
|
||||||
|
|||||||
@@ -324,12 +324,7 @@ namespace OpenRA.Mods.Common.Graphics
|
|||||||
if (ShadowStart > 0)
|
if (ShadowStart > 0)
|
||||||
boundSprites = boundSprites.Concat(SpriteBounds(sprites, Frames, ShadowStart, Facings, Length, Stride, transpose));
|
boundSprites = boundSprites.Concat(SpriteBounds(sprites, Frames, ShadowStart, Facings, Length, Stride, transpose));
|
||||||
|
|
||||||
if (boundSprites.Any())
|
Bounds = boundSprites.Union();
|
||||||
{
|
|
||||||
Bounds = boundSprites.First();
|
|
||||||
foreach (var b in boundSprites.Skip(1))
|
|
||||||
Bounds = Rectangle.Union(Bounds, b);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (FormatException f)
|
catch (FormatException f)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -92,12 +92,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
// If this is a problem, then we may need to fetch the area from somewhere else
|
// If this is a problem, then we may need to fetch the area from somewhere else
|
||||||
var r = previews.SelectMany(p => p.ScreenBounds(worldRenderer, CenterPosition));
|
var r = previews.SelectMany(p => p.ScreenBounds(worldRenderer, CenterPosition));
|
||||||
|
|
||||||
if (r.Any())
|
Bounds = r.Union();
|
||||||
{
|
|
||||||
Bounds = r.First();
|
|
||||||
foreach (var rr in r.Skip(1))
|
|
||||||
Bounds = Rectangle.Union(Bounds, rr);
|
|
||||||
}
|
|
||||||
|
|
||||||
SelectionBox = new SelectionBoxRenderable(new WPos(CenterPosition.X, CenterPosition.Y, 8192),
|
SelectionBox = new SelectionBoxRenderable(new WPos(CenterPosition.X, CenterPosition.Y, 8192),
|
||||||
new Rectangle(Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height), Color.White);
|
new Rectangle(Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height), Color.White);
|
||||||
|
|||||||
@@ -53,20 +53,10 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
// Calculate the preview bounds
|
// Calculate the preview bounds
|
||||||
PreviewOffset = int2.Zero;
|
|
||||||
IdealPreviewSize = int2.Zero;
|
|
||||||
|
|
||||||
var r = preview.SelectMany(p => p.ScreenBounds(worldRenderer, WPos.Zero));
|
var r = preview.SelectMany(p => p.ScreenBounds(worldRenderer, WPos.Zero));
|
||||||
|
var b = r.Union();
|
||||||
if (r.Any())
|
IdealPreviewSize = new int2(b.Width, b.Height);
|
||||||
{
|
PreviewOffset = -new int2(b.Left, b.Top) - IdealPreviewSize / 2;
|
||||||
var b = r.First();
|
|
||||||
foreach (var rr in r.Skip(1))
|
|
||||||
b = Rectangle.Union(b, rr);
|
|
||||||
|
|
||||||
IdealPreviewSize = new int2(b.Width, b.Height);
|
|
||||||
PreviewOffset = -new int2(b.Left, b.Top) - IdealPreviewSize / 2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IFinalizedRenderable[] renderables;
|
IFinalizedRenderable[] renderables;
|
||||||
|
|||||||
@@ -425,7 +425,7 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
DisplayedIconCount++;
|
DisplayedIconCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
eventBounds = icons.Any() ? icons.Keys.Aggregate(Rectangle.Union) : Rectangle.Empty;
|
eventBounds = icons.Keys.Union();
|
||||||
|
|
||||||
if (oldIconCount != DisplayedIconCount)
|
if (oldIconCount != DisplayedIconCount)
|
||||||
OnIconCountChanged(oldIconCount, DisplayedIconCount);
|
OnIconCountChanged(oldIconCount, DisplayedIconCount);
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
IconCount++;
|
IconCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
eventBounds = icons.Any() ? icons.Keys.Aggregate(Rectangle.Union) : Rectangle.Empty;
|
eventBounds = icons.Keys.Union();
|
||||||
|
|
||||||
if (oldIconCount != IconCount)
|
if (oldIconCount != IconCount)
|
||||||
OnIconCountChanged(oldIconCount, IconCount);
|
OnIconCountChanged(oldIconCount, IconCount);
|
||||||
|
|||||||
Reference in New Issue
Block a user