1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00

added CustomDrawGroup which allows for components to have their own draw call

This commit is contained in:
Ellpeck 2019-09-08 16:30:55 +02:00
parent 4a7fcd7570
commit 20d7b95891
2 changed files with 39 additions and 2 deletions

View file

@ -0,0 +1,32 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace MLEM.Ui.Elements {
public class CustomDrawGroup : Group {
public Matrix Transform;
public BeginDelegate BeginImpl;
public CustomDrawGroup(Anchor anchor, Vector2 size, Matrix? transform = null, BeginDelegate beginImpl = null, bool setHeightBasedOnChildren = true) :
base(anchor, size, setHeightBasedOnChildren) {
this.Transform = transform ?? Matrix.Identity;
this.BeginImpl = beginImpl ?? ((time, batch, alpha, blendState, samplerState, matrix) => batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, null, null, null, matrix));
}
public delegate void BeginDelegate(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix);
public override void Draw(GameTime time, SpriteBatch batch, float alpha) {
// this is left empty because child components are drawn in DrawEarly
}
public override void DrawEarly(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
var mat = matrix * this.Transform;
this.BeginImpl(time, batch, alpha, blendState, samplerState, mat);
// draw child components in custom begin call
base.Draw(time, batch, alpha);
batch.End();
base.DrawEarly(time, batch, alpha, blendState, samplerState, mat);
}
}
}

View file

@ -110,13 +110,18 @@ namespace MLEM.Ui.Elements {
batch.GraphicsDevice.Clear(Color.Transparent); batch.GraphicsDevice.Clear(Color.Transparent);
// offset children by the render target's location // offset children by the render target's location
var area = this.GetRenderTargetArea(); var area = this.GetRenderTargetArea();
batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, null, null, null, Matrix.CreateTranslation(-area.X, -area.Y, 0)); var trans = Matrix.CreateTranslation(-area.X, -area.Y, 0);
// do the usual draw, but within the render target
batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, null, null, null, trans);
base.Draw(time, batch, alpha); base.Draw(time, batch, alpha);
batch.End(); batch.End();
// also draw any children early within the render target with the translation applied
base.DrawEarly(time, batch, alpha, blendState, samplerState, matrix * trans);
batch.GraphicsDevice.SetRenderTarget(null); batch.GraphicsDevice.SetRenderTarget(null);
} } else {
base.DrawEarly(time, batch, alpha, blendState, samplerState, matrix); base.DrawEarly(time, batch, alpha, blendState, samplerState, matrix);
} }
}
public override Element GetElementUnderPos(Point position) { public override Element GetElementUnderPos(Point position) {
// if overflow is handled, don't propagate mouse checks to hidden children // if overflow is handled, don't propagate mouse checks to hidden children