Refactoring

move files, abstract some logic for later reuse
This commit is contained in:
Kurt
2020-05-11 22:14:27 -07:00
parent 46d4f4e8c1
commit 60faecdc69
18 changed files with 321 additions and 270 deletions

View File

@@ -4,6 +4,7 @@
using System.Linq;
using System.Windows.Forms;
using NHSE.Core;
using NHSE.Sprites;
namespace NHSE.WinForms
{
@@ -290,8 +291,8 @@ private void ViewTile(Item tile, int x, int y)
if (CHK_RedirectExtensionLoad.Checked && tile.IsExtension)
{
var l = Map.CurrentLayer;
var rx = Math.Max(0, Math.Min(l.MapWidth - 1, x - tile.ExtensionX));
var ry = Math.Max(0, Math.Min(l.MapHeight - 1, y - tile.ExtensionY));
var rx = Math.Max(0, Math.Min(l.MaxWidth - 1, x - tile.ExtensionX));
var ry = Math.Max(0, Math.Min(l.MaxHeight - 1, y - tile.ExtensionY));
var redir = l.GetTile(rx, ry);
if (redir.IsRoot && redir.ItemId == tile.ExtensionItemId)
tile = redir;
@@ -322,8 +323,8 @@ private void SetTile(Item tile, int x, int y)
var permission = l.IsOccupied(pgt, x, y);
switch (permission)
{
case FieldItemPermission.OutOfBounds:
case FieldItemPermission.Collision when CHK_NoOverwrite.Checked:
case PlacedItemPermission.OutOfBounds:
case PlacedItemPermission.Collision when CHK_NoOverwrite.Checked:
System.Media.SystemSounds.Asterisk.Play();
return;
}

View File

@@ -53,7 +53,7 @@ public static bool ImportToLayerAcreAll(FieldItemLayer layer)
}
var data = File.ReadAllBytes(path);
layer.ImportAllAcres(data);
layer.ImportAll(data);
return true;
}
@@ -83,11 +83,11 @@ public static void DumpLayerAcreAll(FieldItemLayer layer)
return;
var path = sfd.FileName;
var data = layer.DumpAllAcres();
var data = layer.DumpAll();
File.WriteAllBytes(path, data);
}
public static bool ImportTerrainAcre(TerrainManager m, int acreIndex, string acre)
public static bool ImportTerrainAcre(TerrainLayer m, int acreIndex, string acre)
{
using var ofd = new OpenFileDialog
{
@@ -112,7 +112,7 @@ public static bool ImportTerrainAcre(TerrainManager m, int acreIndex, string acr
return true;
}
public static bool ImportTerrainAll(TerrainManager m)
public static bool ImportTerrainAll(TerrainLayer m)
{
using var ofd = new OpenFileDialog
{
@@ -133,11 +133,11 @@ public static bool ImportTerrainAll(TerrainManager m)
}
var data = File.ReadAllBytes(path);
m.ImportAllAcres(data);
m.ImportAll(data);
return true;
}
public static void DumpTerrainAcre(TerrainManager m, int acreIndex, string acre)
public static void DumpTerrainAcre(TerrainLayer m, int acreIndex, string acre)
{
using var sfd = new SaveFileDialog
{
@@ -152,7 +152,7 @@ public static void DumpTerrainAcre(TerrainManager m, int acreIndex, string acre)
File.WriteAllBytes(path, data);
}
public static void DumpTerrainAll(TerrainManager m)
public static void DumpTerrainAll(TerrainLayer m)
{
using var sfd = new SaveFileDialog
{
@@ -163,7 +163,7 @@ public static void DumpTerrainAll(TerrainManager m)
return;
var path = sfd.FileName;
var data = m.DumpAllAcres();
var data = m.DumpAll();
File.WriteAllBytes(path, data);
}

View File

@@ -1,75 +0,0 @@
using System;
using System.Drawing;
using NHSE.Core;
using NHSE.Sprites;
namespace NHSE.WinForms
{
public sealed class MapViewer : MapView, IDisposable
{
// Cached acre view objects to remove allocation/GC
private readonly int[] PixelsItemAcre1;
private readonly int[] PixelsItemAcreX;
private readonly Bitmap ScaleAcre;
private readonly int[] PixelsItemMap;
private readonly Bitmap MapReticle;
private readonly int[] PixelsBackgroundAcre1;
private readonly int[] PixelsBackgroundAcreX;
private readonly Bitmap BackgroundAcre;
private readonly int[] PixelsBackgroundMap1;
private readonly int[] PixelsBackgroundMapX;
private readonly Bitmap BackgroundMap;
public MapViewer(MapManager m) : base(m)
{
var l1 = m.Items.Layer1;
PixelsItemAcre1 = new int[l1.GridWidth * l1.GridHeight];
PixelsItemAcreX = new int[PixelsItemAcre1.Length * AcreScale * AcreScale];
ScaleAcre = new Bitmap(l1.GridWidth * AcreScale, l1.GridHeight * AcreScale);
PixelsItemMap = new int[l1.MapWidth * l1.MapHeight * MapScale * MapScale];
MapReticle = new Bitmap(l1.MapWidth * MapScale, l1.MapHeight * MapScale);
PixelsBackgroundAcre1 = new int[16 * 16];
PixelsBackgroundAcreX = new int[PixelsItemAcreX.Length];
BackgroundAcre = new Bitmap(ScaleAcre.Width, ScaleAcre.Height);
PixelsBackgroundMap1 = new int[PixelsItemMap.Length / 4];
PixelsBackgroundMapX = new int[PixelsItemMap.Length];
BackgroundMap = new Bitmap(MapReticle.Width, MapReticle.Height);
}
public void Dispose()
{
ScaleAcre.Dispose();
MapReticle.Dispose();
BackgroundAcre.Dispose();
BackgroundMap.Dispose();
}
public Bitmap GetLayerAcre(int t) => GetLayerAcre(X, Y, t);
public Bitmap GetMapWithReticle(int t) => GetMapWithReticle(X, Y, t, Map.CurrentLayer);
public Bitmap GetBackgroundTerrain(int index = -1)
{
return TerrainSprite.GetMapWithBuildings(Map, null, PixelsBackgroundMap1, PixelsBackgroundMapX, BackgroundMap, 2, index);
}
private Bitmap GetLayerAcre(int topX, int topY, int t)
{
var layer = Map.CurrentLayer;
return FieldItemSpriteDrawer.GetBitmapItemLayerAcre(layer, topX, topY, AcreScale, PixelsItemAcre1, PixelsItemAcreX, ScaleAcre, t);
}
public Bitmap GetBackgroundAcre(Font f, byte tbuild, byte tterrain, int index = -1)
{
return TerrainSprite.GetAcre(this, f, PixelsBackgroundAcre1, PixelsBackgroundAcreX, BackgroundAcre, index, tbuild, tterrain);
}
private Bitmap GetMapWithReticle(int topX, int topY, int t, FieldItemLayer layer)
{
return FieldItemSpriteDrawer.GetBitmapItemLayer(layer, topX, topY, PixelsItemMap, MapReticle, t);
}
}
}