Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaShared/Source/Sprite/Sprite.cs
Joonas Rikkonen 110d803b78 5d2c9f2...bdbcef4
commit bdbcef41bfbc42beeb2d942e7db431b8ab627f1c
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Thu Mar 7 13:31:02 2019 +0200

    Simplify the sprite related commands.

commit 49c8e1cd01953ae31cff99bf35432ab887b45d62
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Thu Mar 7 13:05:20 2019 +0200

    Rename "Reload Texture" button as "Reload Sprite", because it actually reloads both the xml and the texture. Add the button text into the localization file.

commit 84f58d9e0bb46f29412f936d569348e46227c9d5
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Thu Mar 7 12:50:51 2019 +0200

    Add the option to reload xml/texture/both for items when they are hovered over with the mouse. dry.

commit 732084c044e6a2c220cee8d5c1d9b5e4ce05587d
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Thu Mar 7 12:48:49 2019 +0200

    Fix the sprite name of the right hand in the assistant job gear.

commit 30344369d7e87a17312cc839f195c36c77c4a3b2
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Thu Mar 7 12:26:20 2019 +0200

    Fix ReloadXML failing when multiple source elements are found.

commit ddb6fb48b6e0789b18a1ea889dcdf408fcbb5b12
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Thu Mar 7 11:47:13 2019 +0200

    Fix damagemodifier multiplier not applied on particles if no sound is played.

commit 41206013be41d240b12dad48f4ddaba9b072742b
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Thu Mar 7 11:46:22 2019 +0200

    Fix engigear lower arm sprite name.

commit 8ce938305206ca65aeb00515639e799d9f2fd526
Merge: e6166d2ef 61703e8af
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Wed Mar 6 18:53:36 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit e6166d2ef24489bac6f217211849884013dbf4e6
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Wed Mar 6 18:53:30 2019 +0200

    Implement husk sprite overlays as wearables. Add new husk injector (wip).

commit 367c4f5b3784c65c3c26807dd6c10e837960c6bb
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Wed Mar 6 18:52:15 2019 +0200

    Add console commands for changing the gender and race of the character.

commit 61703e8af274c8f5d9e05575ebaf13c9bde8855f
Author: Iiro Enges <iiro@fakefish.fi>
Date:   Wed Mar 6 17:47:09 2019 +0200

    Refined environment item scales and colors
2019-03-18 22:31:14 +02:00

