/// <summary> /// Creates a deep copy of the texture /// </summary> /// <returns></returns> public CompositeTexture Clone() { CompositeTexture[] alternatesClone = null; if (Alternates != null) { alternatesClone = new CompositeTexture[Alternates.Length]; for (int i = 0; i < alternatesClone.Length; i++) { alternatesClone[i] = Alternates[i].CloneWithoutAlternates(); } } CompositeTexture ct = new CompositeTexture() { Base = Base.Clone(), Alternates = alternatesClone, Rotation = Rotation, Alpha = Alpha }; if (Overlays != null) { ct.Overlays = new AssetLocation[Overlays.Length]; for (int i = 0; i < ct.Overlays.Length; i++) { ct.Overlays[i] = Overlays[i].Clone(); } } return(ct); }
internal CompositeTexture CloneWithoutAlternates() { CompositeTexture ct = new CompositeTexture() { Base = Base.Clone(), Rotation = Rotation }; if (Overlays != null) { ct.Overlays = new AssetLocation[Overlays.Length]; for (int i = 0; i < ct.Overlays.Length; i++) { ct.Overlays[i] = Overlays[i].Clone(); } } return(ct); }
/// <summary> /// Expands a CompositeTexture to a texture atlas friendly version and populates the Baked field /// </summary> /// <param name="assetManager"></param> /// <param name="ct"></param> /// <returns></returns> static BakedCompositeTexture Bake(IAssetManager assetManager, CompositeTexture ct) { BakedCompositeTexture bct = new BakedCompositeTexture(); bct.BakedName = ct.Base.Clone(); if (ct.Base.EndsWithWildCard) { List <IAsset> assets = assetManager.GetMany("textures/" + bct.BakedName.Path.Substring(0, bct.BakedName.Path.Length - 1), bct.BakedName.Domain); if (assets.Count == 0) { ct.Base = bct.BakedName = new AssetLocation("unknown"); } else if (assets.Count == 1) { ct.Base = assets[0].Location.CloneWithoutPrefixAndEnding("textures/".Length); bct.BakedName = ct.Base.Clone(); } else if (assets.Count > 1) { int origLength = (ct.Alternates == null ? 0 : ct.Alternates.Length); CompositeTexture[] alternates = new CompositeTexture[origLength + assets.Count - 1]; if (ct.Alternates != null) { Array.Copy(ct.Alternates, alternates, ct.Alternates.Length); } for (int i = 0; i < assets.Count; i++) { IAsset asset = assets[i]; AssetLocation newLocation = asset.Location.CloneWithoutPrefixAndEnding("textures/".Length); if (i == 0) { ct.Base = newLocation; bct.BakedName = newLocation.Clone(); } else { var act = alternates[origLength + i - 1] = new CompositeTexture(newLocation); act.Rotation = ct.Rotation; act.Alpha = ct.Alpha; } } ct.Alternates = alternates; } } if (ct.Overlays != null) { bct.TextureFilenames = new AssetLocation[ct.Overlays.Length + 1]; bct.TextureFilenames[0] = ct.Base.Clone(); for (int i = 0; i < ct.Overlays.Length; i++) { bct.TextureFilenames[i + 1] = ct.Overlays[i].Clone(); bct.BakedName.Path += "++" + ct.Overlays[i].ToShortString(); } } else { bct.TextureFilenames = new AssetLocation[] { ct.Base.Clone() }; } if (ct.Rotation != 0) { if (ct.Rotation != 90 && ct.Rotation != 180 && ct.Rotation != 270) { throw new Exception("Texture definition " + ct.Base + " has a rotation thats not 0, 90, 180 or 270. These are the only allowed values!"); } bct.BakedName.Path += "@" + ct.Rotation; } if (ct.Alpha != 255) { if (ct.Alpha < 0 || ct.Alpha > 255) { throw new Exception("Texture definition " + ct.Base + " has a alpha value outside the 0..255 range."); } bct.BakedName.Path += "å" + ct.Alpha; } if (ct.Alternates != null) { bct.BakedVariants = new BakedCompositeTexture[ct.Alternates.Length + 1]; bct.BakedVariants[0] = bct; for (int i = 0; i < ct.Alternates.Length; i++) { bct.BakedVariants[i + 1] = Bake(assetManager, ct.Alternates[i]); } } return(bct); }
/// <summary> /// Expands a CompositeTexture to a texture atlas friendly version and populates the Baked field /// </summary> /// <param name="assetManager"></param> /// <param name="ct"></param> /// <returns></returns> static BakedCompositeTexture Bake(IAssetManager assetManager, CompositeTexture ct) { BakedCompositeTexture bct = new BakedCompositeTexture(); bct.BakedName = ct.Base.Clone(); if (ct.Base.Path.EndsWith("*")) { List <IAsset> assets = assetManager.GetMany("textures/" + bct.BakedName.Path.Substring(0, bct.BakedName.Path.Length - 1), bct.BakedName.Domain); if (assets.Count == 0) { ct.Base = bct.BakedName = new AssetLocation("unknown"); } if (assets.Count == 1) { ct.Base = assets[0].Location.Clone(); ct.Base.Path = assets[0].Location.Path.Substring("textures/".Length); ct.Base.RemoveEnding(); bct.BakedName = ct.Base.Clone(); } if (assets.Count > 1) { int origLength = (ct.Alternates == null ? 0 : ct.Alternates.Length); CompositeTexture[] alternates = new CompositeTexture[origLength + assets.Count - 1]; if (ct.Alternates != null) { Array.Copy(ct.Alternates, alternates, ct.Alternates.Length); } int i = 0; foreach (IAsset asset in assets) { AssetLocation newLocation = assets[0].Location.Clone(); newLocation.Path = asset.Location.Path.Substring("textures/".Length); newLocation.RemoveEnding(); if (i == 0) { ct.Base = newLocation; bct.BakedName = ct.Base; } else { alternates[origLength + i - 1] = new CompositeTexture(newLocation); } i++; } ct.Alternates = alternates; } } if (ct.Overlays != null) { bct.TextureFilenames = new AssetLocation[ct.Overlays.Length + 1]; bct.TextureFilenames[0] = ct.Base.Clone(); for (int i = 0; i < ct.Overlays.Length; i++) { bct.TextureFilenames[i + 1] = ct.Overlays[i].Clone(); bct.BakedName.Path += "++" + ct.Overlays[i].ToShortString(); } } else { bct.TextureFilenames = new AssetLocation[] { ct.Base.Clone() }; } if (ct.Rotation != 0) { if (ct.Rotation != 90 && ct.Rotation != 0 && ct.Rotation != 180 && ct.Rotation != 270) { throw new Exception("Texture definition " + ct.Base + " has a rotation thats not 0, 90, 180 or 270. These are the only allowed values!"); } bct.BakedName.Path += "@" + ct.Rotation; } if (ct.Alternates != null) { bct.BakedVariants = new BakedCompositeTexture[ct.Alternates.Length + 1]; bct.BakedVariants[0] = bct; for (int i = 0; i < ct.Alternates.Length; i++) { bct.BakedVariants[i + 1] = Bake(assetManager, ct.Alternates[i]); } } return(bct); }