From ed7865379ddf5fdaf82b79b2713c5b8c0feffccc Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 27 Aug 2019 21:44:02 +0200 Subject: [PATCH] added image button --- Demos/Content/Content.mgcb | 19 ++++++++++++------- Demos/Content/Textures/Tree.png | Bin 0 -> 248 bytes Demos/UiDemo.cs | 3 +++ MLEM.Ui/Elements/ElementHelper.cs | 12 ++++++++++++ MLEM.Ui/Elements/Image.cs | 16 ++++++++++++---- 5 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 Demos/Content/Textures/Tree.png diff --git a/Demos/Content/Content.mgcb b/Demos/Content/Content.mgcb index 80f2e3d..ada6f11 100644 --- a/Demos/Content/Content.mgcb +++ b/Demos/Content/Content.mgcb @@ -20,13 +20,6 @@ /processorParam:TextureFormat=Compressed /build:Fonts/TestFont.spritefont -#begin Fonts/TestFont.spritefont -/importer:FontDescriptionImporter -/processor:FontDescriptionProcessor -/processorParam:PremultiplyAlpha=True -/processorParam:TextureFormat=Compressed -/build:Fonts/TestFont.spritefont - #begin Fonts/TestFontBold.spritefont /importer:FontDescriptionImporter /processor:FontDescriptionProcessor @@ -77,3 +70,15 @@ /processorParam:TextureFormat=Color /build:Textures/Test.png +#begin Textures/Tree.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:Textures/Tree.png + diff --git a/Demos/Content/Textures/Tree.png b/Demos/Content/Textures/Tree.png new file mode 100644 index 0000000000000000000000000000000000000000..2d29e3992bc9005a4cd0f7f25ba1d2983c5dc049 GIT binary patch literal 248 zcmeAS@N?(olHy`uVBq!ia0vp^96&6S?)pbHp0UHx3vIVCg!0B?#)bpQYW literal 0 HcmV?d00001 diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index 732878c..a99c2ad 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -161,6 +161,9 @@ namespace Demos { PositionOffset = new Vector2(0, 1) }); + var region = new TextureRegion(LoadContent("Textures/Tree")); + root.AddChild(ElementHelper.ImageButton(Anchor.AutoLeft, new Vector2(1, 10), region, "Button with image")).PositionOffset = new Vector2(0, 1); + // Below are some querying examples that help you find certain elements easily var children = root.GetChildren(); diff --git a/MLEM.Ui/Elements/ElementHelper.cs b/MLEM.Ui/Elements/ElementHelper.cs index 39318b1..294241f 100644 --- a/MLEM.Ui/Elements/ElementHelper.cs +++ b/MLEM.Ui/Elements/ElementHelper.cs @@ -1,9 +1,21 @@ using Microsoft.Xna.Framework; using MLEM.Input; +using MLEM.Textures; namespace MLEM.Ui.Elements { public static class ElementHelper { + public static Button ImageButton(Anchor anchor, Vector2 size, TextureRegion texture, string text = null, string tooltipText = null, float tooltipWidth = 50, int imagePadding = 2) { + var button = new Button(anchor, size, text, tooltipText, tooltipWidth); + var image = new Image(Anchor.CenterLeft, Vector2.One, texture) { + Padding = new Point(imagePadding), + IgnoresMouse = true + }; + button.OnAreaUpdated += e => image.Size = new Vector2(e.Area.Height, e.Area.Height) / e.Scale; + button.AddChild(image, 0); + return button; + } + public static Panel ShowInfoBox(UiSystem system, Anchor anchor, float width, string text, float buttonHeight = 10, string okText = "Okay") { var box = new Panel(anchor, new Vector2(width, 1), Vector2.Zero, true); box.AddChild(new Paragraph(Anchor.AutoLeft, 1, text)); diff --git a/MLEM.Ui/Elements/Image.cs b/MLEM.Ui/Elements/Image.cs index 1b1134a..3bf5f78 100644 --- a/MLEM.Ui/Elements/Image.cs +++ b/MLEM.Ui/Elements/Image.cs @@ -1,3 +1,4 @@ +using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MLEM.Extensions; @@ -8,19 +9,26 @@ namespace MLEM.Ui.Elements { public Color Color = Color.White; public TextureRegion Texture; - private readonly bool scaleToImage; + public bool ScaleToImage; + public bool MaintainImageAspect = true; public Image(Anchor anchor, Vector2 size, TextureRegion texture, bool scaleToImage = false) : base(anchor, size) { this.Texture = texture; - this.scaleToImage = scaleToImage; + this.ScaleToImage = scaleToImage; } protected override Point CalcActualSize(Rectangle parentArea) { - return this.scaleToImage ? this.Texture.Size : base.CalcActualSize(parentArea); + return this.ScaleToImage ? this.Texture.Size : base.CalcActualSize(parentArea); } public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) { - batch.Draw(this.Texture, this.DisplayArea.OffsetCopy(offset), this.Color * alpha); + if (this.MaintainImageAspect) { + var scale = Math.Min(this.DisplayArea.Width / (float) this.Texture.Width, this.DisplayArea.Height / (float) this.Texture.Height); + var imageOffset = new Vector2(this.DisplayArea.Width / 2F - this.Texture.Width * scale / 2, this.DisplayArea.Height / 2F - this.Texture.Height * scale / 2); + batch.Draw(this.Texture, (this.DisplayArea.Location + offset).ToVector2() + imageOffset, this.Color * alpha, 0, Vector2.Zero, scale, SpriteEffects.None, 0); + } else { + batch.Draw(this.Texture, this.DisplayArea.OffsetCopy(offset), this.Color * alpha); + } base.Draw(time, batch, alpha, offset); }