From da603c5f05cdc9868dcdb56c59fa79c6ebbc9c59 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 29 Dec 2019 19:28:56 +0100 Subject: [PATCH] made custom draw groups be a bit easier to use --- MLEM.Ui/Elements/CustomDrawGroup.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/MLEM.Ui/Elements/CustomDrawGroup.cs b/MLEM.Ui/Elements/CustomDrawGroup.cs index 1d590c9..bb19d19 100644 --- a/MLEM.Ui/Elements/CustomDrawGroup.cs +++ b/MLEM.Ui/Elements/CustomDrawGroup.cs @@ -4,21 +4,21 @@ using Microsoft.Xna.Framework.Graphics; namespace MLEM.Ui.Elements { public class CustomDrawGroup : Group { - public Matrix Transform; + public Matrix? Transform; + public TransformCallback TransformGetter; public BeginDelegate BeginImpl; - public CustomDrawGroup(Anchor anchor, Vector2 size, Matrix? transform = null, BeginDelegate beginImpl = null, bool setHeightBasedOnChildren = true) : + public CustomDrawGroup(Anchor anchor, Vector2 size, TransformCallback transformGetter = null, BeginDelegate beginImpl = null, bool setHeightBasedOnChildren = true) : base(anchor, size, setHeightBasedOnChildren) { - this.Transform = transform ?? Matrix.Identity; + this.TransformGetter = transformGetter ?? ((element, time, matrix) => Matrix.Identity); this.BeginImpl = beginImpl ?? ((element, time, batch, alpha, blendState, samplerState, matrix) => batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, null, null, null, matrix)); } - public delegate void BeginDelegate(CustomDrawGroup element, GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix); - public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) { // end the usual draw so that we can begin our own batch.End(); - var mat = matrix * this.Transform; + var trans = this.Transform ?? this.TransformGetter(this, time, matrix); + var mat = matrix * trans; this.BeginImpl(this, time, batch, alpha, blendState, samplerState, mat); // draw child components in custom begin call base.Draw(time, batch, alpha, blendState, samplerState, mat); @@ -28,5 +28,9 @@ namespace MLEM.Ui.Elements { batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, null, null, null, matrix); } + public delegate void BeginDelegate(CustomDrawGroup element, GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix); + + public delegate Matrix TransformCallback(CustomDrawGroup element, GameTime time, Matrix matrix); + } } \ No newline at end of file