mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-12-25 17:59:24 +01:00
added TextureExtensions
This commit is contained in:
parent
915506d8d1
commit
dce6499a0c
2 changed files with 105 additions and 2 deletions
98
MLEM/Extensions/TextureExtensions.cs
Normal file
98
MLEM/Extensions/TextureExtensions.cs
Normal file
|
@ -0,0 +1,98 @@
|
|||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace MLEM.Extensions {
|
||||
/// <summary>
|
||||
/// A set of extensions for dealing with <see cref="Texture2D"/>
|
||||
/// </summary>
|
||||
public static class TextureExtensions {
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new instance of <see cref="TextureData"/> which allows easily managing a texture's data with texture coordinates rather than indices.
|
||||
/// </summary>
|
||||
/// <param name="texture">The texture whose data to get</param>
|
||||
/// <returns>The texture's data</returns>
|
||||
public static TextureData GetTextureData(this Texture2D texture) {
|
||||
return new TextureData(texture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A struct that represents the data of a texture, accessed through <see cref="TextureExtensions.GetTextureData"/>.
|
||||
/// </summary>
|
||||
public struct TextureData {
|
||||
|
||||
private readonly Texture2D texture;
|
||||
private readonly Color[] data;
|
||||
/// <summary>
|
||||
/// Returns the color at the given x,y position of the texture, where 0,0 represents the bottom left.
|
||||
/// </summary>
|
||||
/// <param name="x">The x coordinate of the texture location</param>
|
||||
/// <param name="y">The y coordinate of the texture location</param>
|
||||
public Color this[int x, int y] {
|
||||
get => this.data[this.ToIndex(x, y)];
|
||||
set => this.data[this.ToIndex(x, y)] = value;
|
||||
}
|
||||
/// <inheritdoc cref="this[int,int]"/>
|
||||
public Color this[Point point] {
|
||||
get => this[point.X, point.Y];
|
||||
set => this[point.X, point.Y] = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new texture data instance for the given texture.
|
||||
/// Note that this can more easily be invoked using <see cref="TextureExtensions.GetTextureData"/>.
|
||||
/// </summary>
|
||||
/// <param name="texture">The texture whose data to get</param>
|
||||
public TextureData(Texture2D texture) {
|
||||
this.texture = texture;
|
||||
this.data = new Color[texture.Width * texture.Height];
|
||||
this.texture.GetData(this.data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores this texture data back into the underlying texture
|
||||
/// </summary>
|
||||
public void Store() {
|
||||
this.texture.SetData(this.data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the given x,y texture coordinate to the corresponding index in the <see cref="Texture2D.GetData{T}(T[])"/> array.
|
||||
/// </summary>
|
||||
/// <param name="x">The x coordinate</param>
|
||||
/// <param name="y">The y coordinate</param>
|
||||
/// <returns>The corresponding texture array index</returns>
|
||||
/// <exception cref="ArgumentException">If the given coordinate is out of bounds</exception>
|
||||
public int ToIndex(int x, int y) {
|
||||
if (!this.IsInBounds(x, y))
|
||||
throw new ArgumentException();
|
||||
return y * this.texture.Width + x;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the given index from the <see cref="Texture2D.GetData{T}(T[])"/> array into the corresponding x,y texture coordinate.
|
||||
/// </summary>
|
||||
/// <param name="index">The texture array index</param>
|
||||
/// <returns>The corresponding texture coordinate</returns>
|
||||
/// <exception cref="ArgumentException">If the given index is out of bounds</exception>
|
||||
public Point FromIndex(int index) {
|
||||
if (index < 0 || index >= this.data.Length)
|
||||
throw new ArgumentException();
|
||||
return new Point(index % this.texture.Width, index / this.texture.Width);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the given x,y texture coordinates is within the bounds of the underlying texture.
|
||||
/// </summary>
|
||||
/// <param name="x">The x coordinate</param>
|
||||
/// <param name="y">The y coordinate</param>
|
||||
/// <returns>Whether the given coordinate is within bounds of the underlying texture</returns>
|
||||
public bool IsInBounds(int x, int y) {
|
||||
return x >= 0 && y >= 0 && x < this.texture.Width && y < this.texture.Height;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -62,6 +62,11 @@ namespace Sandbox {
|
|||
};
|
||||
|
||||
var tex = this.rawContent.Load<Texture2D>("Textures/Test");
|
||||
var data = tex.GetTextureData();
|
||||
data[1, 9] = Color.Pink;
|
||||
data[data.FromIndex(data.ToIndex(25, 9))] = Color.Yellow;
|
||||
data.Store();
|
||||
|
||||
//var font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
|
||||
var font = new GenericBitmapFont(LoadContent<BitmapFont>("Fonts/Regular"));
|
||||
this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) {
|
||||
|
@ -126,14 +131,14 @@ namespace Sandbox {
|
|||
this.tokenized = formatter.Tokenize(font, strg);
|
||||
this.tokenized.Split(font, 400, 5);
|
||||
|
||||
this.OnDraw += (g, time) => {
|
||||
/*this.OnDraw += (g, time) => {
|
||||
this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
|
||||
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, 5, SpriteEffects.None, 0);
|
||||
this.tokenized.Draw(time, this.SpriteBatch, new Vector2(400, 20), font, Color.White, 5, 0);
|
||||
this.SpriteBatch.DrawGrid(new Vector2(30, 30), new Vector2(40, 60), new Point(10, 5), Color.Yellow, 3);
|
||||
this.SpriteBatch.End();
|
||||
};
|
||||
};*/
|
||||
this.OnUpdate += (g, time) => {
|
||||
if (this.InputHandler.IsPressed(Keys.W)) {
|
||||
this.tokenized = formatter.Tokenize(font, strg);
|
||||
|
|
Loading…
Reference in a new issue