1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-24 01:23:37 +02: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

@ -10,13 +10,15 @@ namespace MLEM.Ui.Elements {
/// </summary>
[Obsolete("CustomDrawGroup mechanics have been moved down to Element, which means custom draw groups are no longer necessary.")]
public class CustomDrawGroup : Group {
public readonly TransformCallback TransformGetter;
/// <summary>
/// Creates a new custom draw group with the given settings
/// </summary>
/// <param name="anchor">The group's anchor</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="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) :
@ -31,5 +33,20 @@ namespace MLEM.Ui.Elements {
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"/>.
/// Note that, when this is non-null, a new <see cref="SpriteBatch.Begin"/> call is used for this element.
/// </summary>
public Matrix? Transform;
/// <summary>
/// A callback for retrieving this element's <see cref="Transform"/> automatically.
/// </summary>
public TransformCallback TransformGetter = (e, time, m) => Matrix.Identity;
public Matrix Transform = Matrix.Identity;
/// <summary>
/// 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.
@ -832,9 +828,8 @@ namespace MLEM.Ui.Elements {
/// <param name="samplerState">The sampler state 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) {
var transform = this.Transform ?? this.TransformGetter(this, time, matrix);
var customDraw = this.BeginImpl != null || transform != Matrix.Identity;
var mat = transform * matrix;
var customDraw = this.BeginImpl != null || this.Transform != Matrix.Identity;
var mat = this.Transform * matrix;
if (customDraw) {
// end the usual draw so that we can begin our own
batch.End();
@ -902,6 +897,8 @@ namespace MLEM.Ui.Elements {
public virtual Element GetElementUnderPos(Vector2 position) {
if (this.IsHidden)
return null;
if (this.Transform != Matrix.Identity)
position = Vector2.Transform(position, Matrix.Invert(this.Transform));
var children = this.GetRelevantChildren();
for (var i = children.Count - 1; i >= 0; i--) {
var element = children[i].GetElementUnderPos(position);
@ -1020,13 +1017,5 @@ namespace MLEM.Ui.Elements {
/// <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);
/// <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);
}
}