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

added image button

This commit is contained in:
Ellpeck 2019-08-27 21:44:02 +02:00
parent abd58f3869
commit ed7865379d
5 changed files with 39 additions and 11 deletions

View file

@ -20,13 +20,6 @@
/processorParam:TextureFormat=Compressed /processorParam:TextureFormat=Compressed
/build:Fonts/TestFont.spritefont /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 #begin Fonts/TestFontBold.spritefont
/importer:FontDescriptionImporter /importer:FontDescriptionImporter
/processor:FontDescriptionProcessor /processor:FontDescriptionProcessor
@ -77,3 +70,15 @@
/processorParam:TextureFormat=Color /processorParam:TextureFormat=Color
/build:Textures/Test.png /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

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 B

View file

@ -161,6 +161,9 @@ namespace Demos {
PositionOffset = new Vector2(0, 1) PositionOffset = new Vector2(0, 1)
}); });
var region = new TextureRegion(LoadContent<Texture2D>("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 // Below are some querying examples that help you find certain elements easily
var children = root.GetChildren(); var children = root.GetChildren();

View file

@ -1,9 +1,21 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using MLEM.Input; using MLEM.Input;
using MLEM.Textures;
namespace MLEM.Ui.Elements { namespace MLEM.Ui.Elements {
public static class ElementHelper { 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") { 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); var box = new Panel(anchor, new Vector2(width, 1), Vector2.Zero, true);
box.AddChild(new Paragraph(Anchor.AutoLeft, 1, text)); box.AddChild(new Paragraph(Anchor.AutoLeft, 1, text));

View file

@ -1,3 +1,4 @@
using System;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions; using MLEM.Extensions;
@ -8,19 +9,26 @@ namespace MLEM.Ui.Elements {
public Color Color = Color.White; public Color Color = Color.White;
public TextureRegion Texture; 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) { public Image(Anchor anchor, Vector2 size, TextureRegion texture, bool scaleToImage = false) : base(anchor, size) {
this.Texture = texture; this.Texture = texture;
this.scaleToImage = scaleToImage; this.ScaleToImage = scaleToImage;
} }
protected override Point CalcActualSize(Rectangle parentArea) { 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) { 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); base.Draw(time, batch, alpha, offset);
} }