Skip to content

Instantly share code, notes, and snippets.

@tedigc
Last active May 8, 2022 19:15
Show Gist options
  • Select an option

  • Save tedigc/4c1b1dfc9372d7d91d7465fed038d361 to your computer and use it in GitHub Desktop.

Select an option

Save tedigc/4c1b1dfc9372d7d91d7465fed038d361 to your computer and use it in GitHub Desktop.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using rpg.Source.Assets;
namespace rpg {
public class NinePatch {
private const int DefaultSpacing = 1;
private const int Bleed = 1;
private readonly Sprite sprite;
private readonly Rectangle topLeft, top, topRight;
private readonly Rectangle midLeft, mid, midRight;
private readonly Rectangle botLeft, bot, botRight;
public NinePatch(string spriteRef, int top, int bottom, int left, int right, int spacing = DefaultSpacing) {
sprite = Assets.GetSprite(spriteRef);
Rectangle spriteRect = sprite.Rectangle;
int midWidth = spriteRect.Width - (left + right + (spacing + Bleed) * 2);
int midHeight = spriteRect.Height - (top + bottom + (spacing + Bleed) * 2);
int left1 = spriteRect.X;
int left2 = spriteRect.X + left + spacing + Bleed;
int left3 = spriteRect.X + left + midWidth + (spacing + Bleed) * 2;
int top1 = spriteRect.Y;
int top2 = spriteRect.Y + top + spacing + Bleed;
int top3 = spriteRect.Y + top + midHeight + (spacing + Bleed) * 2;
this.topLeft = new Rectangle(left1, top1, left, top);
this.top = new Rectangle(left2, top1, midWidth, top);
this.topRight = new Rectangle(left3, top1, right, top);
this.midLeft = new Rectangle(left1, top2, left, midHeight);
this.mid = new Rectangle(left2, top2, midWidth, midHeight);
this.midRight = new Rectangle(left3, top2, right, midHeight);
this.botLeft = new Rectangle(left1, top3, left, bottom);
this.bot = new Rectangle(left2, top3, midWidth, bottom);
this.botRight = new Rectangle(left3, top3, right, bottom);
}
public void Draw(int x, int y, int w, int h) {
float scaleX = (w - (topLeft.Width + topRight.Width)) / (float) top.Width;
float scaleY = (h - (topLeft.Height + botLeft.Height)) / (float) mid.Height;
DrawPatch(topLeft, new Vector2(x, y), Vector2.One);
DrawPatch(top, new Vector2(x + topLeft.Width, y), new Vector2(scaleX, 1));
DrawPatch(topRight, new Vector2(x + w - topRight.Width, y), Vector2.One);
DrawPatch(midLeft, new Vector2(x, y + topLeft.Height), new Vector2(1, scaleY));
DrawPatch(mid, new Vector2(x + midLeft.Width, y + topLeft.Height), new Vector2(scaleX, scaleY));
DrawPatch(midRight, new Vector2(x + w - midRight.Width, y + topLeft.Height), new Vector2(1, scaleY));
DrawPatch(botLeft, new Vector2(x, y + (h - botLeft.Height)), Vector2.One);
DrawPatch(bot, new Vector2(x + botLeft.Width, y + (h - botLeft.Height)), new Vector2(scaleX, 1));
DrawPatch(botRight, new Vector2(x + w - botRight.Width, y + (h - botLeft.Height)), Vector2.One);
}
private void DrawPatch(Rectangle rect, Vector2 pos, Vector2 scale) {
Texture2D texture = sprite.Texture;
Main.sb.Draw(texture, pos, rect, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment