Reduce duplicate code in Sprite constructors.

This commit is contained in:
itchyOwl
2018-04-06 15:56:32 +03:00
parent 1d30cbfac7
commit 9fc65298e8
2 changed files with 23 additions and 33 deletions
@@ -110,13 +110,13 @@ namespace Barotrauma
t = new Texture2D(graphicsDevice, 1, 1); t = new Texture2D(graphicsDevice, 1, 1);
t.SetData(new Color[] { Color.White });// fill the texture with white t.SetData(new Color[] { Color.White });// fill the texture with white
submarineIcon = new Sprite("Content/UI/uiIcons.png", new Rectangle(0, 192, 64, 64), null); submarineIcon = new Sprite("Content/UI/uiIcons.png", new Rectangle(0, 192, 64, 64));
submarineIcon.Origin = submarineIcon.size / 2; submarineIcon.Origin = submarineIcon.size / 2;
arrow = new Sprite("Content/UI/uiIcons.png", new Rectangle(80, 240, 16, 16), null); arrow = new Sprite("Content/UI/uiIcons.png", new Rectangle(80, 240, 16, 16));
arrow.Origin = arrow.size / 2; arrow.Origin = arrow.size / 2;
SpeechBubbleIcon = new Sprite("Content/UI/uiIcons.png", new Rectangle(0, 129, 65, 61), null); SpeechBubbleIcon = new Sprite("Content/UI/uiIcons.png", new Rectangle(0, 129, 65, 61));
SpeechBubbleIcon.Origin = SpeechBubbleIcon.size / 2; SpeechBubbleIcon.Origin = SpeechBubbleIcon.size / 2;
} }
@@ -26,7 +26,7 @@ namespace Barotrauma
public float rotation; public float rotation;
public SpriteEffects effects; public SpriteEffects effects = SpriteEffects.None;
protected float depth; protected float depth;
@@ -61,6 +61,7 @@ namespace Barotrauma
partial void LoadTexture(ref Vector4 sourceVector, ref bool shouldReturn); partial void LoadTexture(ref Vector4 sourceVector, ref bool shouldReturn);
partial void CalculateSourceRect(); partial void CalculateSourceRect();
// TODO: use the Init method below?
public Sprite(XElement element, string path = "", string file = "") public Sprite(XElement element, string path = "", string file = "")
{ {
if (file == "") if (file == "")
@@ -106,50 +107,39 @@ namespace Barotrauma
public Sprite(string newFile, Vector2 newOrigin) public Sprite(string newFile, Vector2 newOrigin)
{ {
file = newFile; Init(newFile, newOrigin: newOrigin);
Vector4 sourceVector = Vector4.Zero;
bool shouldReturn = false;
LoadTexture(ref sourceVector, ref shouldReturn);
if (shouldReturn) return;
CalculateSourceRect();
size = new Vector2(sourceRect.Width, sourceRect.Height);
origin = new Vector2((float)sourceRect.Width * newOrigin.X, (float)sourceRect.Height * newOrigin.Y);
effects = SpriteEffects.None;
list.Add(this);
} }
public Sprite(string newFile, Rectangle? sourceRectangle, Vector2? newOffset, float newRotation = 0.0f) public Sprite(string newFile, Rectangle? sourceRectangle, Vector2? newOffset = null, float newRotation = 0)
{
Init(newFile, sourceRectangle: sourceRectangle, newOffset: newOffset, newRotation: newRotation);
}
private void Init(string newFile, Rectangle? sourceRectangle = null, Vector2? newOrigin = null, Vector2? newOffset = null, float newRotation = 0)
{ {
file = newFile; file = newFile;
Vector4 sourceVector = Vector4.Zero; Vector4 sourceVector = Vector4.Zero;
bool shouldReturn = false; bool shouldReturn = false;
LoadTexture(ref sourceVector, ref shouldReturn); LoadTexture(ref sourceVector, ref shouldReturn);
if (shouldReturn) return; if (shouldReturn) return;
if (sourceRectangle.HasValue)
if (sourceRectangle != null)
{ {
sourceRect = (Rectangle)sourceRectangle; sourceRect = sourceRectangle.Value;
} }
else else
{ {
CalculateSourceRect(); CalculateSourceRect();
} }
offset = newOffset ?? Vector2.Zero;
offset = newOffset ?? Vector2.Zero; if (newOrigin.HasValue)
{
size = new Vector2(sourceRect.Width, sourceRect.Height); origin = new Vector2(sourceRect.Width * newOrigin.Value.X, sourceRect.Height * newOrigin.Value.Y);
}
origin = Vector2.Zero;
rotation = newRotation; rotation = newRotation;
if (!list.Contains(this))
list.Add(this); {
list.Add(this);
}
} }
public void Remove() public void Remove()