Extract out some logic

This commit is contained in:
Kurt 2020-04-06 22:49:33 -07:00
parent 6ecf659d5e
commit bc95507bf2
2 changed files with 83 additions and 41 deletions

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}