private void AlignScreenSurfaces()
{
if (surfaces.Count == 0) return;
var placeable = new HashSet<string>();
var orphans = new List<string>();
int oldHorizScroll = this.HorizontalScroll.Value;
int oldVertScroll = this.VerticalScroll.Value;
this.HorizontalScroll.Value = 0;
this.VerticalScroll.Value = 0;
int minX = 0, minY = 0, maxX = 0, maxY = 0;
foreach (var surface in surfaces.Values)
{
surface.Placed = false;
}
var startScreen = surfaces.Values.First();
if (surfaces.ContainsKey(stage.StartScreen))
{
startScreen = surfaces[stage.StartScreen];
}
// lay the screens out like a deep graph traversal
LayoutFromScreen(startScreen, new Point(0, 0));
// any remaining disconnected screens
foreach (var surface in surfaces.Values.Where(s => !s.Placed))
{
LayoutFromScreen(surface, new Point(0, 0));
}
foreach (var surface in surfaces.Values)
{
minX = Math.Min(minX, surface.Location.X);
minY = Math.Min(minY, surface.Location.Y);
}
if (minX < -this.HorizontalScroll.Value || minY < -this.VerticalScroll.Value)
{
// now readjust to all positive locations
foreach (var surface in surfaces.Values)
{
surface.Location = new Point(surface.Location.X - minX - this.HorizontalScroll.Value, surface.Location.Y - minY - this.VerticalScroll.Value);
}
}
foreach (var surface in surfaces.Values)
{
maxX = Math.Max(maxX, surface.Right);
maxY = Math.Max(maxY, surface.Bottom);
}
joinOverlay.Refresh(maxX + 20, maxY + 20, stage.Joins, surfaces);
joinOverlay.Visible = MainForm.Instance.DrawJoins;
this.HorizontalScroll.Value = oldHorizScroll;
this.VerticalScroll.Value = oldVertScroll;
foreach (var surfacePair in surfaces)
{
surfaceLocations[surfacePair.Key] = surfacePair.Value.Location;
}
}