diff --git a/PCK-Studio/Controls/PckAssetBrowserEditor.cs b/PCK-Studio/Controls/PckAssetBrowserEditor.cs index 7a4a5f5d..671edc65 100644 --- a/PCK-Studio/Controls/PckAssetBrowserEditor.cs +++ b/PCK-Studio/Controls/PckAssetBrowserEditor.cs @@ -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 /// True if the remove should be canceled, otherwise False 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 && diff --git a/PCK-Studio/Forms/Additional-Popups/Animation/ChangeTile.cs b/PCK-Studio/Forms/Additional-Popups/Animation/ChangeTile.cs index de2fa054..d73d306b 100644 --- a/PCK-Studio/Forms/Additional-Popups/Animation/ChangeTile.cs +++ b/PCK-Studio/Forms/Additional-Popups/Animation/ChangeTile.cs @@ -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 textureInfos, ImageList imgList, string name) = key switch + (List 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)) diff --git a/PCK-Studio/Forms/Editor/TextureAtlasEditor.cs b/PCK-Studio/Forms/Editor/TextureAtlasEditor.cs index 55158e49..e71407ed 100644 --- a/PCK-Studio/Forms/Editor/TextureAtlasEditor.cs +++ b/PCK-Studio/Forms/Editor/TextureAtlasEditor.cs @@ -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(); 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 animationSaveContext); diff --git a/PCK-Studio/Internal/ApplicationScope.cs b/PCK-Studio/Internal/ApplicationScope.cs index 8f3c9e9c..380ed5b4 100644 --- a/PCK-Studio/Internal/ApplicationScope.cs +++ b/PCK-Studio/Internal/ApplicationScope.cs @@ -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); } diff --git a/PckStudio.Core/Atlas.cs b/PckStudio.Core/Atlas.cs index d30f19b0..e9b3f559 100644 --- a/PckStudio.Core/Atlas.cs +++ b/PckStudio.Core/Atlas.cs @@ -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 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; } diff --git a/PckStudio.Core/AtlasResource.cs b/PckStudio.Core/AtlasResource.cs new file mode 100644 index 00000000..6e9451c7 --- /dev/null +++ b/PckStudio.Core/AtlasResource.cs @@ -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 TilesInfo { get; } + public AtlasGroup[] AtlasGroups { get; } + public readonly Image DefaultTexture; + + public AtlasResource(string path, ResourceCategory resourceCategory, int tillingFactor, Image defaultTexture, TillingMode tilling = default, IEnumerable 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 tilesInfo = default, AtlasGroup[] atlasGroups = default) + : base(path, resourceCategory, tillingFactor, tilling, isGroup: true) + { + TilesInfo = tilesInfo ?? Enumerable.Empty(); + AtlasGroups = atlasGroups ?? Array.Empty(); + DefaultTexture = defaultTexture; + } + + public Atlas GetDefaultAtlas() + { + return Atlas.FromResourceLocation(DefaultTexture, this); + } + } +} diff --git a/PckStudio.Core/Deserializer/AtlasDeserializer.cs b/PckStudio.Core/Deserializer/AtlasDeserializer.cs index 52394d69..c8c3c940 100644 --- a/PckStudio.Core/Deserializer/AtlasDeserializer.cs +++ b/PckStudio.Core/Deserializer/AtlasDeserializer.cs @@ -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 { - 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); diff --git a/PckStudio.Core/Extensions/EnumerableExtensions.cs b/PckStudio.Core/Extensions/EnumerableExtensions.cs index 266c5317..4f5f2f5f 100644 --- a/PckStudio.Core/Extensions/EnumerableExtensions.cs +++ b/PckStudio.Core/Extensions/EnumerableExtensions.cs @@ -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(this T type, params T[] items) { foreach (T item in items) diff --git a/PckStudio.Core/GroupResource.cs b/PckStudio.Core/GroupResource.cs new file mode 100644 index 00000000..f7b3b2f8 --- /dev/null +++ b/PckStudio.Core/GroupResource.cs @@ -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) + { + } + } +} diff --git a/PckStudio.Core/PckStudio.Core.csproj b/PckStudio.Core/PckStudio.Core.csproj index 92655915..c05415ef 100644 --- a/PckStudio.Core/PckStudio.Core.csproj +++ b/PckStudio.Core/PckStudio.Core.csproj @@ -58,6 +58,7 @@ + @@ -77,6 +78,7 @@ + @@ -113,6 +115,7 @@ + diff --git a/PckStudio.Core/ResourceCategory.cs b/PckStudio.Core/ResourceCategory.cs index 1512c8b7..64c33179 100644 --- a/PckStudio.Core/ResourceCategory.cs +++ b/PckStudio.Core/ResourceCategory.cs @@ -25,6 +25,7 @@ namespace PckStudio.Core BlockAnimation, MobEntityTextures, ItemEntityTextures, + ArmorTextures, ItemAtlas, BlockAtlas, ParticleAtlas, diff --git a/PckStudio.Core/ResourceLocation.cs b/PckStudio.Core/ResourceLocation.cs index 95dbd5d4..dffb6a82 100644 --- a/PckStudio.Core/ResourceLocation.cs +++ b/PckStudio.Core/ResourceLocation.cs @@ -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 ResourceGroups = new List(); - 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 _categoryLookUp = new Dictionary() - { - ["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 ResourceGroups = new List(); + private static readonly ResourceLocation Unknown = new ResourceLocation(string.Empty, ResourceCategory.Unknown, -1); + private static readonly Dictionary _pathLookUp = new Dictionary(); + private static readonly Dictionary _categoryLookUp = new Dictionary(); 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 TilesInfo { get; } - public IEnumerable 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 tilesInfo = default, IEnumerable 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 tilesInfo = default, IEnumerable 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(); - AtlasGroups = atlasGroups ?? Enumerable.Empty(); - 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; } } } diff --git a/PckStudio.Core/Tiles.cs b/PckStudio.Core/Tiles.cs index e1a1a807..c9e22301 100644 --- a/PckStudio.Core/Tiles.cs +++ b/PckStudio.Core/Tiles.cs @@ -39,74 +39,5 @@ namespace PckStudio.Json public static List ExplosionTileInfos => JsonExplosionData.Entries; public static List PaintingTileInfos => JsonPaintingData.Entries; public static List 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; } } } }