Refactoring

Move sprite resources to subfolder for organization
Add FieldItem class (replaces MapItem), might work in a field item editor (similar to building editor)
This commit is contained in:
Kurt 2020-04-07 10:19:58 -07:00
parent bc95507bf2
commit 2ce535c67c
414 changed files with 593 additions and 452 deletions

View File

@ -0,0 +1,72 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace NHSE.Core
{
[StructLayout(LayoutKind.Explicit, Size = SIZE, Pack = 1)]
public class FieldItem
{
public const ushort EXTENSION = 0xFFFD;
public const ushort NONE = 0xFFFE;
public const int SIZE = 8;
public bool IsNone => ItemType == NONE;
public bool IsExtension => ItemType == EXTENSION;
public bool IsItem => !IsExtension;
// Item Definition
[FieldOffset(0)] public ushort ItemId;
[FieldOffset(2)] public byte Flags0;
[FieldOffset(3)] public byte Flags1;
[FieldOffset(4)] public ushort Count;
[FieldOffset(6)] public ushort UseCount;
// Field Item Definition
[FieldOffset(0)] public ushort ItemType;
[FieldOffset(2)] public byte Rotation;
[FieldOffset(3)] public byte E03;
[FieldOffset(4)] public ushort ExtensionItemId;
[FieldOffset(6)] public byte ExtensionX;
[FieldOffset(7)] public byte ExtensionY;
public ushort DisplayItemId => IsExtension ? ExtensionItemId : ItemId;
public void CopyFrom(FieldItem item)
{
ItemType = item.ItemType;
Rotation = item.Rotation;
E03 = item.E03;
ExtensionItemId = item.ExtensionItemId;
ExtensionX = item.ExtensionX;
ExtensionY = item.ExtensionY;
}
public FieldItem(ushort itemId = NONE, byte flags0 = 0, byte flags1 = 0, byte count = 0, ushort useCount = 0)
{
ItemId = itemId;
Flags0 = flags0;
Flags1 = flags1;
Count = count;
UseCount = useCount;
}
public void Delete()
{
ItemId = NONE;
Flags0 = Flags1 = 0;
Count = UseCount = 0;
}
public void CopyFrom(Item item)
{
ItemId = item.ItemId;
Flags0 = item.Flags0;
Flags1 = item.Flags1;
Count = item.Count;
UseCount = item.UseCount;
}
public static FieldItem[] GetArray(byte[] data) => data.GetArray<FieldItem>(SIZE);
public static byte[] SetArray(IReadOnlyList<FieldItem> data) => data.SetArray(SIZE);
}
}

View File

@ -13,4 +13,4 @@ public FieldItemDefinition(ushort id, string name, FieldItemKind kind)
Kind = kind;
}
}
}
}

View File

@ -5,6 +5,7 @@ public static class ItemInfo
private static readonly byte[] ItemKinds = ResourceUtil.GetBinaryResource("item_kind.bin");
public static ItemKind GetItemKind(Item item) => GetItemKind(item.ItemId);
public static ItemKind GetItemKind(FieldItem item) => GetItemKind(item.DisplayItemId);
public static ItemKind GetItemKind(ushort id)
{

View File

@ -1,15 +0,0 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace NHSE.Core
{
[StructLayout(LayoutKind.Sequential)]
public class MapItem : Item
{
public new const int SIZE = 0x10;
public ushort X1, X2, X3, X4;
public new static MapItem[] GetArray(byte[] data) => data.GetArray<MapItem>(SIZE);
public static byte[] SetArray(IReadOnlyList<MapItem> data) => data.SetArray(SIZE);
}
}

View File

@ -0,0 +1,68 @@
using System;
using System.Drawing;
using NHSE.Core;
namespace NHSE.Sprites
{
public static class FieldItemSprite
{
public static Bitmap GetImage(FieldItem item, int width, int height)
{
if (item.ItemId == Item.NONE)
return GetNone(width, height);
return CreateFake(item, width, height);
}
private static Bitmap GetNone(int w, int h) => new Bitmap(w, h);
public static Bitmap CreateFake(FieldItem item, 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, x1, y1, x2, y2, slash);
return bmp;
}
public static void DrawItemAt(Graphics gfx, FieldItem 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)
DrawX(gfx, x1, y1, x2, y2, color);
}
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);
private static void DrawForwardSlash(Graphics gfx, int x1, int y1, int x2, int y2, Pen ipen) => gfx.DrawLine(ipen, x2, y1, x1, y2);
private static void DrawBackwardSlash(Graphics gfx, int x1, int y1, int x2, int y2, Pen ipen) => gfx.DrawLine(ipen, x1, y1, x2, y2);
private static Color GetItemColor(FieldItem item)
{
var kind = ItemInfo.GetItemKind(item);
if (kind == ItemKind.Unknown)
return Color.LimeGreen;
var known = Colors[(int)kind];
var color = Color.FromKnownColor(known);
// soften the colors a little
return ColorUtil.Blend(Color.White, color, 0.5d);
}
private static readonly KnownColor[] Colors = (KnownColor[])Enum.GetValues(typeof(KnownColor));
}
}

