mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
made custom draw group only end and begin when it needs to
This commit is contained in:
parent
c92e4e0fd1
commit
612df004e6
1 changed files with 25 additions and 11 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
using System;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
|
||||||
|
@ -6,26 +7,39 @@ namespace MLEM.Ui.Elements {
|
||||||
|
|
||||||
public Matrix? Transform;
|
public Matrix? Transform;
|
||||||
public TransformCallback TransformGetter;
|
public TransformCallback TransformGetter;
|
||||||
public BeginDelegate BeginImpl;
|
private BeginDelegate beginImpl;
|
||||||
|
private bool isDefaultBegin;
|
||||||
|
public BeginDelegate BeginImpl {
|
||||||
|
get => this.beginImpl;
|
||||||
|
set {
|
||||||
|
this.beginImpl = value ?? ((element, time, batch, alpha, blendState, samplerState, matrix) => batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, null, null, null, matrix));
|
||||||
|
this.isDefaultBegin = value == null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public CustomDrawGroup(Anchor anchor, Vector2 size, TransformCallback transformGetter = 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) {
|
base(anchor, size, setHeightBasedOnChildren) {
|
||||||
this.TransformGetter = transformGetter ?? ((element, time, matrix) => 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));
|
this.BeginImpl = beginImpl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Draw(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
|
var transform = this.Transform ?? this.TransformGetter(this, time, matrix);
|
||||||
batch.End();
|
var customDraw = !this.isDefaultBegin || transform != Matrix.Identity;
|
||||||
var trans = this.Transform ?? this.TransformGetter(this, time, matrix);
|
var mat = matrix * transform;
|
||||||
var mat = matrix * trans;
|
if (customDraw) {
|
||||||
this.BeginImpl(this, time, batch, alpha, blendState, samplerState, mat);
|
// end the usual draw so that we can begin our own
|
||||||
|
batch.End();
|
||||||
|
this.BeginImpl(this, time, batch, alpha, blendState, samplerState, mat);
|
||||||
|
}
|
||||||
// draw child components in custom begin call
|
// draw child components in custom begin call
|
||||||
base.Draw(time, batch, alpha, blendState, samplerState, mat);
|
base.Draw(time, batch, alpha, blendState, samplerState, mat);
|
||||||
// end our draw
|
if (customDraw) {
|
||||||
batch.End();
|
// end our draw
|
||||||
// begin the usual draw again for other elements
|
batch.End();
|
||||||
batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, null, null, null, matrix);
|
// begin the usual draw again for other 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 void BeginDelegate(CustomDrawGroup element, GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix);
|
||||||
|
|
Loading…
Reference in a new issue