refactoring; removed configuration setup; added consistent sprite batch; got rid of sprite batch settings; implemented newer setup;

This commit is contained in:
2019-03-23 19:04:43 -05:00
parent e3535f5662
commit b045033b25
28 changed files with 342 additions and 329 deletions

View File

@@ -4,7 +4,9 @@ using Microsoft.Xna.Framework.Input;
using RecrownedAthenaeum.Camera;
using RecrownedAthenaeum.ContentSystem;
using RecrownedAthenaeum.Input;
using RecrownedAthenaeum.Render;
using RecrownedAthenaeum.UI.SkinSystem;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -17,46 +19,80 @@ namespace RecrownedAthenaeum.UI.BookSystem
{
readonly ContentManagerController assets;
readonly ISkin skin;
Camera2D camera;
Page targetPage;
Rectangle dimensions;
int width, height;
Dictionary<string, Page> pages = new Dictionary<string, Page>();
List<Page> orderedPages = new List<Page>();
/// <summary>
/// The camera to use to change between pages.
/// </summary>
public Camera2D camera;
/// <summary>
/// The scissorbox used to crop things.
/// </summary>
public ScissorBox scissorBox;
/// <summary>
/// Creates a book.
/// </summary>
/// <param name="assets"><see cref="ContentManagerController"/> that holds the assets that are to be used in the pages for this book during initialization.</param>
/// <param name="skin">The skin that will be passed to pages during their initialization.</param>
public Book(ContentManagerController assets, ISkin skin)
/// <param name="camera">Camera to move to change pages.</param>
/// <param name="scissorBox">The scissor box to use to crop the pages to the given dimensions. Default is null, and will not crop anything.</param>
public Book(ContentManagerController assets, ISkin skin, Camera2D camera, ScissorBox scissorBox = null)
{
this.assets = assets;
this.skin = skin;
this.camera = camera ?? throw new ArgumentNullException("Camera can't be null since book needs a camera to move to change between the slides (pages).");
this.scissorBox = scissorBox;
}
/// <summary>
/// Should be called whenever a valid camera and dimensions for the book exist.
/// Initializes book with given parameters.
/// Generally used with a <see cref="ScreenSystem.Screen"/> and called in the <see cref="ScreenSystem.Screen.Initiate(Rectangle, Camera2D)"/> function.
/// Generally used with a <see cref="ScreenSystem.Screen"/> and called in the <see cref="ScreenSystem.Screen.ApplySize(int, int)"/> function.
/// </summary>
/// <param name="camera">Camera game is currently using.</param>
/// <param name="camera">Camera to move to change pages.</param>
/// <param name="dimensions">Dimensions of the book and the dimensions the pages will use.</param>
public void Initiate(Camera2D camera, Rectangle dimensions)
/// <param name="scissorBox">The scissor box to use to crop the pages to the given dimensions. Default is null, and will not crop anything.</param>
public void Initiate(Camera2D camera, Rectangle dimensions, ScissorBox scissorBox = null)
{
this.camera = camera;
this.dimensions = dimensions;
}
/// <summary>
/// Applies the size if the book's pages.
/// </summary>
/// <param name="width">The width of one page.</param>
/// <param name="height">The height of one page.</param>
public void ApplySize(int width, int height)
{
if (this.width == width && this.height == height) return;
this.width = width;
this.height = height;
for (int pageIndex = 0; pageIndex < pages.Count; pageIndex++)
{
Page page = orderedPages[pageIndex];
if (page.Boundaries.Width != width || page.Boundaries.Height != height)
{
page.requiresSizeUpdate = true;
}
}
}
/// <summary>
/// Draws the pages.
/// </summary>
/// <param name="batch">Batch used to draw.</param>
public void Draw(SpriteBatch batch)
public void Draw(ConsistentSpriteBatch batch)
{
for (int pageIndex = 0; pageIndex < pages.Count; pageIndex++)
{
Page page = orderedPages[pageIndex];
page.Draw(batch);
orderedPages[pageIndex].Draw(batch);
}
}
@@ -82,7 +118,7 @@ namespace RecrownedAthenaeum.UI.BookSystem
for (int pageIndex = 0; pageIndex < pages.Count; pageIndex++)
{
Page page = pages.ElementAt(pageIndex).Value;
if (page.requiresSizeUpdate) page.ApplySize(dimensions.Width, dimensions.Height);
if (page.requiresSizeUpdate) page.ApplySize(width, height);
page.Update(gameTime);
}
}
@@ -97,6 +133,7 @@ namespace RecrownedAthenaeum.UI.BookSystem
{
orderedPages.Add(page);
this.pages.Add(page.name, page);
page.Initialize(assets, skin, scissorBox);
}
}

