1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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);
}
}
}
|