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

allow for the transformation matrix to modify mouse positions

This commit is contained in:
Ellpeck 2020-07-20 14:18:26 +02:00
parent abffa4db57
commit 411d3c1cd9
2 changed files with 23 additions and 17 deletions

View file

@ -11,12 +11,14 @@ namespace MLEM.Ui.Elements {
[Obsolete("CustomDrawGroup mechanics have been moved down to Element, which means custom draw groups are no longer necessary.")] [Obsolete("CustomDrawGroup mechanics have been moved down to Element, which means custom draw groups are no longer necessary.")]
public class CustomDrawGroup : Group { public class CustomDrawGroup : Group {
public readonly TransformCallback TransformGetter;
/// <summary> /// <summary>
/// Creates a new custom draw group with the given settings /// Creates a new custom draw group with the given settings
/// </summary> /// </summary>
/// <param name="anchor">The group's anchor</param> /// <param name="anchor">The group's anchor</param>
/// <param name="size">The group's size</param> /// <param name="size">The group's size</param>
/// <param name="transformGetter">The group's <see cref="Element.TransformGetter"/></param> /// <param name="transformGetter">The group's <see cref="TransformGetter"/></param>
/// <param name="beginImpl">The group's <see cref="Element.BeginImpl"/></param> /// <param name="beginImpl">The group's <see cref="Element.BeginImpl"/></param>
/// <param name="setHeightBasedOnChildren">Whether this group should automatically calculate its height based on its children</param> /// <param name="setHeightBasedOnChildren">Whether this group should automatically calculate its height based on its children</param>
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) :
@ -31,5 +33,20 @@ namespace MLEM.Ui.Elements {
this.ScaleTransform(scale, origin); this.ScaleTransform(scale, origin);
} }
/// <inheritdoc />
public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
if (this.Transform == Matrix.Identity)
this.Transform = this.TransformGetter(this, time, matrix);
base.Draw(time, batch, alpha, blendState, samplerState, matrix);
}
/// <summary>
/// A delegate method used for <see cref="CustomDrawGroup.TransformGetter"/>
/// </summary>
/// <param name="element">The element whose transform to get</param>
/// <param name="time">The game's time</param>
/// <param name="matrix">The regular transform matrix</param>
public delegate Matrix TransformCallback(Element element, GameTime time, Matrix matrix);
} }
} }

View file

@ -230,11 +230,7 @@ namespace MLEM.Ui.Elements {
/// Can easily be scaled using <see cref="ScaleTransform"/>. /// Can easily be scaled using <see cref="ScaleTransform"/>.
/// Note that, when this is non-null, a new <see cref="SpriteBatch.Begin"/> call is used for this element. /// Note that, when this is non-null, a new <see cref="SpriteBatch.Begin"/> call is used for this element.
/// </summary> /// </summary>
public Matrix? Transform; public Matrix Transform = Matrix.Identity;
/// <summary>
/// A callback for retrieving this element's <see cref="Transform"/> automatically.
/// </summary>
public TransformCallback TransformGetter = (e, time, m) => Matrix.Identity;
/// <summary> /// <summary>
/// The call that this element should make to <see cref="SpriteBatch"/> to begin drawing. /// The call that this element should make to <see cref="SpriteBatch"/> to begin drawing.
/// Note that, when this is non-null, a new <see cref="SpriteBatch.Begin"/> call is used for this element. /// Note that, when this is non-null, a new <see cref="SpriteBatch.Begin"/> call is used for this element.
@ -832,9 +828,8 @@ namespace MLEM.Ui.Elements {
/// <param name="samplerState">The sampler state that is used for drawing</param> /// <param name="samplerState">The sampler state that is used for drawing</param>
/// <param name="matrix">The transformation matrix that is used for drawing</param> /// <param name="matrix">The transformation matrix that is used for drawing</param>
public void DrawTransformed(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) { public void DrawTransformed(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
var transform = this.Transform ?? this.TransformGetter(this, time, matrix); var customDraw = this.BeginImpl != null || this.Transform != Matrix.Identity;
var customDraw = this.BeginImpl != null || transform != Matrix.Identity; var mat = this.Transform * matrix;
var mat = transform * matrix;
if (customDraw) { if (customDraw) {
// end the usual draw so that we can begin our own // end the usual draw so that we can begin our own
batch.End(); batch.End();
@ -902,6 +897,8 @@ namespace MLEM.Ui.Elements {
public virtual Element GetElementUnderPos(Vector2 position) { public virtual Element GetElementUnderPos(Vector2 position) {
if (this.IsHidden) if (this.IsHidden)
return null; return null;
if (this.Transform != Matrix.Identity)
position = Vector2.Transform(position, Matrix.Invert(this.Transform));
var children = this.GetRelevantChildren(); var children = this.GetRelevantChildren();
for (var i = children.Count - 1; i >= 0; i--) { for (var i = children.Count - 1; i >= 0; i--) {
var element = children[i].GetElementUnderPos(position); var element = children[i].GetElementUnderPos(position);
@ -1020,13 +1017,5 @@ namespace MLEM.Ui.Elements {
/// <param name="matrix">The transform matrix used for drawing</param> /// <param name="matrix">The transform matrix used for drawing</param>
public delegate void BeginDelegate(Element element, GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix); public delegate void BeginDelegate(Element element, GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix);
/// <summary>
/// A delegate method used for <see cref="TransformGetter"/>
/// </summary>
/// <param name="element">The element whose transform to get</param>
/// <param name="time">The game's time</param>
/// <param name="matrix">The regular transform matrix</param>
public delegate Matrix TransformCallback(Element element, GameTime time, Matrix matrix);
} }
} }