View File

@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Drawing;
using NHSE.Core;
namespace NHSE.Sprites
{
public class FieldItemSpriteDrawer
{
public int Width { get; set; } = 8;
public int Height { get; set; } = 8;
public Bitmap GetItemArray(IReadOnlyList<FieldItem> items, int height)
{
//var items = FieldItem.GetArray(SAV.Main.Data.Slice(0x20191C, 0xA8000));
var width = items.Count / height;
var png = new Bitmap(width * Width, height * Height);
var gfx = Graphics.FromImage(png);
for (int i = 0; i < items.Count; i++)
{
var x = Height * (i / height);
var y = Width * (i % height);
FieldItemSprite.DrawItemAt(gfx, items[i], x, y, x + Width - 1, y + Height - 1);
}
return png;
}
}
}

View File

@ -0,0 +1,17 @@
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);
}
}

View File

@ -1,34 +0,0 @@
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;
}
}
}

View File

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="Villagers\" />
<Folder Include="Resources\Villagers\" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">

File diff suppressed because it is too large Load Diff

View File

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

Before

Width:  |  Height:  |  Size: 5.1 KiB

After

Width:  |  Height:  |  Size: 5.1 KiB

View File

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 5.3 KiB

View File

Before

Width:  |  Height:  |  Size: 6.2 KiB

After

Width:  |  Height:  |  Size: 6.2 KiB

View File

Before

Width:  |  Height:  |  Size: 5.1 KiB

After

Width:  |  Height:  |  Size: 5.1 KiB

View File

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 4.6 KiB

View File

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 5.8 KiB

View File

Before

Width:  |  Height:  |  Size: 8.3 KiB

After

Width:  |  Height:  |  Size: 8.3 KiB

View File

Before

Width:  |  Height:  |  Size: 7.8 KiB

After

Width:  |  Height:  |  Size: 7.8 KiB

View File

Before

Width:  |  Height:  |  Size: 8.9 KiB

After

Width:  |  Height:  |  Size: 8.9 KiB

View File

Before

Width:  |  Height:  |  Size: 7.7 KiB

After

Width:  |  Height:  |  Size: 7.7 KiB

View File

Before

Width:  |  Height:  |  Size: 8.5 KiB

After

Width:  |  Height:  |  Size: 8.5 KiB

View File

Before

Width:  |  Height:  |  Size: 8.3 KiB

After

Width:  |  Height:  |  Size: 8.3 KiB

View File

Before

Width:  |  Height:  |  Size: 7.2 KiB

After

Width:  |  Height:  |  Size: 7.2 KiB

View File

Before

Width:  |  Height:  |  Size: 9.0 KiB

After

Width:  |  Height:  |  Size: 9.0 KiB

View File

Before

Width:  |  Height:  |  Size: 8.3 KiB

After

Width:  |  Height:  |  Size: 8.3 KiB

View File

Before

Width:  |  Height:  |  Size: 8.9 KiB

After

Width:  |  Height:  |  Size: 8.9 KiB

View File

Before

Width:  |  Height:  |  Size: 7.9 KiB

After

Width:  |  Height:  |  Size: 7.9 KiB

View File

Before

Width:  |  Height:  |  Size: 8.5 KiB

After

Width:  |  Height:  |  Size: 8.5 KiB

View File

Before

Width:  |  Height:  |  Size: 8.4 KiB

After

Width:  |  Height:  |  Size: 8.4 KiB

View File

Before

Width:  |  Height:  |  Size: 8.7 KiB

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

Before

Width:  |  Height:  |  Size: 7.3 KiB

After

Width:  |  Height:  |  Size: 7.3 KiB

View File

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 5.7 KiB

View File

Before

Width:  |  Height:  |  Size: 6.5 KiB

After

Width:  |  Height:  |  Size: 6.5 KiB

View File

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View File

Before

Width:  |  Height:  |  Size: 5.6 KiB

After

Width:  |  Height:  |  Size: 5.6 KiB

View File

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 6.1 KiB

View File

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

Before

Width:  |  Height:  |  Size: 7.2 KiB

After

Width:  |  Height:  |  Size: 7.2 KiB

View File

Before

Width:  |  Height:  |  Size: 6.6 KiB

After

Width:  |  Height:  |  Size: 6.6 KiB

View File

Before

Width:  |  Height:  |  Size: 5.2 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

Before

Width:  |  Height:  |  Size: 6.0 KiB

After

Width:  |  Height:  |  Size: 6.0 KiB

View File

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View File

Before

Width:  |  Height:  |  Size: 8.9 KiB

