1 using Microsoft.Xna.Framework;
8 using Microsoft.Xna.Framework.Graphics;
36 public Vector2
size = Vector2.One;
41 public SpriteEffects
effects = SpriteEffects.None;
48 get {
return sourceRect; }
49 set { sourceRect = value; }
55 set {
depth = MathHelper.Clamp(value, 0.001f, 0.999f); }
67 _relativeOrigin =
new Vector2(
origin.X / sourceRect.Width,
origin.Y / sourceRect.Height);
71 private Vector2 _relativeOrigin;
77 get => _relativeOrigin;
80 _relativeOrigin = value;
81 origin =
new Vector2(_relativeOrigin.X * sourceRect.Width, _relativeOrigin.Y * sourceRect.Height);
102 public string Name {
get;
set; }
104 partial
void LoadTexture(ref Vector4 sourceVector, ref
bool shouldReturn);
106 partial
void CalculateSourceRect();
108 static partial
void AddToList(
Sprite sprite);
110 public Sprite(
ContentXElement element,
string path =
"",
string file =
"",
bool lazyLoad =
false,
float sourceRectScale = 1)
114 DebugConsole.ThrowError($
"Sprite: xml element null in {file}. Failed to create the sprite!");
122 var overrideElement = GetLocalizationOverrideElement();
123 if (overrideElement !=
null && overrideElement.Attribute(
"sourcerect") !=
null)
125 sourceVector = overrideElement.GetAttributeVector4(
"sourcerect", Vector4.Zero);
127 if ((overrideElement ??
SourceElement).Attribute(
"sheetindex") !=
null)
129 Point sheetElementSize = (overrideElement ??
SourceElement).GetAttributePoint(
"sheetelementsize", Point.Zero);
130 Point sheetIndex = (overrideElement ??
SourceElement).GetAttributePoint(
"sheetindex", Point.Zero);
131 sourceVector =
new Vector4(sheetIndex.X * sheetElementSize.X, sheetIndex.Y * sheetElementSize.Y, sheetElementSize.X, sheetElementSize.Y);
134 bool shouldReturn =
false;
137 LoadTexture(ref sourceVector, ref shouldReturn);
139 if (shouldReturn) {
return; }
140 sourceRect =
new Rectangle((
int)(sourceVector.X * sourceRectScale), (
int)(sourceVector.Y * sourceRectScale), (
int)(sourceVector.Z * sourceRectScale), (
int)(sourceVector.W * sourceRectScale));
143 size.X *= sourceRect.Width;
144 size.Y *= sourceRect.Height;
153 internal void LoadParams(SpriteParams spriteParams,
bool isFlipped)
156 sourceRect = spriteParams.SourceRect;
162 depth = spriteParams.Depth;
165 public Sprite(
string newFile, Vector2 newOrigin)
167 Init(newFile, newOrigin: newOrigin);
173 Init(newFile, sourceRectangle: sourceRectangle, newOrigin:
origin, newRotation:
rotation);
177 private void Init(
string newFile, Rectangle? sourceRectangle =
null, Vector2? newOrigin =
null, Vector2? newOffset =
null,
float newRotation = 0)
180 Vector4 sourceVector = Vector4.Zero;
181 bool shouldReturn =
false;
182 LoadTexture(ref sourceVector, ref shouldReturn);
183 if (shouldReturn)
return;
184 if (sourceRectangle.HasValue)
186 sourceRect = sourceRectangle.Value;
190 CalculateSourceRect();
192 offset = newOffset ?? Vector2.Zero;
193 if (newOrigin.HasValue)
197 size =
new Vector2(sourceRect.Width, sourceRect.Height);
208 if (sourceElement ==
null) {
return "".ToIdentifier(); }
209 var parentElement = sourceElement.Parent;
210 return $
"{sourceElement}{parentElement?.ToString() ?? ""}".ToIdentifier();
213 static partial
void RemoveFromList(
Sprite sprite);
217 RemoveFromList(
this);
226 partial
void DisposeTexture();
235 if (
string.IsNullOrWhiteSpace(path))
237 DebugConsole.NewMessage($
"[Sprite] Could not parse the content path from the source element ({SourceElement}) uri: {SourceElement.BaseUri}", Color.Yellow);
240 var doc = XMLExtensions.TryLoadXml(path);
241 if (doc ==
null) {
return; }
242 if (
string.IsNullOrWhiteSpace(
Name) &&
string.IsNullOrWhiteSpace(
EntityIdentifier.Value)) {
return; }
243 var spriteElements = doc.Descendants(
"sprite").Concat(doc.Descendants(
"Sprite"));
244 var sourceElements = spriteElements.Where(e => e.GetAttributeString(
"name",
null) ==
Name);
245 if (sourceElements.None())
248 sourceElements = spriteElements.Where(e => e.Parent?.GetAttributeString(
"identifier",
null) ==
EntityIdentifier);
249 if (sourceElements.None())
251 sourceElements = spriteElements.Where(e => e.Parent?.GetAttributeString(
"name",
null) ==
Name);
254 if (sourceElements.Multiple())
256 DebugConsole.NewMessage($
"[Sprite] Multiple matching elements found by name ({Name}) or identifier ({EntityIdentifier})!: {SourceElement}", Color.Yellow);
258 else if (sourceElements.None())
260 DebugConsole.NewMessage($
"[Sprite] Cannot find matching source element by comparing the name attribute ({Name}) or identifier ({EntityIdentifier})! Cannot reload the xml for sprite element \"{SourceElement.ToString()}\"!", Color.Yellow);
269 var overrideElement = GetLocalizationOverrideElement();
270 if (overrideElement !=
null && overrideElement.Attribute(
"sourcerect") !=
null)
272 sourceRect = overrideElement.GetAttributeRect(
"sourcerect",
Rectangle.Empty);
274 if ((overrideElement ??
SourceElement).Attribute(
"sheetindex") !=
null)
276 Point sheetElementSize = (overrideElement ??
SourceElement).GetAttributePoint(
"sheetelementsize", Point.Zero);
277 Point sheetIndex = (overrideElement ??
SourceElement).GetAttributePoint(
"sheetindex", Point.Zero);
278 sourceRect =
new Rectangle(sheetIndex.X * sheetElementSize.X, sheetIndex.Y * sheetElementSize.Y, sheetElementSize.X, sheetElementSize.Y);
281 size.X *= sourceRect.Width;
282 size.Y *= sourceRect.Height;
300 var overrideElement = GetLocalizationOverrideElement();
301 if (overrideElement !=
null)
303 string overrideFile = overrideElement.GetAttributeStringUnrestricted(
"texture",
"");
304 if (!
string.IsNullOrEmpty(overrideFile)) { file = overrideFile; }
309 DebugConsole.ThrowError(
"Sprite " +
SourceElement.
Element +
" doesn't have a texture specified!",
313 if (!
string.IsNullOrEmpty(path))
315 if (!path.EndsWith(
"/")) path +=
"/";
321 private XElement GetLocalizationOverrideElement()
325 if (subElement.Name.ToString().Equals(
"override", StringComparison.OrdinalIgnoreCase))
327 LanguageIdentifier language = subElement.GetAttributeIdentifier(
"language",
"").ToLanguageIdentifier();
328 if (GameSettings.CurrentConfig.Language == language)
static ContentPath FromRaw(string? rawValue)
string? GetAttributeString(string key, string? def)
Vector4 GetAttributeVector4(string key, in Vector4 def)
float GetAttributeFloat(string key, float def)
ContentPackage? ContentPackage
string GetAttributeStringUnrestricted(string key, string def)
IEnumerable< ContentXElement > Elements()
string ParseContentPathFromUri()
readonly XElement Element
Vector2 GetAttributeVector2(string key, in Vector2 def)
bool GetAttributeBool(string key, bool def)
Rectangle GetAttributeRect(string key, in Rectangle def)
Vector2 RelativeOrigin
0 - 1
Sprite(string newFile, Rectangle? sourceRectangle, Vector2? origin=null, float rotation=0)
static Identifier GetIdentifier(XElement sourceElement)
Creates a supposedly unique identifier from the parent element. If the parent element is not found,...
Identifier EntityIdentifier
Identifier of the Map Entity so that we can link the sprite to its owner.
bool ParseTexturePath(string path="", string file="")
Sprite(string newFile, Vector2 newOrigin)
override string ToString()
Sprite(ContentXElement element, string path="", string file="", bool lazyLoad=false, float sourceRectScale=1)
ContentXElement SourceElement
Reference to the xml element from where the sprite was created. Can be null if the sprite was not def...
void ReloadXML()
Works only if there is a name attribute defined for the sprite. For items and structures,...