private object CreateLayerObject(NamedObjectSave namedObjectSave, object returnObject)
{
Layer newLayer = SpriteManager.AddLayer();
// SpriteManager.MoveToFront(newLayer);
newLayer.Name = namedObjectSave.InstanceName;
if (namedObjectSave.Is2D)
{
newLayer.UsePixelCoordinates();
if (namedObjectSave.DestinationRectangle.HasValue)
{
var rectangle = namedObjectSave.DestinationRectangle.Value;
if (namedObjectSave.LayerCoordinateUnit == LayerCoordinateUnit.Pixel)
{
newLayer.LayerCameraSettings.LeftDestination = rectangle.X;
newLayer.LayerCameraSettings.RightDestination = rectangle.X + rectangle.Width;
newLayer.LayerCameraSettings.TopDestination = rectangle.Y;
newLayer.LayerCameraSettings.BottomDestination = rectangle.Y + rectangle.Height;
newLayer.LayerCameraSettings.OrthogonalWidth = newLayer.LayerCameraSettings.RightDestination -
newLayer.LayerCameraSettings.LeftDestination;
newLayer.LayerCameraSettings.OrthogonalHeight = newLayer.LayerCameraSettings.BottomDestination -
newLayer.LayerCameraSettings.TopDestination;
}
else if (namedObjectSave.LayerCoordinateUnit == LayerCoordinateUnit.Percent)
{
newLayer.LayerCameraSettings.LeftDestination = FlatRedBall.Math.MathFunctions.RoundToInt(rectangle.X * SpriteManager.Camera.DestinationRectangle.Width * .01f);
newLayer.LayerCameraSettings.RightDestination = FlatRedBall.Math.MathFunctions.RoundToInt((rectangle.X + rectangle.Width) * SpriteManager.Camera.DestinationRectangle.Width * .01f);
newLayer.LayerCameraSettings.TopDestination = FlatRedBall.Math.MathFunctions.RoundToInt(rectangle.Y * SpriteManager.Camera.DestinationRectangle.Height * .01f);
newLayer.LayerCameraSettings.BottomDestination = FlatRedBall.Math.MathFunctions.RoundToInt((rectangle.Y + rectangle.Height) * SpriteManager.Camera.DestinationRectangle.Height * .01f);
newLayer.LayerCameraSettings.OrthogonalWidth = SpriteManager.Camera.OrthogonalWidth * (rectangle.Width) / 100.0f;
newLayer.LayerCameraSettings.OrthogonalHeight = SpriteManager.Camera.OrthogonalHeight * (rectangle.Height) / 100.0f;
}
else
{
throw new NotImplementedException();
}
if (newLayer.LayerCameraSettings.RightDestination > SpriteManager.Camera.DestinationRectangle.Width)
{
MessageBox.Show("The Layer named " + namedObjectSave.InstanceName +
" has a right-side of " + newLayer.LayerCameraSettings.RightDestination +
" but the resolution width is only " + SpriteManager.Camera.DestinationRectangle.Width +
". Setting the Layer's Right to the Camera's width. This may cause crashes in your game, so you will want to change this value",
"Layer out of screen");
newLayer.LayerCameraSettings.RightDestination = SpriteManager.Camera.DestinationRectangle.Width ;
}
if (newLayer.LayerCameraSettings.BottomDestination > SpriteManager.Camera.DestinationRectangle.Bottom)
{
MessageBox.Show("The Layer named " + namedObjectSave.InstanceName +
" has a bottom-side of " + newLayer.LayerCameraSettings.BottomDestination +
" but the resolution height is only " + SpriteManager.Camera.DestinationRectangle.Height +
". Setting the Layer's Bottom to the Camera's height. This may cause crashes in your game, so you will want to change this value",
"Layer out of screen");
newLayer.LayerCameraSettings.BottomDestination = SpriteManager.Camera.DestinationRectangle.Bottom;
}
}
if (namedObjectSave.LayerCoordinateType == LayerCoordinateType.MatchCamera)
{
if (namedObjectSave.DestinationRectangle.HasValue)
{
var rectangle = namedObjectSave.DestinationRectangle.Value;
float ratioX = 0;
float ratioY = 0;
if (namedObjectSave.LayerCoordinateUnit == LayerCoordinateUnit.Pixel)
{
ratioX = rectangle.Width / (float)SpriteManager.Camera.DestinationRectangle.Width;
ratioY = rectangle.Height / (float)SpriteManager.Camera.DestinationRectangle.Height;
}
else
{
ratioX = rectangle.Width / 100.0f;
ratioY = rectangle.Height / 100.0f;
}
newLayer.LayerCameraSettings.OrthogonalWidth = SpriteManager.Camera.OrthogonalWidth * ratioX;
newLayer.LayerCameraSettings.OrthogonalHeight = SpriteManager.Camera.OrthogonalHeight * ratioY;
}
else
{
newLayer.LayerCameraSettings.OrthogonalWidth = SpriteManager.Camera.OrthogonalWidth;
newLayer.LayerCameraSettings.OrthogonalHeight = SpriteManager.Camera.OrthogonalHeight;
}
}
}
returnObject = newLayer;
return returnObject;
}