After

Width:  |  Height:  |  Size: 8.9 KiB

View File

Before

Width:  |  Height:  |  Size: 8.7 KiB

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

Before

Width:  |  Height:  |  Size: 7.6 KiB

After

Width:  |  Height:  |  Size: 7.6 KiB

View File

Before

Width:  |  Height:  |  Size: 9.2 KiB

After

Width:  |  Height:  |  Size: 9.2 KiB

View File

Before

Width:  |  Height:  |  Size: 9.0 KiB

After

Width:  |  Height:  |  Size: 9.0 KiB

View File

Before

Width:  |  Height:  |  Size: 7.1 KiB

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

Before

Width:  |  Height:  |  Size: 8.8 KiB

After

Width:  |  Height:  |  Size: 8.8 KiB

View File

Before

Width:  |  Height:  |  Size: 8.4 KiB

After

Width:  |  Height:  |  Size: 8.4 KiB

View File

Before

Width:  |  Height:  |  Size: 8.0 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

Before

Width:  |  Height:  |  Size: 7.0 KiB

After

Width:  |  Height:  |  Size: 7.0 KiB

View File

Before

Width:  |  Height:  |  Size: 9.4 KiB

After

Width:  |  Height:  |  Size: 9.4 KiB

View File

Before

Width:  |  Height:  |  Size: 8.5 KiB

After

Width:  |  Height:  |  Size: 8.5 KiB

View File

Before

Width:  |  Height:  |  Size: 7.8 KiB

After

Width:  |  Height:  |  Size: 7.8 KiB

View File

Before

Width:  |  Height:  |  Size: 9.8 KiB

After

Width:  |  Height:  |  Size: 9.8 KiB

View File

Before

Width:  |  Height:  |  Size: 9.1 KiB

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

Before

Width:  |  Height:  |  Size: 9.6 KiB

After

Width:  |  Height:  |  Size: 9.6 KiB

View File

Before

Width:  |  Height:  |  Size: 9.1 KiB

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

Before

Width:  |  Height:  |  Size: 9.1 KiB

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

Before

Width:  |  Height:  |  Size: 9.2 KiB

After

Width:  |  Height:  |  Size: 9.2 KiB

View File

Before

Width:  |  Height:  |  Size: 8.8 KiB

After

Width:  |  Height:  |  Size: 8.8 KiB

View File

Before

Width:  |  Height:  |  Size: 8.1 KiB

After

Width:  |  Height:  |  Size: 8.1 KiB

View File

Before

Width:  |  Height:  |  Size: 8.1 KiB

After

Width:  |  Height:  |  Size: 8.1 KiB

View File

Before

Width:  |  Height:  |  Size: 8.5 KiB

After

Width:  |  Height:  |  Size: 8.5 KiB

View File

Before

Width:  |  Height:  |  Size: 7.6 KiB

After

Width:  |  Height:  |  Size: 7.6 KiB

View File

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 8.2 KiB

View File

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

Before

Width:  |  Height:  |  Size: 8.0 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 8.2 KiB

View File

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 8.2 KiB

View File

Before

Width:  |  Height:  |  Size: 8.0 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

Before

Width:  |  Height:  |  Size: 7.9 KiB

After

Width:  |  Height:  |  Size: 7.9 KiB

View File

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 7.4 KiB

View File

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 7.4 KiB

View File

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 7.4 KiB

View File

Before

Width:  |  Height:  |  Size: 7.6 KiB

After

Width:  |  Height:  |  Size: 7.6 KiB

View File

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

Before

Width:  |  Height:  |  Size: 8.1 KiB

After

Width:  |  Height:  |  Size: 8.1 KiB

View File

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 7.4 KiB

View File

Before

Width:  |  Height:  |  Size: 8.1 KiB

After

Width:  |  Height:  |  Size: 8.1 KiB

View File

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

Before

Width:  |  Height:  |  Size: 8.0 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

Before

Width:  |  Height:  |  Size: 6.3 KiB

After

Width:  |  Height:  |  Size: 6.3 KiB

View File

Before

Width:  |  Height:  |  Size: 6.9 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

Before

Width:  |  Height:  |  Size: 7.8 KiB

After

Width:  |  Height:  |  Size: 7.8 KiB

View File

Before

Width:  |  Height:  |  Size: 7.8 KiB

After

Width:  |  Height:  |  Size: 7.8 KiB

View File

Before

Width:  |  Height:  |  Size: 7.0 KiB

After

Width:  |  Height:  |  Size: 7.0 KiB

View File

Before

Width:  |  Height:  |  Size: 6.8 KiB

After

Width:  |  Height:  |  Size: 6.8 KiB

View File

Before

Width:  |  Height:  |  Size: 7.9 KiB

After

Width:  |  Height:  |  Size: 7.9 KiB

Some files were not shown because too many files have changed in this diff Show More