refactoring; removed configuration setup; added consistent sprite batch; got rid of sprite batch settings; implemented newer setup;
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.Render;
|
||||
|
||||
namespace RecrownedAthenaeum.SpecialTypes
|
||||
{
|
||||
@@ -16,6 +17,6 @@ namespace RecrownedAthenaeum.SpecialTypes
|
||||
/// <param name="color">The color tint to draw with.</param>
|
||||
/// <param name="rotation">The rotation to be used.</param>
|
||||
/// <param name="origin">The origin for the rotation.</param>
|
||||
void Draw(SpriteBatch spriteBatch, Rectangle destination, Color color, float rotation = 0f, Vector2 origin = default(Vector2));
|
||||
void Draw(ConsistentSpriteBatch spriteBatch, Rectangle destination, Color color, float rotation = 0f, Vector2 origin = default(Vector2));
|
||||
}
|
||||
}
|
||||
|
@@ -86,20 +86,21 @@ namespace RecrownedAthenaeum.SpecialTypes
|
||||
/// Draws the ninepatch.
|
||||
/// </summary>
|
||||
/// <param name="spriteBatch">Batch to use.</param>
|
||||
/// <param name="destination">Where to the patch.</param>
|
||||
/// <param name="color">The color of the patch.</param>
|
||||
/// <param name="spriteBatchSettings">The sprite batch settings to use to begin the batch in after drawing the ninepatch.</param>
|
||||
public void Draw(SpriteBatch spriteBatch, Color color, Rectangle destination, SpriteBatchSettings? spriteBatchSettings = null)
|
||||
/// <param name="destination">Where to the patch.</param>
|
||||
public void Draw(ConsistentSpriteBatch spriteBatch, Color color, Rectangle destination)
|
||||
{
|
||||
if (spriteBatchSettings == null) spriteBatchSettings = Configuration.SpriteBatchSettings;
|
||||
spriteBatch.End();
|
||||
try
|
||||
{
|
||||
spriteBatch.End();
|
||||
} catch (InvalidOperationException)
|
||||
{
|
||||
throw new InvalidOperationException("Begin must be called to draw a nine patch!");
|
||||
}
|
||||
|
||||
SpriteBatchSettings ss = spriteBatchSettings.Value;
|
||||
SamplerState nSS = ss.samplerState;
|
||||
|
||||
ss.samplerState = SamplerState.PointClamp;
|
||||
|
||||
ss.BeginSpriteBatch(spriteBatch);
|
||||
SamplerState originalSamplerState = spriteBatch.SamplerState;
|
||||
GraphicsDevice graphics = spriteBatch.GraphicsDevice;
|
||||
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
|
||||
|
||||
Rectangle[] destinations = GenenerateDestinationRectangles(destination.Width, destination.Height);
|
||||
for (int i = 0; i < destinations.Length; i++)
|
||||
@@ -110,20 +111,17 @@ namespace RecrownedAthenaeum.SpecialTypes
|
||||
}
|
||||
spriteBatch.End();
|
||||
|
||||
ss.samplerState = nSS;
|
||||
ss.BeginSpriteBatch(spriteBatch);
|
||||
spriteBatch.Begin(samplerState: originalSamplerState);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draw with more options.
|
||||
/// Uses the default <see cref="Configuration"/> for the spritebatch settings.
|
||||
/// </summary>
|
||||
/// <param name="spriteBatch">Spritebatch to use.</param>
|
||||
/// <param name="destination">The destination to draw the patch.</param>
|
||||
/// <param name="color">The tint for each patch.</param>
|
||||
/// <param name="rotation">Not considered for 9patches.</param>
|
||||
/// <param name="origin">Not considered for 9patches.</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))
|
||||
{
|
||||
if (rotation != 0) throw new NotImplementedException("Ninepatches can't be rotated.");
|
||||
if (origin != default(Vector2)) throw new NotImplementedException("Ninepatches can't have origin changed (hint: use the destination rectangle to shift and position).");
|
||||
|
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.Render;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -56,7 +57,7 @@ namespace RecrownedAthenaeum.SpecialTypes
|
||||
/// <param name="color">Color to use.</param>
|
||||
/// <param name="rotation">Rotation of texture drawn.</param>
|
||||
/// <param name="origin">Origin used by rotation.</param>
|
||||
public void Draw(string name, SpriteBatch batch, Rectangle destination, Color color = default(Color), float rotation = 0, Vector2 origin = new Vector2())
|
||||
public void Draw(string name, ConsistentSpriteBatch batch, Rectangle destination, Color color = default(Color), float rotation = 0, Vector2 origin = new Vector2())
|
||||
{
|
||||
dictionaryOfRegions[name].Draw(batch, destination, color, rotation, origin);
|
||||
}
|
||||
@@ -65,11 +66,10 @@ namespace RecrownedAthenaeum.SpecialTypes
|
||||
/// Creates or obtains a previously created texture of a region.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of region.</param>
|
||||
/// <param name="graphicsDevice">graphics device to be used. Default is null and will resort to using graphics device from <see cref="Configuration"/>'s graphics device manager.</param>
|
||||
/// <param name="graphicsDevice">graphics device to be used to generate the texture.</param>
|
||||
/// <returns>The texture from the region.</returns>
|
||||
public Texture2D ObtainRegionAsTexture(string name, GraphicsDevice graphicsDevice = null)
|
||||
public Texture2D ObtainRegionAsTexture(string name, GraphicsDevice graphicsDevice)
|
||||
{
|
||||
if (graphicsDevice == null) graphicsDevice = Configuration.GraphicsDeviceManager.GraphicsDevice;
|
||||
return dictionaryOfRegions[name].AsTexture2D(graphicsDevice);
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ namespace RecrownedAthenaeum.SpecialTypes
|
||||
/// <summary>
|
||||
/// A region of a <see cref="TextureAtlas"/>.
|
||||
/// </summary>
|
||||
public class Region : IComparable<Region>, ISpecialDrawable, IDisposable
|
||||
public class Region : ISpecialDrawable, IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the region. Mostly used to be refered to within the context of a <see cref="TextureAtlas"/>.
|
||||
@@ -156,8 +156,8 @@ namespace RecrownedAthenaeum.SpecialTypes
|
||||
{
|
||||
this.atlasTexture = atlasTexture ?? throw new ArgumentNullException("Name parameters can be null.");
|
||||
this.name = name ?? throw new ArgumentNullException("Name parameters can be null.");
|
||||
this.sourceRectangle = sourceRegion;
|
||||
this.ninepatch = ninePatch;
|
||||
sourceRectangle = sourceRegion;
|
||||
ninepatch = ninePatch;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -168,7 +168,7 @@ namespace RecrownedAthenaeum.SpecialTypes
|
||||
/// <param name="color">The color to use.</param>
|
||||
/// <param name="rotation">Rotation of the final drawing. Ignored if is a 9patch.</param>
|
||||
/// <param name="origin">The origin of the drawing. Ignored if is a 9patch.</param>
|
||||
public void Draw(SpriteBatch batch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2))
|
||||
public void Draw(ConsistentSpriteBatch batch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2))
|
||||
{
|
||||
if (Disposed) throw new ObjectDisposedException(GetType().Name);
|
||||
|
||||
@@ -185,16 +185,15 @@ namespace RecrownedAthenaeum.SpecialTypes
|
||||
/// <summary>
|
||||
/// Create or obtains a previously created texture of this region.
|
||||
/// </summary>
|
||||
/// <param name="graphicsDevice">The graphics device to use to create the texture. Will use graphics device from <see cref="Configuration"/>'s graphics device manager if left to null.</param>
|
||||
/// <param name="graphicsDevice">The graphics device to use to create the texture.</param>
|
||||
/// <returns>The texture of the region.</returns>
|
||||
public Texture2D AsTexture2D(GraphicsDevice graphicsDevice = null)
|
||||
public Texture2D AsTexture2D(GraphicsDevice graphicsDevice)
|
||||
{
|
||||
if (Disposed) throw new ObjectDisposedException(GetType().Name);
|
||||
|
||||
if (regionTexture == null)
|
||||
{
|
||||
Color[] data = new Color[sourceRectangle.Width * sourceRectangle.Height];
|
||||
if (graphicsDevice == null) graphicsDevice = Configuration.GraphicsDeviceManager.GraphicsDevice;
|
||||
regionTexture = new Texture2D(graphicsDevice, sourceRectangle.Width, sourceRectangle.Height);
|
||||
atlasTexture.GetData(0, sourceRectangle, data, 0, sourceRectangle.Width * sourceRectangle.Height);
|
||||
regionTexture.SetData(data);
|
||||
@@ -202,16 +201,6 @@ namespace RecrownedAthenaeum.SpecialTypes
|
||||
return regionTexture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares this region to another in terms of name.
|
||||
/// </summary>
|
||||
/// <param name="other">The other region to compare to in terms of name.</param>
|
||||
/// <returns>Less than one if precedes, greater than one if after, 0 if same.</returns>
|
||||
public int CompareTo(Region other)
|
||||
{
|
||||
return name.CompareTo(other);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Call this to dispose.
|
||||
/// </summary>
|
||||
|
Reference in New Issue
Block a user