diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index 11d8c96..dee399e 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -155,8 +155,9 @@ namespace Demos { root.AddChild(slider); // Check the WobbleButton method for an explanation of how this button works - root.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, 10), "Wobble Me", "This button wobbles around visually when clicked, but this doesn't affect its actual size and positioning") { - OnPressed = element => CoroutineHandler.Start(this.WobbleButton(element)), + var group = root.AddChild(new CustomDrawGroup(Anchor.AutoLeft, new Vector2(1, 0))); + group.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, 10), "Wobble Me", "This button wobbles around visually when clicked, but this doesn't affect its actual size and positioning") { + OnPressed = element => CoroutineHandler.Start(this.WobbleButton(group)), PositionOffset = new Vector2(0, 1) }); root.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, 10), "Transform Ui", "This button causes the entire ui to be transformed (both in positioning, rotation and scale). As this is an easily pulled off operation, it can be used for animations and other gimmicks.") { @@ -207,19 +208,18 @@ namespace Demos { // This method is used by the wobbling button (see above) // Note that this particular example makes use of the Coroutine package, which is not required but makes demonstration easier - private IEnumerator WobbleButton(Element button) { + private IEnumerator WobbleButton(CustomDrawGroup group) { var counter = 0F; while (counter < 4 * Math.PI) { - // The imporant bit is that it changes its added display scale and offset, allowing the button to still maintain the + // The important bit is that it changes its added display scale and offset, allowing the button to still maintain the // correct position and scaling for both anchoring and interacting purposes, but to show any kind of animation visually // This could be useful, for example, to create a little feedback effect to clicking it where it changes size for a second - button.AddedDisplayScale = new Vector2((float) Math.Sin(counter)); - button.AddedDisplayOffset = new Vector2((float) Math.Sin(counter / 2) * 4, 0); + // note that other changes can be applied to a custom draw group, like MG effects and so on + group.Transform = Matrix.CreateTranslation((float) Math.Sin(counter / 2) * 10 * group.Scale, 0, 0); counter += 0.1F; yield return new WaitSeconds(0.01F); } - button.AddedDisplayScale = Vector2.Zero; - button.AddedDisplayOffset = Vector2.Zero; + group.Transform = Matrix.Identity; } public override void Clear() { diff --git a/MLEM.Ui/Elements/CustomDrawGroup.cs b/MLEM.Ui/Elements/CustomDrawGroup.cs index fed0e62..98296f8 100644 --- a/MLEM.Ui/Elements/CustomDrawGroup.cs +++ b/MLEM.Ui/Elements/CustomDrawGroup.cs @@ -10,10 +10,10 @@ namespace MLEM.Ui.Elements { 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)); + this.BeginImpl = beginImpl ?? ((element, 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 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) { // this is left empty because child components are drawn in DrawEarly @@ -21,7 +21,7 @@ namespace MLEM.Ui.Elements { 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); + this.BeginImpl(this, time, batch, alpha, blendState, samplerState, mat); // draw child components in custom begin call base.Draw(time, batch, alpha); batch.End(); diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index c575829..8952230 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -75,11 +75,7 @@ namespace MLEM.Ui.Elements { } } public Point ScaledChildPadding => this.childPadding.Multiply(this.Scale); - public Vector2 AddedDisplayOffset; - public Point ScaledDisplayOffset => (this.AddedDisplayOffset * this.Scale).ToPoint(); - public Vector2 AddedDisplayScale; - public Point ScaledDisplayScale => (this.AddedDisplayScale * this.Scale).ToPoint(); - + public GenericCallback OnPressed; public GenericCallback OnSecondaryPressed; public GenericCallback OnSelected; @@ -137,8 +133,7 @@ namespace MLEM.Ui.Elements { public Rectangle DisplayArea { get { var padded = this.Area; - padded.Inflate(this.ScaledDisplayScale.X, this.ScaledDisplayScale.Y); - padded.Location += this.ScaledPadding + this.ScaledDisplayOffset; + padded.Location += this.ScaledPadding; padded.Width -= this.ScaledPadding.X * 2; padded.Height -= this.ScaledPadding.Y * 2; return padded;