View File

@@ -1,4 +1,5 @@
using RecrownedAthenaeum.ContentSystem;
using RecrownedAthenaeum.Render;
using RecrownedAthenaeum.UI.Modular;
using RecrownedAthenaeum.UI.SkinSystem;
@@ -20,7 +21,7 @@ namespace RecrownedAthenaeum.UI.BookSystem
/// </summary>
/// <param name="pageX">The X position in the book.</param>
/// <param name="pageY">The Y position in the book.</param>
public Page(int pageX, int pageY) : base(false)
public Page(int pageX, int pageY) : base()
{
this.pageX = pageX;
this.pageY = pageY;
@@ -47,9 +48,10 @@ namespace RecrownedAthenaeum.UI.BookSystem
/// </summary>
/// <param name="assets">The assets to be used during initialization passed by the book this page belongs to.</param>
/// <param name="skin">The skin the book containing this page is given that can be used by this page.</param>
protected internal virtual void Initialize(ContentManagerController assets, ISkin skin)
/// <param name="scissorBox">The scissor box to use for cropping.</param>
protected internal virtual void Initialize(ContentManagerController assets, ISkin skin, ScissorBox scissorBox)
{
this.scissorBox = scissorBox;
}
}
}

View File

@@ -1,5 +1,6 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Render;
using RecrownedAthenaeum.SpecialTypes;
using System;
@@ -49,7 +50,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// Draws the image with default values.
/// </summary>
/// <param name="batch">The batch to use.</param>
public override void Draw(SpriteBatch batch)
public override void Draw(ConsistentSpriteBatch batch)
{
batch.Draw(texture, situation, null, color, rotation, origin, SpriteEffects.None, 0f);
base.Draw(batch);
@@ -63,7 +64,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// <param name="color">The color tint to use.</param>
/// <param name="rotation">Rotation of image.</param>
/// <param name="origin">Origin for the rotation.</param>
public void Draw(SpriteBatch spriteBatch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2))
public void Draw(ConsistentSpriteBatch spriteBatch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2))
{
this.color = color;
this.rotation = rotation;

View File

@@ -4,6 +4,7 @@ using RecrownedAthenaeum.SpecialTypes;
using RecrownedAthenaeum.Input;
using RecrownedAthenaeum.UI.SkinSystem.Definitions;
using RecrownedAthenaeum.UI.SkinSystem;
using RecrownedAthenaeum.Render;
namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
{
@@ -80,7 +81,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
/// Draws the button.
/// </summary>
/// <param name="batch">Batch used to draw the button.</param>
public override void Draw(SpriteBatch batch)
public override void Draw(ConsistentSpriteBatch batch)
{
if (disabled)
{

View File

@@ -1,5 +1,6 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Render;
using RecrownedAthenaeum.SpecialTypes;
using RecrownedAthenaeum.UI.SkinSystem;
using RecrownedAthenaeum.UI.SkinSystem.Definitions;
@@ -84,7 +85,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
/// Called whenever game wants to render this button.
/// </summary>
/// <param name="batch">Batch to use. Batch should already be started.</param>
public override void Draw(SpriteBatch batch)
public override void Draw(ConsistentSpriteBatch batch)
{
base.Draw(batch);
text.Draw(batch);

View File

@@ -1,5 +1,6 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Render;
using System;
using System.Text;
@@ -80,7 +81,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// Draws the text.
/// </summary>
/// <param name="batch">Batch to use.</param>
public override void Draw(SpriteBatch batch)
public override void Draw(ConsistentSpriteBatch batch)
{
batch.DrawString(font, Content, position, color, 0f, default(Vector2), scale, SpriteEffects.None, 0f);
base.Draw(batch);

View File

@@ -2,6 +2,7 @@
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RecrownedAthenaeum.Input;
using RecrownedAthenaeum.Render;
using System;
namespace RecrownedAthenaeum.UI.Modular
@@ -61,7 +62,7 @@ namespace RecrownedAthenaeum.UI.Modular
/// Called every frame to draw this module. Anything that needs to be drawn should go here.
/// </summary>
/// <param name="batch">Batch used to draw.</param>
public virtual void Draw(SpriteBatch batch)
public virtual void Draw(ConsistentSpriteBatch batch)
{
}

View File

@@ -14,36 +14,22 @@ namespace RecrownedAthenaeum.UI.Modular
public class UIModuleGroup : UIModule
{
List<UIModule> modules = new List<UIModule>();
ScissorBox scissorBox;
/// <summary>
/// Configuration for starting sprite batch after scissoring. Only used if cropping.
/// Set this to crop anything that flows out of the boundaries of this group. Will not crop if this is null.
/// </summary>
public SpriteBatchSettings spriteBatchSettings;
public ScissorBox scissorBox;
/// <summary>
/// Creates a module group.
/// Draws this group of modules. If scissoring, will use the matrix and effect designated in the <see cref="ScissorBox"/> to begin the batch normally again.
/// </summary>
/// <param name="crop">Whether or not to crop out of bounds. Default is false.</param>
public UIModuleGroup(bool crop = false)
{
this.spriteBatchSettings = Configuration.SpriteBatchSettings.Value;
if (crop)
{
scissorBox = new ScissorBox();
}
}
/// <summary>
/// Draws this group of modules.
/// </summary>
/// <param name="batch">Batch used to draw the group.</param>
public override void Draw(SpriteBatch batch)
/// <param name="spriteBatch">Batch used to draw the group.</param>
public override void Draw(ConsistentSpriteBatch spriteBatch)
{
if (scissorBox != null)
{
batch.End();
scissorBox.Begin(Boundaries, batch, spriteBatchSettings);
spriteBatch.End();
scissorBox.Begin(Boundaries, spriteBatch);
}
foreach (UIModule module in modules)
@@ -52,7 +38,7 @@ namespace RecrownedAthenaeum.UI.Modular
int offsetY = module.situation.Y;
module.situation.X = situation.X + offsetX;
module.situation.Y = situation.Y + offsetY;
module.Draw(batch);
module.Draw(spriteBatch);
module.situation.X = offsetX;
module.situation.Y = offsetY;
}
@@ -61,6 +47,8 @@ namespace RecrownedAthenaeum.UI.Modular
{
scissorBox.End();
GraphicsDevice graphics = spriteBatch.GraphicsDevice;
spriteBatch.Begin();
}
}

View File

@@ -1,5 +1,6 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Render;
using RecrownedAthenaeum.SpecialTypes;
using RecrownedAthenaeum.UI.SkinSystem.Definitions;
@@ -24,7 +25,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem
/// <param name="destination">The destination to draw to.</param>
/// <param name="rotation">The rotation to use in radians.</param>
/// <param name="origin">The origin for the rotation.</param>
void Draw(string regionName, string color, SpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2));
void Draw(string regionName, string color, ConsistentSpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2));
/// <summary>
/// Returns a <see cref="Color"/> with given name of defined color;

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Render;
using RecrownedAthenaeum.SpecialTypes;
using RecrownedAthenaeum.UI.SkinSystem.Definitions;
@@ -33,7 +34,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem
}
}
public void Draw(string regionName, string color, SpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2))
public void Draw(string regionName, string color, ConsistentSpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2))
{
try
{

View File

@@ -1,5 +1,6 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Render;
using RecrownedAthenaeum.SpecialTypes;
using RecrownedAthenaeum.UI.SkinSystem.Definitions;
using System;
@@ -80,7 +81,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem
/// <param name="destination">The destination to draw to.</param>
/// <param name="rotation">The rotation to use in radians.</param>
/// <param name="origin">The origin for the rotation.</param>
public void Draw(string regionName, string color, SpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2))
public void Draw(string regionName, string color, ConsistentSpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2))
{
if (disposed) throw new ObjectDisposedException(GetType().Name);
textureAtlas.Draw(regionName, batch, destination, colors[color], rotation, origin);

View File

@@ -71,7 +71,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem
public ISkin Skin { get { return mergedSkin; } }
/// <summary>
/// The user loaded skin. Set by the skin loaded by calling <see cref="LoadSkin(SkinData, string, GraphicsDevice)"/>.
/// The user loaded skin resulted from asynchronous <see cref="LoadSkin(string, GraphicsDevice)"/>.
/// </summary>
public ISkin loadedSkin { get { return mergedSkin.mainSkin; } private set { mergedSkin.mainSkin = value; } }
@@ -111,16 +111,14 @@ namespace RecrownedAthenaeum.UI.SkinSystem
/// <summary>
/// loads a skin asynchronously to the <see cref="loadedSkin"/>.
/// </summary>
/// <param name="graphicsDevice">Graphics device to use for texture creation. Uses graphics device from <see cref="Configuration"/>by default.</param>
/// <param name="skinData">The data to generate from.</param>
/// <param name="path">The path pointing to the file with the extension "<see cref="EXTENSION"/>".</param>
public void LoadSkin(SkinData skinData, string path, GraphicsDevice graphicsDevice = null)
/// <param name="graphicsDevice">Graphics device to use for texture creation.</param>
public void LoadSkin(string path, GraphicsDevice graphicsDevice)
{
if (graphicsDevice == null) graphicsDevice = Configuration.GraphicsDeviceManager.GraphicsDevice;
action = Action.LOAD;
this.graphicsDevice = graphicsDevice;
this.selectedSkinPath = path;
this.skinDataToUse = skinData;
this.graphicsDevice = graphicsDevice ?? throw new ArgumentNullException("Requires graphics device to create textures.");
selectedSkinPath = path ?? throw new ArgumentNullException("Requires path to find textures.");
skinDataToUse = ReadSkinData(path);
AttemptAsync();
}
@@ -163,14 +161,18 @@ namespace RecrownedAthenaeum.UI.SkinSystem
}
TextureAtlasDataReader tatlasDataReader = new TextureAtlasDataReader();
TextureAtlasData atlasData;
using (FileStream fileStream = new FileStream(filePath[skinData.nameOfTextureAtlas], FileMode.Open))
{
atlasData = tatlasDataReader.ReadTextureAtlasData(new BinaryReader(fileStream));
}
atlasData = JsonConvert.DeserializeObject<TextureAtlasData>(File.ReadAllText(filePath[skinData.nameOfTextureAtlas]));
Texture2D atlasTexture;
using (FileStream stream = new FileStream(filePath[atlasData.textureName], FileMode.Open))
{
atlasTexture = Texture2D.FromStream(graphicsDevice, stream);
Vector4[] data = new Vector4[atlasTexture.Width * atlasTexture.Height];
atlasTexture.GetData(data);
for (int i = 0; i < data.Length; i++)
{
Color.FromNonPremultiplied(data[i]);
}
atlasTexture.SetData(data);
}
TextureAtlas.Region[] regions = textureAtlasDataReader.GenerateAtlasRegionsFromData(atlasData, atlasTexture);
TextureAtlas textureAtlas = new TextureAtlas(atlasTexture, regions);
@@ -180,6 +182,13 @@ namespace RecrownedAthenaeum.UI.SkinSystem
using (FileStream stream = new FileStream(filePath[skinData.cursorTextureName], FileMode.Open))
{
cursorTexture = Texture2D.FromStream(graphicsDevice, stream);
Vector4[] data = new Vector4[cursorTexture.Width * cursorTexture.Height];
atlasTexture.GetData(data);
for (int i = 0; i < data.Length; i++)
{
Color.FromNonPremultiplied(data[i]);
}
cursorTexture.SetData(data);
}
}
else