Core - Refactor ResourceLocation

This commit is contained in:
miku-666
2025-11-20 02:45:41 +01:00
parent 1abd4efd94
commit 1edc12b4ff
13 changed files with 194 additions and 167 deletions

View File

@@ -217,7 +217,7 @@ namespace PckStudio.Controls
return;
}
ResourceLocation resourceLocation = ResourceLocation.GetFromPath(asset.Filename);
ResourceLocation resourceLocation = ResourceLocations.GetFromPath(asset.Filename);
Debug.WriteLine("Handling Resource file: " + resourceLocation?.ToString());
switch (resourceLocation.Category)
@@ -942,7 +942,7 @@ namespace PckStudio.Controls
if (asset.Type != PckAssetType.TextureFile)
break;
ResourceLocation resourceLocation = ResourceLocation.GetFromPath(asset.Filename);
ResourceLocation resourceLocation = ResourceLocations.GetFromPath(asset.Filename);
if (resourceLocation is null || resourceLocation.Category == ResourceCategory.Unknown)
break;
@@ -1279,7 +1279,7 @@ namespace PckStudio.Controls
if (diag.ShowDialog(this) != DialogResult.OK)
return;
string animationFilepath = $"{ResourceLocation.GetPathFromCategory(diag.Category)}/{diag.SelectedTile.InternalName}.png";
string animationFilepath = $"{ResourceLocations.GetPathFromCategory(diag.Category)}/{diag.SelectedTile.InternalName}.png";
if (EditorValue.File.Contains(animationFilepath, PckAssetType.TextureFile))
{
@@ -1771,7 +1771,7 @@ namespace PckStudio.Controls
/// <returns>True if the remove should be canceled, otherwise False</returns>
private bool BeforeFileRemove(PckAsset asset)
{
string itemPath = ResourceLocation.GetPathFromCategory(ResourceCategory.ItemAnimation);
string itemPath = ResourceLocations.GetPathFromCategory(ResourceCategory.ItemAnimation);
// warn the user about deleting compass.png and clock.png
if (asset.Type == PckAssetType.TextureFile &&

View File

@@ -5,6 +5,8 @@ using PckStudio.Core;
using PckStudio.Core.Json;
using PckStudio.Json;
using PckStudio.Core.Extensions;
using System.Drawing;
using System.Linq;
namespace PckStudio.Forms.Additional_Popups.Animation
{
@@ -48,14 +50,14 @@ namespace PckStudio.Forms.Additional_Popups.Animation
private void GetTileDataToView(ResourceCategory key)
{
(List<JsonTileInfo> textureInfos, ImageList imgList, string name) = key switch
(List<JsonTileInfo> textureInfos, Image[] images, string name) = key switch
{
ResourceCategory.BlockAnimation => (Tiles.BlockTileInfos, Tiles.BlockImageList, "Blocks"),
ResourceCategory.ItemAnimation => (Tiles.ItemTileInfos, Tiles.ItemImageList, "Items"),
ResourceCategory.BlockAnimation => (Tiles.BlockTileInfos, ((AtlasResource)ResourceLocations.GetFromCategory(ResourceCategory.BlockAtlas)).GetDefaultAtlas().GetTiles().Select(t => t.Texture).ToArray(), "Blocks"),
ResourceCategory.ItemAnimation => (Tiles.ItemTileInfos, ((AtlasResource)ResourceLocations.GetFromCategory(ResourceCategory.ItemAtlas)).GetDefaultAtlas().GetTiles().Select(t => t.Texture).ToArray(), "Items"),
_ => throw new InvalidOperationException(nameof(key))
};
TreeView view = filterPrompt.AddFilterPage(name, null, filterPredicate);
view.ImageList = imgList;
view.ImageList = images.ToImageList();
foreach ((int i, JsonTileInfo tileData) in textureInfos?.enumerate())
{
if (string.IsNullOrEmpty(tileData.InternalName) || view.Nodes.ContainsKey(tileData.InternalName))

View File

@@ -151,7 +151,7 @@ namespace PckStudio.Forms.Editor
{
ResourceCategory animationResourceCategory = _resourceLocationCategory == ResourceCategory.ItemAtlas ? ResourceCategory.ItemAnimation : ResourceCategory.BlockAnimation;
string animationAssetPath = $"{ResourceLocation.GetPathFromCategory(animationResourceCategory)}/{tileInfo.InternalName}";
string animationAssetPath = $"{ResourceLocations.GetPathFromCategory(animationResourceCategory)}/{tileInfo.InternalName}";
bool hasAnimation = _tryGetAnimation.TryGet(animationAssetPath, out Animation animation);
animationButton.Text = hasAnimation ? "Edit Animation" : "Create Animation";
@@ -458,7 +458,7 @@ namespace PckStudio.Forms.Editor
}
JsonTileInfo tileInfo = _selectedTile.GetUserDataOfType<JsonTileInfo>();
ResourceCategory animationResourceCategory = _resourceLocationCategory == ResourceCategory.ItemAtlas ? ResourceCategory.ItemAnimation : ResourceCategory.BlockAnimation;
string animationAssetPath = $"{ResourceLocation.GetPathFromCategory(animationResourceCategory)}/{tileInfo.InternalName}";
string animationAssetPath = $"{ResourceLocations.GetPathFromCategory(animationResourceCategory)}/{tileInfo.InternalName}";
bool hasAnimation = _tryGetAnimation.TryGet(animationAssetPath, out Animation animation);
bool isValidAnimationSaveContext = _tryGetAnimationSaveContext.TryGet(animationAssetPath, out ISaveContext<Animation> animationSaveContext);

View File

@@ -36,14 +36,6 @@ namespace PckStudio.Internal.App
_ = Tiles.JsonMapIconData;
_ = Tiles.JsonExperienceOrbData;
_ = Tiles.JsonPaintingData;
_ = Tiles.BlockImageList;
_ = Tiles.ItemImageList;
_ = Tiles.ParticleImageList;
_ = Tiles.ExplosionImageList;
_ = Tiles.MapIconImageList;
_ = Tiles.ExperienceOrbImageList;
_ = Tiles.MoonPhaseImageList;
_ = Tiles.PaintingImageList;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
Task.Run(GetContributors);
}

View File

@@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using PckStudio.Core.Extensions;
@@ -57,13 +58,14 @@ namespace PckStudio.Core
public static Atlas FromResourceLocation(Image source, ResourceLocation resourceLocation, ImageLayoutDirection imageLayout = default)
{
Json.JsonTileInfo[] tilesInfo = resourceLocation.TilesInfo.ToArray();
Size tileArea = resourceLocation.GetTileArea(source.Size);
AtlasResource atlasResource = resourceLocation as AtlasResource ?? throw new InvalidDataException(nameof(resourceLocation));
Json.JsonTileInfo[] tilesInfo = atlasResource.TilesInfo.ToArray();
Size tileArea = atlasResource.GetTileArea(source.Size);
int rows = source.Width / tileArea.Width;
int columns = source.Height / tileArea.Height;
IEnumerable<AtlasTile> tiles = source.Split(tileArea, imageLayout).enumerate().Select(((int index, Image img) data) => new AtlasTile(data.img, GetSelectedPoint(data.index, out int col, rows, columns, imageLayout), col, index: data.index, userData: tilesInfo.IndexInRange(data.index) ? tilesInfo[data.index] : default));
var atlas = new Atlas(resourceLocation.Path, rows, columns, tiles, tileArea, imageLayout);
atlas.AddGroups(resourceLocation.AtlasGroups);
var atlas = new Atlas(atlasResource.Path, rows, columns, tiles, tileArea, imageLayout);
atlas.AddGroups(atlasResource.AtlasGroups);
return atlas;
}

View File

@@ -0,0 +1,52 @@
/* Copyright (c) 2024-present miku-666
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1.The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using PckStudio.Core.Deserializer;
using PckStudio.Core.Json;
namespace PckStudio.Core
{
public sealed class AtlasResource : ResourceLocation
{
public IEnumerable<JsonTileInfo> TilesInfo { get; }
public AtlasGroup[] AtlasGroups { get; }
public readonly Image DefaultTexture;
public AtlasResource(string path, ResourceCategory resourceCategory, int tillingFactor, Image defaultTexture, TillingMode tilling = default, IEnumerable<JsonTileInfo> tilesInfo = default, AtlasGroup[] atlasGroups = default)
: this(path, resourceCategory, new Size(tillingFactor, tillingFactor), defaultTexture, tilling, tilesInfo, atlasGroups)
{
}
public AtlasResource(string path, ResourceCategory resourceCategory, Size tillingFactor, Image defaultTexture, TillingMode tilling = default, IEnumerable<JsonTileInfo> tilesInfo = default, AtlasGroup[] atlasGroups = default)
: base(path, resourceCategory, tillingFactor, tilling, isGroup: true)
{
TilesInfo = tilesInfo ?? Enumerable.Empty<JsonTileInfo>();
AtlasGroups = atlasGroups ?? Array.Empty<AtlasGroup>();
DefaultTexture = defaultTexture;
}
public Atlas GetDefaultAtlas()
{
return Atlas.FromResourceLocation(DefaultTexture, this);
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -11,11 +12,11 @@ namespace PckStudio.Core.Deserializer
{
public sealed class AtlasDeserializer : IPckAssetDeserializer<Atlas>
{
private readonly ResourceLocation _resourceLocation;
private readonly AtlasResource _resourceLocation;
public AtlasDeserializer(ResourceLocation resourceLocation)
{
_resourceLocation = resourceLocation;
_resourceLocation = resourceLocation as AtlasResource ?? throw new InvalidDataException(nameof(resourceLocation));
}
public Atlas Deserialize(PckAsset asset) => Atlas.FromResourceLocation(asset.GetTexture(), _resourceLocation);

View File

@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace PckStudio.Core.Extensions
{
@@ -15,6 +17,17 @@ namespace PckStudio.Core.Extensions
yield break;
}
public static ImageList ToImageList(this Image[] images)
{
ImageList imageList = new ImageList
{
ColorDepth = ColorDepth.Depth32Bit
};
imageList.Images.AddRange(images);
return imageList;
}
public static bool EqualsAny<T>(this T type, params T[] items)
{
foreach (T item in items)

View File

@@ -0,0 +1,29 @@
/* Copyright (c) 2024-present miku-666
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1.The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
using System.Drawing;
namespace PckStudio.Core
{
public sealed class GroupResource : ResourceLocation
{
public GroupResource(string path, ResourceCategory category)
: base(path, category, Size.Empty, default, true)
{
}
}
}

View File

@@ -58,6 +58,7 @@
<Compile Include="AtlasGroupAnimation.cs" />
<Compile Include="AtlasGroupLargeTile.cs" />
<Compile Include="AtlasGroupLargeTileAnimation.cs" />
<Compile Include="AtlasResource.cs" />
<Compile Include="AtlasTile.cs" />
<Compile Include="BoundingBox.cs" />
<Compile Include="ConsolePlatform.cs" />
@@ -77,6 +78,7 @@
<Compile Include="DLC\UnknownDLCPackage.cs" />
<Compile Include="Extensions\AnimationExtensions.cs" />
<Compile Include="Extensions\BlendMode.cs" />
<Compile Include="Extensions\BlendOption.cs" />
<Compile Include="Extensions\BoundingBoxExtensions.cs" />
<Compile Include="Extensions\ColorExtensions.cs" />
<Compile Include="Extensions\CursorExtensions.cs" />
@@ -113,6 +115,7 @@
<Compile Include="GameRule\SpawnPositionSet.cs" />
<Compile Include="GameRule\UpdatePlayer.cs" />
<Compile Include="GameRule\WorldPosition.cs" />
<Compile Include="GroupResource.cs" />
<Compile Include="Interfaces\IDLCPackage.cs" />
<Compile Include="Interfaces\IDLCPackageSerialization.cs" />
<Compile Include="Interfaces\IEditor.cs" />

View File

@@ -25,6 +25,7 @@ namespace PckStudio.Core
BlockAnimation,
MobEntityTextures,
ItemEntityTextures,
ArmorTextures,
ItemAtlas,
BlockAtlas,
ParticleAtlas,

View File

@@ -17,19 +17,47 @@
**/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using PckStudio.Core.Json;
using PckStudio.Core.Properties;
using PckStudio.Json;
using static PckStudio.Core.ResourceLocation;
namespace PckStudio.Core
{
public sealed class ResourceLocation
public static class ResourceLocations
{
private static List<ResourceLocation> ResourceGroups = new List<ResourceLocation>();
private static readonly ResourceLocation Unknown = new ResourceLocation(string.Empty, ResourceCategory.Unknown, -1);
static ResourceLocations()
{
_all = new ResourceLocation[] {
new GroupResource("textures/items", ResourceCategory.ItemAnimation),
new GroupResource("textures/blocks", ResourceCategory.BlockAnimation),
new GroupResource("mob", ResourceCategory.MobEntityTextures),
new GroupResource("item", ResourceCategory.ItemEntityTextures),
new GroupResource("armor", ResourceCategory.ArmorTextures),
new AtlasResource("terrain.png", ResourceCategory.BlockAtlas, 16, Resources.terrain_atlas, tilesInfo: Tiles.BlockTileInfos, atlasGroups: _terrainAtlasGroups),
new AtlasResource("items.png", ResourceCategory.ItemAtlas, 16, Resources.items_atlas, tilesInfo: Tiles.ItemTileInfos, atlasGroups: _itemsAtlasGroups),
new AtlasResource("particles.png", ResourceCategory.ParticleAtlas, 16, Resources.particles_atlas, tilesInfo: Tiles.ParticleTileInfos, atlasGroups: _particaleAtlasGroups),
new AtlasResource("item/banner/Banner_Atlas.png", ResourceCategory.BannerAtlas, new Size(6, 7), Resources.banners_atlas, TillingMode.WidthAndHeight, tilesInfo: Tiles.BannerTileInfos),
new AtlasResource("art/kz.png", ResourceCategory.PaintingAtlas, 16, Resources.paintings_atlas, tilesInfo: Tiles.PaintingTileInfos, atlasGroups: _paintingAtlasGroups),
new AtlasResource("misc/explosion.png", ResourceCategory.ExplosionAtlas, 16, Resources.explosions_atlas, tilesInfo: Tiles.ExplosionTileInfos),
new AtlasResource("item/xporb.png", ResourceCategory.ExperienceOrbAtlas, 4, Resources.experience_orbs_atlas, tilesInfo: Tiles.ExperienceOrbTileInfos),
new AtlasResource("terrain/moon_phases.png", ResourceCategory.MoonPhaseAtlas, 4, Resources.moon_phases_atlas, tilesInfo: Tiles.MoonPhaseTileInfos),
new AtlasResource("misc/mapicons.png", ResourceCategory.MapIconAtlas, 4, Resources.map_icons_atlas, tilesInfo: Tiles.MapIconTileInfos),
new AtlasResource("misc/additionalmapicons.png", ResourceCategory.AdditionalMapIconsAtlas, 4, Resources.additional_map_icons_atlas, tilesInfo: Tiles.AdditionalMapIconTileInfos),
};
}
private static AtlasGroup[] _particaleAtlasGroups =
public static ResourceLocation GetFromCategory(ResourceCategory category) => ResourceLocation.GetFromCategory(category);
public static ResourceLocation GetFromPath(string path) => ResourceLocation.GetFromPath(path);
public static ResourceCategory GetCategoryFromPath(string path) => ResourceLocation.GetCategoryFromPath(path);
public static string GetPathFromCategory(ResourceCategory category) => ResourceLocation.GetPathFromCategory(category);
private static readonly AtlasGroup[] _particaleAtlasGroups =
{
new AtlasGroupAnimation("generic" , row: 0, column: 0, frameCount: 8, ImageLayoutDirection.Horizontal, 2),
new AtlasGroupAnimation("splash" , row: 3, column: 1, frameCount: 4, ImageLayoutDirection.Horizontal, 2),
@@ -43,7 +71,7 @@ namespace PckStudio.Core
new AtlasGroupLargeTileAnimation("bubble_pop", row: 6, column: 6, rowSpan: 2, columnSpan: 2, frameCount: 5, ImageLayoutDirection.Horizontal, 2),
};
private static AtlasGroup[] _terrainAtlasGroups =
private static readonly AtlasGroup[] _terrainAtlasGroups =
{
new AtlasGroupLargeTile("Oak Door" , row: 1, column: 5, rowSpan: 1, columnSpan: 2),
new AtlasGroupLargeTile("Iron Door" , row: 2, column: 5, rowSpan: 1, columnSpan: 2),
@@ -67,12 +95,12 @@ namespace PckStudio.Core
new AtlasGroupAnimation("Destroy" , row: 0, column: 15, frameCount: 10, ImageLayoutDirection.Horizontal, 3),
};
private static AtlasGroup[] _itemsAtlasGroups =
private static readonly AtlasGroup[] _itemsAtlasGroups =
{
new AtlasGroupAnimation("Bow Pulling", row: 5, column: 6, frameCount: 3, ImageLayoutDirection.Vertical, 6),
};
private static AtlasGroup[] _paintingAtlasGroups =
private static readonly AtlasGroup[] _paintingAtlasGroups =
{
new AtlasGroupLargeTile("The Pool" , row: 0, column: 2, rowSpan: 2, columnSpan: 1),
new AtlasGroupLargeTile("Bonjour Monsiuer Courbet", row: 2, column: 2, rowSpan: 2, columnSpan: 1),
@@ -82,7 +110,7 @@ namespace PckStudio.Core
new AtlasGroupLargeTile("Wanderer" , row: 0, column: 4, rowSpan: 1, columnSpan: 2),
new AtlasGroupLargeTile("Graham" , row: 1, column: 4, rowSpan: 1, columnSpan: 2),
new AtlasGroupLargeTile("Fighters" , row: 0, column: 6, rowSpan: 4, columnSpan: 2),
new AtlasGroupLargeTile("Match" , row: 0, column: 8, rowSpan: 2, columnSpan: 2),
@@ -101,39 +129,16 @@ namespace PckStudio.Core
new AtlasGroupLargeTile("Skull On Fire" , row: 8, column: 12, rowSpan: 4, columnSpan: 4),
};
private static readonly Dictionary<string, ResourceLocation> _categoryLookUp = new Dictionary<string, ResourceLocation>()
{
["textures/items"] = new ResourceLocation("textures/items", ResourceCategory.ItemAnimation, 16, isGroup: true),
["textures/blocks"] = new ResourceLocation("textures/blocks", ResourceCategory.BlockAnimation, 16, isGroup: true),
["mob"] = new ResourceLocation("mob", ResourceCategory.MobEntityTextures, 1, isGroup: true),
["item"] = new ResourceLocation("item", ResourceCategory.ItemEntityTextures, 1, isGroup: true),
["terrain.png"] = new ResourceLocation("terrain.png", ResourceCategory.BlockAtlas, 16, tilesInfo: Tiles.BlockTileInfos, atlasGroups: _terrainAtlasGroups),
["items.png"] = new ResourceLocation("items.png", ResourceCategory.ItemAtlas, 16, tilesInfo: Tiles.ItemTileInfos, atlasGroups: _itemsAtlasGroups),
["particles.png"] = new ResourceLocation("particles.png", ResourceCategory.ParticleAtlas, 16, tilesInfo: Tiles.ParticleTileInfos, atlasGroups: _particaleAtlasGroups),
//============ TODO ============//
["item/banner/Banner_Atlas.png"] = new ResourceLocation("item/banner/Banner_Atlas.png", ResourceCategory.BannerAtlas, new Size(6, 7), TillingMode.WidthAndHeight, tilesInfo: Tiles.BannerTileInfos),
//==============================//
["art/kz.png"] = new ResourceLocation("art/kz.png", ResourceCategory.PaintingAtlas, 16, tilesInfo: Tiles.PaintingTileInfos, atlasGroups: _paintingAtlasGroups),
["misc/explosion.png"] = new ResourceLocation("misc/explosion.png", ResourceCategory.ExplosionAtlas, 16, tilesInfo: Tiles.ExplosionTileInfos),
["item/xporb.png"] = new ResourceLocation("item/xporb.png", ResourceCategory.ExperienceOrbAtlas, 4, tilesInfo: Tiles.ExperienceOrbTileInfos),
["terrain/moon_phases.png"] = new ResourceLocation("terrain/moon_phases.png", ResourceCategory.MoonPhaseAtlas, 4, tilesInfo: Tiles.MoonPhaseTileInfos),
["misc/mapicons.png"] = new ResourceLocation("misc/mapicons.png", ResourceCategory.MapIconAtlas, 4, tilesInfo: Tiles.MapIconTileInfos),
["misc/additionalmapicons.png"] = new ResourceLocation("misc/additionalmapicons.png", ResourceCategory.AdditionalMapIconsAtlas, 4, tilesInfo: Tiles.AdditionalMapIconTileInfos),
};
private static readonly ResourceLocation[] _all;
}
public static string GetPathFromCategory(ResourceCategory category) => GetFromCategory(category).ToString();
public static ResourceCategory GetCategoryFromPath(string path) => GetFromPath(path).Category;
public static ResourceLocation GetFromPath(string path)
{
if (string.IsNullOrWhiteSpace(path) || !path.StartsWith("res/"))
return Unknown;
string categoryPath = path.Substring("res/".Length);
if (_categoryLookUp.ContainsKey(categoryPath))
return _categoryLookUp[categoryPath];
return ResourceGroups.Where(group => categoryPath.StartsWith(group.Path)).FirstOrDefault() ?? Unknown;
}
public class ResourceLocation
{
internal const string RESOURCE_PATH_PREFIX = "res/";
private static List<ResourceLocation> ResourceGroups = new List<ResourceLocation>();
private static readonly ResourceLocation Unknown = new ResourceLocation(string.Empty, ResourceCategory.Unknown, -1);
private static readonly Dictionary<string, ResourceLocation> _pathLookUp = new Dictionary<string, ResourceLocation>();
private static readonly Dictionary<ResourceCategory, ResourceLocation> _categoryLookUp = new Dictionary<ResourceCategory, ResourceLocation>();
public enum TillingMode
{
@@ -143,12 +148,11 @@ namespace PckStudio.Core
}
public string Path { get; }
public string FullPath => System.IO.Path.Combine(RESOURCE_PATH_PREFIX, Path);
public ResourceCategory Category { get; }
public Size TillingFactor { get; }
public TillingMode Tilling { get; }
public bool IsGroup { get; }
public IEnumerable<JsonTileInfo> TilesInfo { get; }
public IEnumerable<AtlasGroup> AtlasGroups { get; }
public Size GetTileArea(Size imgSize)
{
@@ -161,50 +165,47 @@ namespace PckStudio.Core
};
}
private ResourceLocation(string path, ResourceCategory category, int tillingFactor, TillingMode tilling = TillingMode.Width, bool isGroup = false, IEnumerable<JsonTileInfo> tilesInfo = default, IEnumerable<AtlasGroup> atlasGroups = default)
: this(path, category, new Size(tillingFactor, tillingFactor), tilling, isGroup, tilesInfo, atlasGroups)
protected ResourceLocation(string path, ResourceCategory category, int tillingFactor, TillingMode tilling = default, bool isGroup = false)
: this(path, category, new Size(tillingFactor, tillingFactor), tilling, isGroup)
{
}
private ResourceLocation(string path, ResourceCategory category, Size tillingFactor, TillingMode tilling = TillingMode.Width, bool isGroup = false, IEnumerable<JsonTileInfo> tilesInfo = default, IEnumerable<AtlasGroup> atlasGroups = default)
protected ResourceLocation(string path, ResourceCategory category, Size tillingFactor, TillingMode tilling = default, bool isGroup = false)
{
Path = path;
Category = category;
TillingFactor = new Size(Math.Max(1, tillingFactor.Width), Math.Max(1, tillingFactor.Height));
Tilling = tilling;
Tilling = Enum.IsDefined(typeof(TillingMode), tilling) ? tilling : default;
IsGroup = isGroup;
TilesInfo = tilesInfo ?? Enumerable.Empty<JsonTileInfo>();
AtlasGroups = atlasGroups ?? Enumerable.Empty<AtlasGroup>();
if (isGroup)
if (IsGroup)
ResourceGroups.Add(this);
}
public override string ToString()
{
return "res/" + Path;
}
internal static ResourceLocation GetFromCategory(ResourceCategory category)
{
return category switch
if (Category != ResourceCategory.Unknown && !string.IsNullOrWhiteSpace(Path))
{
ResourceCategory.ItemAnimation => _categoryLookUp["textures/items"],
ResourceCategory.BlockAnimation => _categoryLookUp["textures/blocks"],
ResourceCategory.MobEntityTextures => _categoryLookUp["mob"],
ResourceCategory.ItemEntityTextures => _categoryLookUp["item"],
ResourceCategory.BlockAtlas => _categoryLookUp["terrain.png"],
ResourceCategory.ItemAtlas => _categoryLookUp["items.png"],
ResourceCategory.ParticleAtlas => _categoryLookUp["particles.png"],
ResourceCategory.BannerAtlas => _categoryLookUp["item/banner/Banner_Atlas.png"],
ResourceCategory.PaintingAtlas => _categoryLookUp["art/kz.png"],
ResourceCategory.ExplosionAtlas => _categoryLookUp["misc/explosion.png"],
ResourceCategory.ExperienceOrbAtlas => _categoryLookUp["item/xporb.png"],
ResourceCategory.MoonPhaseAtlas => _categoryLookUp["terrain/moon_phases.png"],
ResourceCategory.MapIconAtlas => _categoryLookUp["misc/mapicons.png"],
ResourceCategory.AdditionalMapIconsAtlas => _categoryLookUp["misc/additionalmapicons.png"],
_ => Unknown
};
_categoryLookUp.Add(Category, this);
_pathLookUp.Add(Path, this);
Debug.WriteLine($"Add ResourceLocation: {Path}({Category}).");
}
}
public override string ToString() => FullPath;
internal static ResourceLocation GetFromCategory(ResourceCategory category) => _categoryLookUp.ContainsKey(category) ? _categoryLookUp[category] : Unknown;
internal static string GetPathFromCategory(ResourceCategory category) => GetFromCategory(category).ToString();
internal static ResourceCategory GetCategoryFromPath(string path) => GetFromPath(path).Category;
internal static ResourceLocation GetFromPath(string path)
{
if (string.IsNullOrWhiteSpace(path) || !path.StartsWith(RESOURCE_PATH_PREFIX))
return Unknown;
string categoryPath = path.Substring(RESOURCE_PATH_PREFIX.Length);
if (_pathLookUp.ContainsKey(categoryPath))
return _pathLookUp[categoryPath];
return ResourceGroups.Where(group => categoryPath.StartsWith(group.Path)).FirstOrDefault() ?? Unknown;
}
}
}

View File

@@ -39,74 +39,5 @@ namespace PckStudio.Json
public static List<JsonTileInfo> ExplosionTileInfos => JsonExplosionData.Entries;
public static List<JsonTileInfo> PaintingTileInfos => JsonPaintingData.Entries;
public static List<JsonTileInfo> BannerTileInfos => JsonBannerData.Entries;
private static Image[] _itemImages;
public static Image[] ItemImages => _itemImages ??= Resources.items_atlas.SplitHorizontal(16).ToArray();
private static Image[] _blockImages;
public static Image[] BlockImages => _blockImages ??= Resources.terrain_atlas.SplitHorizontal(16).ToArray();
private static Image[] _particleImages;
public static Image[] ParticleImages => _particleImages ??= Resources.particles_atlas.SplitHorizontal(16).ToArray();
private static Image[] _moonPhaseImages;
public static Image[] MoonPhaseImages => _moonPhaseImages ??= Resources.moon_phases_atlas.SplitHorizontal(4).ToArray();
private static Image[] _mapIconImages;
public static Image[] MapIconImages => _mapIconImages ??= Resources.map_icons_atlas.SplitHorizontal(4).ToArray();
private static Image[] _additionalMapIconImages;
public static Image[] AdditionalMapIconImages => _additionalMapIconImages ??= Resources.additional_map_icons_atlas.SplitHorizontal(4).ToArray();
private static Image[] _experienceOrbIconImages;
public static Image[] ExperienceOrbImages => _experienceOrbIconImages ??= Resources.experience_orbs_atlas.SplitHorizontal(4).ToArray();
private static Image[] _explosionImages;
public static Image[] ExplosionImages => _explosionImages ??= Resources.explosions_atlas.SplitHorizontal(4).ToArray();
private static Image[] _paintingImages;
public static Image[] PaintingImages => _paintingImages ??= Resources.paintings_atlas.SplitHorizontal(16).ToArray();
private static Image[] _bannerImages;
public static Image[] BannerImages => _bannerImages ??= Resources.banners_atlas.SplitHorizontal(6).ToArray();
private static ImageList GetImageList(Image[] images)
{
ImageList imageList = new ImageList();
imageList.ColorDepth = ColorDepth.Depth32Bit;
imageList.Images.AddRange(images);
return imageList;
}
private static ImageList _blockImageList = GetImageList(BlockImages);
public static ImageList BlockImageList { get { return _blockImageList; } }
private static ImageList _itemImageList = GetImageList(ItemImages);
public static ImageList ItemImageList { get { return _itemImageList; } }
private static ImageList _particleImageList = GetImageList(ParticleImages);
public static ImageList ParticleImageList { get { return _particleImageList; } }
private static ImageList _moonPhaseImageList = GetImageList(MoonPhaseImages);
public static ImageList MoonPhaseImageList { get { return _moonPhaseImageList; } }
private static ImageList _mapIconImageList = GetImageList(MapIconImages);
public static ImageList MapIconImageList { get { return _mapIconImageList; } }
private static ImageList _additionalMapIconImageList = GetImageList(AdditionalMapIconImages);
public static ImageList AdditionalMapIconImageList { get { return _additionalMapIconImageList; } }
private static ImageList _experienceOrbsImageList = GetImageList(ExperienceOrbImages);
public static ImageList ExperienceOrbImageList { get { return _experienceOrbsImageList; } }
private static ImageList _explosionImageList = GetImageList(ExplosionImages);
public static ImageList ExplosionImageList { get { return _explosionImageList; } }
private static ImageList _paintingImageList = GetImageList(PaintingImages);
public static ImageList PaintingImageList { get { return _paintingImageList; } }
private static ImageList _bannerImageList = GetImageList(BannerImages);
public static ImageList BannerImageList { get { return _bannerImageList; } }
}
}