1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 14:38:34 +01:00

added a pivot point to TextureRegion

This commit is contained in:
Ellpeck 2020-07-23 23:30:44 +02:00
parent 9f2e2ffb0e
commit 9477b5f1d1
2 changed files with 30 additions and 6 deletions

View file

@ -41,6 +41,22 @@ namespace MLEM.Textures {
/// The height of this texture region /// The height of this texture region
/// </summary> /// </summary>
public int Height => this.Area.Height; public int Height => this.Area.Height;
/// <summary>
/// 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.
/// </summary>
public Vector2 Pivot = Vector2.Zero;
/// <summary>
/// The <see cref="Pivot"/> of this texture region, but in absolute pixels rather than percentage.
/// </summary>
public Vector2 PivotPixels {
get => this.Pivot * this.Size.ToVector2();
set => this.Pivot = value / this.Size.ToVector2();
}
/// <summary>
/// The name of this texture region. By default, this name is <see cref="string.Empty"/>.
/// </summary>
public string Name = string.Empty;
/// <summary> /// <summary>
/// Creates a new texture region from a texture and a rectangle which defines the region's area /// Creates a new texture region from a texture and a rectangle which defines the region's area
@ -88,7 +104,7 @@ namespace MLEM.Textures {
/// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/> /// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
public static void Draw(this SpriteBatch batch, TextureRegion texture, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { 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);
} }
/// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/> /// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
@ -98,12 +114,12 @@ namespace MLEM.Textures {
/// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/> /// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
public static void Draw(this SpriteBatch batch, TextureRegion texture, Rectangle destinationRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) { 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);
} }
/// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/> /// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>
public static void Draw(this SpriteBatch batch, TextureRegion texture, RectangleF destinationRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float 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);
} }
/// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/> /// <inheritdoc cref="SpriteBatch.Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)"/>

View file

@ -23,6 +23,8 @@ using MLEM.Ui.Elements;
using MLEM.Ui.Style; using MLEM.Ui.Style;
using MonoGame.Extended; using MonoGame.Extended;
using MonoGame.Extended.BitmapFonts; using MonoGame.Extended.BitmapFonts;
using MonoGame.Extended.Sprites;
using MonoGame.Extended.TextureAtlases;
using MonoGame.Extended.Tiled; using MonoGame.Extended.Tiled;
using RectangleF = MonoGame.Extended.RectangleF; using RectangleF = MonoGame.Extended.RectangleF;
@ -136,12 +138,18 @@ namespace Sandbox {
this.tokenized.Split(font, 400, sc); this.tokenized.Split(font, 400, sc);
var square = this.SpriteBatch.GenerateSquareTexture(Color.Yellow); 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.OnDraw += (g, time) => {
this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp); this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
this.SpriteBatch.Draw(square, 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(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); //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); //font.DrawString(this.SpriteBatch, this.tokenized.DisplayString, new Vector2(400, 20), Color.White * 0.25F, 0, Vector2.Zero, sc, SpriteEffects.None, 0);