summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/GuiSpriteBatchRenderer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/GuiSpriteBatchRenderer.cs')
-rw-r--r--Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/GuiSpriteBatchRenderer.cs81
1 files changed, 81 insertions, 0 deletions
diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/GuiSpriteBatchRenderer.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/GuiSpriteBatchRenderer.cs
new file mode 100644
index 0000000..6d1e8ce
--- /dev/null
+++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/GuiSpriteBatchRenderer.cs
@@ -0,0 +1,81 @@
+using System;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using MonoGame.Extended.BitmapFonts;
+using MonoGame.Extended.TextureAtlases;
+
+namespace MonoGame.Extended.Gui
+{
+ public interface IGuiRenderer
+ {
+ void Begin();
+ void DrawRegion(TextureRegion2D textureRegion, Rectangle rectangle, Color color, Rectangle? clippingRectangle = null);
+ void DrawRegion(TextureRegion2D textureRegion, Vector2 position, Color color, Rectangle? clippingRectangle = null);
+ void DrawText(BitmapFont font, string text, Vector2 position, Color color, Rectangle? clippingRectangle = null);
+ void DrawRectangle(Rectangle rectangle, Color color, float thickness = 1f, Rectangle? clippingRectangle = null);
+ void FillRectangle(Rectangle rectangle, Color color, Rectangle? clippingRectangle = null);
+ void End();
+ }
+
+ public class GuiSpriteBatchRenderer : IGuiRenderer
+ {
+ private readonly Func<Matrix> _getTransformMatrix;
+ private readonly SpriteBatch _spriteBatch;
+
+ public GuiSpriteBatchRenderer(GraphicsDevice graphicsDevice, Func<Matrix> getTransformMatrix)
+ {
+ _getTransformMatrix = getTransformMatrix;
+ _spriteBatch = new SpriteBatch(graphicsDevice);
+ }
+
+ public SpriteSortMode SortMode { get; set; }
+ public BlendState BlendState { get; set; } = BlendState.AlphaBlend;
+ public SamplerState SamplerState { get; set; } = SamplerState.PointClamp;
+ public DepthStencilState DepthStencilState { get; set; } = DepthStencilState.Default;
+ public RasterizerState RasterizerState { get; set; } = RasterizerState.CullNone;
+ public Effect Effect { get; set; }
+
+ public void Begin()
+ {
+ _spriteBatch.Begin(SortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, _getTransformMatrix());
+ }
+
+ public void End()
+ {
+ _spriteBatch.End();
+ }
+
+ public void DrawRegion(TextureRegion2D textureRegion, Rectangle rectangle, Color color, Rectangle? clippingRectangle = null)
+ {
+ if (textureRegion != null)
+ _spriteBatch.Draw(textureRegion, rectangle, color, clippingRectangle);
+ }
+
+ public void DrawRegion(TextureRegion2D textureRegion, Vector2 position, Color color, Rectangle? clippingRectangle = null)
+ {
+ if (textureRegion != null)
+ _spriteBatch.Draw(textureRegion, position, color, clippingRectangle);
+ }
+
+ public void DrawText(BitmapFont font, string text, Vector2 position, Color color, Rectangle? clippingRectangle = null)
+ {
+ _spriteBatch.DrawString(font, text, position, color, clippingRectangle);
+ }
+
+ public void DrawRectangle(Rectangle rectangle, Color color, float thickness = 1f, Rectangle? clippingRectangle = null)
+ {
+ if (clippingRectangle.HasValue)
+ rectangle = rectangle.Clip(clippingRectangle.Value);
+
+ _spriteBatch.DrawRectangle(rectangle, color, thickness);
+ }
+
+ public void FillRectangle(Rectangle rectangle, Color color, Rectangle? clippingRectangle = null)
+ {
+ if (clippingRectangle.HasValue)
+ rectangle = rectangle.Clip(clippingRectangle.Value);
+
+ _spriteBatch.FillRectangle(rectangle, color);
+ }
+ }
+}