From 9477b5f1d1d36a2698f2dd9be136072c8ee7e0be Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 23 Jul 2020 23:30:44 +0200 Subject: [PATCH] added a pivot point to TextureRegion --- MLEM/Textures/TextureRegion.cs | 22 +++++++++++++++++++--- Sandbox/GameImpl.cs | 14 +++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/MLEM/Textures/TextureRegion.cs b/MLEM/Textures/TextureRegion.cs index 1177b45..abe25f8 100644 --- a/MLEM/Textures/TextureRegion.cs +++ b/MLEM/Textures/TextureRegion.cs @@ -41,6 +41,22 @@ namespace MLEM.Textures { /// The height of this texture region /// public int Height => this.Area.Height; + /// + /// The pivot point of this texture region, where 0, 0 is the top left and 1, 1 is the bottom right of the texture. + /// When drawing, this will be seen as the origin from where to start drawing. + /// + public Vector2 Pivot = Vector2.Zero; + /// + /// The of this texture region, but in absolute pixels rather than percentage. + /// + public Vector2 PivotPixels { + get => this.Pivot * this.Size.ToVector2(); + set => this.Pivot = value / this.Size.ToVector2(); + } + /// + /// The name of this texture region. By default, this name is . + /// + public string Name = string.Empty; /// /// Creates a new texture region from a texture and a rectangle which defines the region's area @@ -88,7 +104,7 @@ namespace MLEM.Textures { /// public static void Draw(this SpriteBatch batch, TextureRegion texture, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { - batch.Draw(texture.Texture, position, texture.Area, color, rotation, origin, scale, effects, layerDepth); + batch.Draw(texture.Texture, position, texture.Area, color, rotation, origin + texture.PivotPixels, scale, effects, layerDepth); } /// @@ -98,12 +114,12 @@ namespace MLEM.Textures { /// public static void Draw(this SpriteBatch batch, TextureRegion texture, Rectangle destinationRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) { - batch.Draw(texture.Texture, destinationRectangle, texture.Area, color, rotation, origin, effects, layerDepth); + batch.Draw(texture.Texture, destinationRectangle, texture.Area, color, rotation, origin + texture.PivotPixels, effects, layerDepth); } /// public static void Draw(this SpriteBatch batch, TextureRegion texture, RectangleF destinationRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) { - batch.Draw(texture.Texture, destinationRectangle, texture.Area, color, rotation, origin, effects, layerDepth); + batch.Draw(texture.Texture, destinationRectangle, texture.Area, color, rotation, origin + texture.PivotPixels, effects, layerDepth); } /// diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index cc6c8ea..c811dc1 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -23,6 +23,8 @@ using MLEM.Ui.Elements; using MLEM.Ui.Style; using MonoGame.Extended; using MonoGame.Extended.BitmapFonts; +using MonoGame.Extended.Sprites; +using MonoGame.Extended.TextureAtlases; using MonoGame.Extended.Tiled; using RectangleF = MonoGame.Extended.RectangleF; @@ -136,12 +138,18 @@ namespace Sandbox { this.tokenized.Split(font, 400, sc); var square = this.SpriteBatch.GenerateSquareTexture(Color.Yellow); - var round = this.SpriteBatch.GenerateCircleTexture(Color.Green, 1024); + var round = this.SpriteBatch.GenerateCircleTexture(Color.Green, 128); + + var region = new TextureRegion(round) {Pivot = new Vector2(0.5F)}; + var region2 = new TextureRegion(round); this.OnDraw += (g, time) => { this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp); - this.SpriteBatch.Draw(square, new Rectangle(10, 10, 400, 400), Color.White); - this.SpriteBatch.Draw(round, new Rectangle(10, 10, 400, 400), Color.White); + //this.SpriteBatch.Draw(square, new Rectangle(10, 10, 400, 400), Color.White); + //this.SpriteBatch.Draw(round, new Rectangle(10, 10, 400, 400), Color.White); + this.SpriteBatch.Draw(region, new Vector2(50, 50), Color.White, 0, Vector2.Zero, 0.5F, SpriteEffects.None, 0); + this.SpriteBatch.Draw(region2, new Vector2(50, 50), Color.Yellow * 0.5F, 0, Vector2.Zero, 0.5F, SpriteEffects.None, 0); + this.SpriteBatch.Draw(this.SpriteBatch.GetBlankTexture(), new Vector2(50, 50), Color.Pink); //this.SpriteBatch.FillRectangle(new RectangleF(400, 20, 400, 1000), Color.Green); //font.DrawString(this.SpriteBatch, this.tokenized.DisplayString, new Vector2(400, 20), Color.White * 0.25F, 0, Vector2.Zero, sc, SpriteEffects.None, 0);