From bc95507bf2c1367fc4dc565a562586871a366ff4 Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 6 Apr 2020 22:49:33 -0700 Subject: [PATCH] Extract out some logic --- NHSE.Sprites/ItemSprite.cs | 90 +++++++++++++++++--------------- NHSE.Sprites/ItemSpriteDrawer.cs | 34 ++++++++++++ 2 files changed, 83 insertions(+), 41 deletions(-) create mode 100644 NHSE.Sprites/ItemSpriteDrawer.cs diff --git a/NHSE.Sprites/ItemSprite.cs b/NHSE.Sprites/ItemSprite.cs index 99a5d86..4ae155a 100644 --- a/NHSE.Sprites/ItemSprite.cs +++ b/NHSE.Sprites/ItemSprite.cs @@ -1,44 +1,62 @@ using System; using System.Drawing; using NHSE.Core; -using NHSE.Sprites.Properties; namespace NHSE.Sprites { - public class ItemSpriteDrawer : IGridItem + public static class ItemSprite { - public int Width { get; } = 32; - public int Height { get; } = 32; - - public readonly Image HoverBackground = Resources.itemHover; - - public Bitmap GetImage(Item item, Font font) + public static Bitmap GetImage(Item item, Font font, int width, int height) { if (item.ItemId == Item.NONE) - return GetNone(font); + return GetNone(font, width, height); - return CreateFake(item, font); + return CreateFake(item, font, width, height); + } + + public static Bitmap GetImage(Item item, int width, int height) + { + if (item.ItemId == Item.NONE) + return GetNone(width, height); + + return CreateFake(item, width, height); } private static readonly StringFormat Center = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; - private Bitmap GetNone(Font font) + private static Bitmap GetNone(int w, int h) => new Bitmap(w, h); + + private static Bitmap GetNone(Font font, int w, int h) { - var bmp = new Bitmap(Width, Height); + var bmp = GetNone(w, h); using var gfx = Graphics.FromImage(bmp); - gfx.DrawString("None", font, Brushes.Black, Width / 2f, Height / 2f, Center); + gfx.DrawString("None", font, Brushes.Black, w / 2f, h / 2f, Center); return bmp; } - public Bitmap CreateFake(Item item, Font font, bool slash = false) + public static Bitmap CreateFake(Item item, int width, int height, bool slash = false) { - var bmp = new Bitmap(Width, Height); + var bmp = new Bitmap(width, height); const int x1 = 0; const int y1 = 0; - int x2 = Width - 1; - int y2 = Height - 1; + int x2 = width - 1; + int y2 = height - 1; + using var gfx = Graphics.FromImage(bmp); + DrawItemAt(gfx, item, x1, y1, x2, y2, slash); + + return bmp; + } + + public static Bitmap CreateFake(Item item, Font font, int width, int height, bool slash = false) + { + var bmp = new Bitmap(width, height); + + const int x1 = 0; + const int y1 = 0; + int x2 = width - 1; + int y2 = height - 1; using var gfx = Graphics.FromImage(bmp); DrawItemAt(gfx, item, font, x1, y1, x2, y2, slash); @@ -46,20 +64,27 @@ public Bitmap CreateFake(Item item, Font font, bool slash = false) } public static void DrawItemAt(Graphics gfx, Item item, Font font, int x1, int y1, int x2, int y2, bool slash = false) + { + DrawItemAt(gfx, item, x1, y1, x2, y2, slash); + DrawInfo(gfx, font, item, x1, y1, Brushes.Black); + } + + public static void DrawItemAt(Graphics gfx, Item item, int x1, int y1, int x2, int y2, bool slash = false) { var color = GetItemColor(item); using var brush = new SolidBrush(color); DrawItem(gfx, x1, y1, x2, y2, brush); if (slash) - { - var icolor = Color.FromArgb(color.R ^ 0xFF, color.G ^ 0xFF, color.B ^ 0xFF); - using var ipen = new Pen(icolor); - DrawForwardSlash(gfx, x1, y1, x2, y2, ipen); - DrawBackwardSlash(gfx, x1, y1, x2, y2, ipen); - } + DrawX(gfx, x1, y1, x2, y2, color); + } - DrawInfo(gfx, font, item, x1, y1, Brushes.Black); + private static void DrawX(Graphics gfx, int x1, int y1, int x2, int y2, Color color) + { + var icolor = Color.FromArgb(color.R ^ 0xFF, color.G ^ 0xFF, color.B ^ 0xFF); + using var ipen = new Pen(icolor); + DrawForwardSlash(gfx, x1, y1, x2, y2, ipen); + DrawBackwardSlash(gfx, x1, y1, x2, y2, ipen); } private static void DrawItem(Graphics gfx, int x1, int y1, int x2, int y2, Brush brush) => gfx.FillRectangle(brush, x1, y1, x2 + 1, y2 + 1); @@ -90,22 +115,5 @@ private static Color GetItemColor(Item item) } private static readonly KnownColor[] Colors = (KnownColor[])Enum.GetValues(typeof(KnownColor)); - - public Bitmap GetItemArray(Item[] items, int height, Font f) - { - //var items = MapItem.GetArray(SAV.Main.Data.Slice(0x20191C, 0xA8000)); - var width = items.Length / height; - - var png = new Bitmap(width * Width, height * Height); - var gfx = Graphics.FromImage(png); - for (int i = 0; i < items.Length; i++) - { - var x = Height * (i / height); - var y = Width * (i % height); - DrawItemAt(gfx, items[i], f, x, y, x + Width - 1, y + Height - 1); - } - - return png; - } } } diff --git a/NHSE.Sprites/ItemSpriteDrawer.cs b/NHSE.Sprites/ItemSpriteDrawer.cs new file mode 100644 index 0000000..a654630 --- /dev/null +++ b/NHSE.Sprites/ItemSpriteDrawer.cs @@ -0,0 +1,34 @@ +using System.Drawing; +using NHSE.Core; +using NHSE.Sprites.Properties; + +namespace NHSE.Sprites +{ + public class ItemSpriteDrawer : IGridItem + { + public int Width { get; set; } = 32; + public int Height { get; set; } = 32; + + public readonly Image HoverBackground = Resources.itemHover; + + public Bitmap GetImage(Item item, Font font) => ItemSprite.GetImage(item, font, Width, Height); + public Bitmap GetImage(Item item) => ItemSprite.GetImage(item, Width, Height); + + public Bitmap GetItemArray(Item[] items, int height, Font f) + { + //var items = MapItem.GetArray(SAV.Main.Data.Slice(0x20191C, 0xA8000)); + var width = items.Length / height; + + var png = new Bitmap(width * Width, height * Height); + var gfx = Graphics.FromImage(png); + for (int i = 0; i < items.Length; i++) + { + var x = Height * (i / height); + var y = Width * (i % height); + ItemSprite.DrawItemAt(gfx, items[i], f, x, y, x + Width - 1, y + Height - 1); + } + + return png; + } + } +}