266 lines
10 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using Barotrauma.Extensions;
using System.IO;
namespace Barotrauma
{
public partial class Sprite
{
public static IEnumerable<Sprite> LoadedSprites
{
get { return list; }
}
private static HashSet<Sprite> list = new HashSet<Sprite>();
/// <summary>
/// Reference to the xml element from where the sprite was created. Can be null if the sprite was not defined in xml!
/// </summary>
public XElement SourceElement { get; private set; }
//the area in the texture that is supposed to be drawn
private Rectangle sourceRect;
//the offset used when drawing the sprite
protected Vector2 offset;
protected Vector2 origin;
//the size of the drawn sprite, if larger than the source,
//the sprite is tiled to fill the target size
public Vector2 size;
public float rotation;
public SpriteEffects effects = SpriteEffects.None;
protected float depth;
public Rectangle SourceRect
{
get { return sourceRect; }
set { sourceRect = value; }
}
public float Depth
{
get { return depth; }
set { depth = MathHelper.Clamp(value, 0.001f, 0.999f); }
}
/// <summary>
/// In pixels
/// </summary>
public Vector2 Origin
{
get { return origin; }
set
{
origin = value;
_relativeOrigin = new Vector2(origin.X / sourceRect.Width, origin.Y / sourceRect.Height);
}
}
private Vector2 _relativeOrigin;
/// <summary>
/// 0 - 1
/// </summary>
public Vector2 RelativeOrigin
{
get => _relativeOrigin;
set
{
_relativeOrigin = value;
origin = new Vector2(_relativeOrigin.X * sourceRect.Width, _relativeOrigin.Y * sourceRect.Height);
}
}
public string FilePath { get; private set; }
public string FullPath { get; private set; }
public override string ToString()
{
return FilePath + ": " + sourceRect;
}
public string ID { get; private set; }
/// <summary>
/// ID of the Map Entity so that we can link the sprite to it's owner.
/// </summary>
public string EntityID { get; set; }
public string Name { get; set; }
partial void LoadTexture(ref Vector4 sourceVector, ref bool shouldReturn, bool premultiplyAlpha = true);
partial void CalculateSourceRect();
public Sprite(XElement element, string path = "", string file = "")
{
SourceElement = element;
if (file == "")
{
file = SourceElement.GetAttributeString("texture", "");
}
if (file == "")
{
DebugConsole.ThrowError("Sprite " + SourceElement + " doesn't have a texture specified!");
return;
}
if (!string.IsNullOrEmpty(path))
{
if (!path.EndsWith("/")) path += "/";
}
FilePath = path + file;
if (!string.IsNullOrEmpty(FilePath))
{
FullPath = Path.GetFullPath(FilePath);
}
Name = SourceElement.GetAttributeString("name", null);
Vector4 sourceVector = SourceElement.GetAttributeVector4("sourcerect", Vector4.Zero);
bool shouldReturn = false;
LoadTexture(ref sourceVector, ref shouldReturn, SourceElement.GetAttributeBool("premultiplyalpha", true));
if (shouldReturn) return;
sourceRect = new Rectangle((int)sourceVector.X, (int)sourceVector.Y, (int)sourceVector.Z, (int)sourceVector.W);
size = SourceElement.GetAttributeVector2("size", Vector2.One);
size.X *= sourceRect.Width;
size.Y *= sourceRect.Height;
RelativeOrigin = SourceElement.GetAttributeVector2("origin", new Vector2(0.5f, 0.5f));
Depth = SourceElement.GetAttributeFloat("depth", 0.001f);
ID = GetID(SourceElement);
list.Add(this);
}
internal void LoadParams(SpriteParams spriteParams, bool isFlipped)
{
SourceElement = spriteParams.Element;
sourceRect = spriteParams.SourceRect;
RelativeOrigin = spriteParams.Origin;
if (isFlipped)
{
Origin = new Vector2(sourceRect.Width - origin.X, origin.Y);
}
depth = spriteParams.Depth;
// TODO: size?
}
public Sprite(string newFile, Vector2 newOrigin, bool preMultiplyAlpha = true)
{
Init(newFile, newOrigin: newOrigin, preMultiplyAlpha: preMultiplyAlpha);
list.Add(this);
}
public Sprite(string newFile, Rectangle? sourceRectangle, Vector2? origin = null, float rotation = 0, bool preMultiplyAlpha = true)
{
Init(newFile, sourceRectangle: sourceRectangle, newOrigin: origin, newRotation: rotation, preMultiplyAlpha: preMultiplyAlpha);
list.Add(this);
}
private void Init(string newFile, Rectangle? sourceRectangle = null, Vector2? newOrigin = null, Vector2? newOffset = null, float newRotation = 0,
bool preMultiplyAlpha = true)
{
FilePath = newFile;
if (!string.IsNullOrEmpty(FilePath))
{
FullPath = Path.GetFullPath(FilePath);
}
Vector4 sourceVector = Vector4.Zero;
bool shouldReturn = false;
LoadTexture(ref sourceVector, ref shouldReturn, preMultiplyAlpha);
if (shouldReturn) return;
if (sourceRectangle.HasValue)
{
sourceRect = sourceRectangle.Value;
}
else
{
CalculateSourceRect();
}
offset = newOffset ?? Vector2.Zero;
if (newOrigin.HasValue)
{
RelativeOrigin = newOrigin.Value;
}
size = new Vector2(sourceRect.Width, sourceRect.Height);
rotation = newRotation;
}
/// <summary>
/// Creates a supposedly unique id from the parent element. If the parent element is not found, uses the sprite element.
/// TODO: If there are multiple elements with exactly the same data, the ids will fail. -> Is there a better way to identify the sprites?
/// </summary>
public static string GetID(XElement sourceElement)
{
if (sourceElement == null) { return string.Empty; }
var parentElement = sourceElement.Parent;
return parentElement != null ? sourceElement.ToString() + parentElement.ToString() : sourceElement.ToString();
}
public void Remove()
{
list.Remove(this);
DisposeTexture();
}
partial void DisposeTexture();
/// <summary>
/// Works only if there is a name attribute defined for the sprite. For items and structures, the entity id or name is used if the sprite's name attribute is not defined.
/// </summary>
public void ReloadXML()
{
if (SourceElement == null) { return; }
string path = SourceElement.ParseContentPathFromUri();
if (string.IsNullOrWhiteSpace(path))
{
DebugConsole.NewMessage($"[Sprite] Could not parse the content path from the source element ({SourceElement}) uri: {SourceElement.BaseUri}", Color.Yellow);
return;
}
var doc = XMLExtensions.TryLoadXml(path);
if (doc == null || doc.Root == null) { return; }
if (string.IsNullOrWhiteSpace(Name) && string.IsNullOrWhiteSpace(EntityID)) { return; }
var spriteElements = doc.Descendants("sprite").Concat(doc.Descendants("Sprite"));
var sourceElements = spriteElements.Where(e => e.GetAttributeString("name", null) == Name);
if (sourceElements.None())
{
// Try parents by first comparing the entity id and then the name, if no match was found.
sourceElements = spriteElements.Where(e => e.Parent?.GetAttributeString("identifier", null) == EntityID);
if (sourceElements.None())
{
sourceElements = spriteElements.Where(e => e.Parent?.GetAttributeString("name", null) == Name);
}
}
if (sourceElements.Multiple())
{
DebugConsole.NewMessage($"[Sprite] Multiple matching elements found by name ({Name}) or identifier ({EntityID})!: {SourceElement.ToString()}", Color.Yellow);
}
else if (sourceElements.None())
{
DebugConsole.NewMessage($"[Sprite] Cannot find matching source element by comparing the name attribute ({Name}) or identifier ({EntityID})! Cannot reload the xml for sprite element \"{SourceElement.ToString()}\"!", Color.Yellow);
}
else
{
SourceElement = sourceElements.Single();
}
if (SourceElement != null)
{
Vector4 sourceVector = SourceElement.GetAttributeVector4("sourcerect", Vector4.Zero);
sourceRect = new Rectangle((int)sourceVector.X, (int)sourceVector.Y, (int)sourceVector.Z, (int)sourceVector.W);
size = SourceElement.GetAttributeVector2("size", Vector2.One);
size.X *= sourceRect.Width;
size.Y *= sourceRect.Height;
RelativeOrigin = SourceElement.GetAttributeVector2("origin", new Vector2(0.5f, 0.5f));
Depth = SourceElement.GetAttributeFloat("depth", 0.001f);
ID = GetID(SourceElement);
}
}
}
}