From 4fd37b667542db64c8adace66ff633571654228d Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Fri, 14 Dec 2018 00:42:51 -0600 Subject: [PATCH] implemented the special drawable interface. --- RecrownedAthenaeum/DataTypes/NinePatch.cs | 28 +++++++++++-------- RecrownedAthenaeum/DataTypes/TextureAtlas.cs | 12 ++++++-- .../UI/Modular/Modules/Image.cs | 12 +++++++- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/RecrownedAthenaeum/DataTypes/NinePatch.cs b/RecrownedAthenaeum/DataTypes/NinePatch.cs index a252737..d506d45 100644 --- a/RecrownedAthenaeum/DataTypes/NinePatch.cs +++ b/RecrownedAthenaeum/DataTypes/NinePatch.cs @@ -9,7 +9,7 @@ using System.Xml.Serialization; namespace RecrownedAthenaeum.DataTypes { - public class NinePatch + public class NinePatch : ISpecialDrawable { public Color color; public Rectangle textureRegion; @@ -38,7 +38,7 @@ namespace RecrownedAthenaeum.DataTypes color = Color.White; } - public void Draw(SpriteBatch batch, Rectangle destination) + public void Draw(SpriteBatch spriteBatch, Rectangle destination) { Rectangle sourceRectangle; Rectangle drawnRectangle; @@ -57,7 +57,7 @@ namespace RecrownedAthenaeum.DataTypes sourceRectangle.X += textureRegion.X; sourceRectangle.Y += textureRegion.Y; - batch.Draw(texture, drawnRectangle, sourceRectangle, color); + spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color); //2x1 drawnRectangle.X = destination.X + a; @@ -72,7 +72,7 @@ namespace RecrownedAthenaeum.DataTypes sourceRectangle.X += textureRegion.X; sourceRectangle.Y += textureRegion.Y; - batch.Draw(texture, drawnRectangle, sourceRectangle, color); + spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color); //3x1 drawnRectangle.X = destination.X + destination.Width - b; @@ -87,7 +87,7 @@ namespace RecrownedAthenaeum.DataTypes sourceRectangle.X += textureRegion.X; sourceRectangle.Y += textureRegion.Y; - batch.Draw(texture, drawnRectangle, sourceRectangle, color); + spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color); //1x2 drawnRectangle.X = destination.X; @@ -102,7 +102,7 @@ namespace RecrownedAthenaeum.DataTypes sourceRectangle.X += textureRegion.X; sourceRectangle.Y += textureRegion.Y; - batch.Draw(texture, drawnRectangle, sourceRectangle, color); + spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color); //2x2 drawnRectangle.X = destination.X + a; @@ -117,7 +117,7 @@ namespace RecrownedAthenaeum.DataTypes sourceRectangle.X += textureRegion.X; sourceRectangle.Y += textureRegion.Y; - batch.Draw(texture, drawnRectangle, sourceRectangle, color); + spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color); //3x2 drawnRectangle.X = destination.X + destination.Width - b; @@ -132,7 +132,7 @@ namespace RecrownedAthenaeum.DataTypes sourceRectangle.X += textureRegion.X; sourceRectangle.Y += textureRegion.Y; - batch.Draw(texture, drawnRectangle, sourceRectangle, color); + spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color); //1x3 drawnRectangle.X = destination.X; @@ -147,7 +147,7 @@ namespace RecrownedAthenaeum.DataTypes sourceRectangle.X += textureRegion.X; sourceRectangle.Y += textureRegion.Y; - batch.Draw(texture, drawnRectangle, sourceRectangle, color); + spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color); //2x3 drawnRectangle.X = destination.X + a; @@ -162,7 +162,7 @@ namespace RecrownedAthenaeum.DataTypes sourceRectangle.X += textureRegion.X; sourceRectangle.Y += textureRegion.Y; - batch.Draw(texture, drawnRectangle, sourceRectangle, color); + spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color); //3x3 drawnRectangle.X = destination.X + destination.Width - b; @@ -177,7 +177,13 @@ namespace RecrownedAthenaeum.DataTypes sourceRectangle.X += textureRegion.X; sourceRectangle.Y += textureRegion.Y; - batch.Draw(texture, drawnRectangle, sourceRectangle, color); + spriteBatch.Draw(texture, drawnRectangle, sourceRectangle, color); + } + + public void Draw(SpriteBatch spriteBatch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2)) + { + this.color = color; + Draw(spriteBatch, destination); } } } diff --git a/RecrownedAthenaeum/DataTypes/TextureAtlas.cs b/RecrownedAthenaeum/DataTypes/TextureAtlas.cs index 12697e5..ffdaf1b 100644 --- a/RecrownedAthenaeum/DataTypes/TextureAtlas.cs +++ b/RecrownedAthenaeum/DataTypes/TextureAtlas.cs @@ -16,7 +16,7 @@ namespace RecrownedAthenaeum.DataTypes private Texture2D texture; private Dictionary dictionaryOfRegions = new Dictionary(); - + public TextureAtlasRegion this[string name] { get { if (name != null && dictionaryOfRegions.ContainsKey(name)) return dictionaryOfRegions[name]; else return null; } } public TextureAtlas(Texture2D texture, TextureAtlasRegion[] regions) { this.texture = texture; @@ -57,7 +57,7 @@ namespace RecrownedAthenaeum.DataTypes return dictionaryOfRegions[name].AsTexture2D(graphicsDevice); } - public class TextureAtlasRegion : IComparable, IDisposable + public class TextureAtlasRegion : IComparable, ISpecialDrawable, IDisposable { public readonly string name; public readonly Rectangle sourceRectangle; @@ -82,7 +82,13 @@ namespace RecrownedAthenaeum.DataTypes public void Draw(SpriteBatch batch, Rectangle destination, Color color, float rotation, Vector2 origin) { - batch.Draw(atlasTexture, destination, sourceRectangle, color, rotation, origin, SpriteEffects.None, 0f); + if (ninepatch != null) + { + ninepatch.Draw(batch, destination); + } else + { + batch.Draw(atlasTexture, destination, sourceRectangle, color, rotation, origin, SpriteEffects.None, 0f); + } } /// diff --git a/RecrownedAthenaeum/UI/Modular/Modules/Image.cs b/RecrownedAthenaeum/UI/Modular/Modules/Image.cs index 751c68d..e7b42ab 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/Image.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/Image.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using RecrownedAthenaeum.DataTypes; using RecrownedAthenaeum.UI.Modular; using System; using System.Collections.Generic; @@ -9,7 +10,7 @@ using System.Threading.Tasks; namespace RecrownedAthenaeum.UI.Modular.Modules { - public class Image : UIModule + public class Image : UIModule, ISpecialDrawable { public float rotation = 0f; public Texture2D Texture { get; set; } @@ -59,5 +60,14 @@ namespace RecrownedAthenaeum.UI.Modular.Modules batch.Draw(Texture, bounds, null, color, rotation, origin, SpriteEffects.None, 0f); base.Draw(batch); } + + public void Draw(SpriteBatch spriteBatch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2)) + { + this.color = color; + this.rotation = rotation; + this.origin = origin; + bounds = destination; + Draw(spriteBatch); + } } }