Add 'ITryGetSet.cs' and useful wrappers for it

This commit is contained in:
miku-666
2024-11-09 12:18:59 +01:00
parent e2b33ca8b5
commit 21e2e99baf
5 changed files with 95 additions and 30 deletions

View File

@@ -11,6 +11,7 @@ using OMI.Formats.Model;
using MetroFramework.Forms;
using PckStudio.Internal;
using PckStudio.Interfaces;
using PckStudio.Internal.Json;
using PckStudio.Internal.App;
using PckStudio.Extensions;
@@ -20,19 +21,13 @@ namespace PckStudio.Forms.Editor
public partial class ModelEditor : MetroForm
{
private readonly ModelContainer _models;
private readonly TryGetTextureDelegate _tryGetTexture;
private readonly TrySetTextureDelegate _trySetTexture;
private readonly ITryGetSet<string, Image> _textures;
public delegate bool TryGetTextureDelegate(string path, out Image img);
public delegate bool TrySetTextureDelegate(string path, Image img);
public ModelEditor(ModelContainer models, TryGetTextureDelegate tryGetTexture, TrySetTextureDelegate trySetTexture)
public ModelEditor(ModelContainer models, TryGetDelegate<string, Image> tryGetTexture, TrySetDelegate<string, Image> trySetTexture)
{
InitializeComponent();
_models = models;
_tryGetTexture = tryGetTexture;
_trySetTexture = trySetTexture;
_textures = TryGetSet<string, Image>.FromDelegates(tryGetTexture, trySetTexture);
modelTreeView.ImageList = new ImageList
{
@@ -117,6 +112,7 @@ namespace PckStudio.Forms.Editor
["mooshroom"] = 48,
["witherBoss.armor"] = 90,
// 1.14 models
["panda"] = 52,
["ravager"] = 61,
["pillager"] = 56,
@@ -151,7 +147,7 @@ namespace PckStudio.Forms.Editor
public ModelPart Part => _part;
private ModelPartNode(ModelPart part)
: base(part.Name)
: base($"{part.Name} Pivot:{part.Translation * -1} Rot:{part.Rotation + part.AdditionalRotation} ")
{
_part = part;
ImageIndex = 126;
@@ -262,7 +258,7 @@ namespace PckStudio.Forms.Editor
yield break;
foreach (var textureLocation in GameModelImporter.ModelMetaData[modelName].TextureLocations)
{
if (_tryGetTexture(textureLocation, out Image img))
if (_textures.TryGet(textureLocation, out Image img))
yield return new NamedTexture(Path.GetFileName(textureLocation), img);
}
yield break;
@@ -298,7 +294,7 @@ namespace PckStudio.Forms.Editor
foreach (NamedTexture texture in modelInfo.Textures)
{
_trySetTexture(texture.Name, texture.Texture);
_textures.TrySet(texture.Name, texture.Texture);
}
}
}

View File

@@ -0,0 +1,65 @@
namespace PckStudio.Interfaces
{
public delegate bool TryGetDelegate<in TKey, TValue>(TKey key, out TValue value);
public delegate bool TrySetDelegate<in TKey, TValue>(TKey key, TValue value);
internal sealed class TryGet<TKey, TValue> : ITryGet<TKey, TValue>
{
private TryGetDelegate<TKey, TValue> _tryGetDelegate;
public static ITryGet<TKey, TValue> FromDelegate(TryGetDelegate<TKey, TValue> tryGetDelegate) => new TryGet<TKey, TValue>(tryGetDelegate);
bool ITryGet<TKey, TValue>.TryGet(TKey key, out TValue value) => _tryGetDelegate(key, out value);
private TryGet(TryGetDelegate<TKey, TValue> tryGetDelegate)
{
_tryGetDelegate = tryGetDelegate;
}
}
internal sealed class TrySet<TKey, TValue> : ITrySet<TKey, TValue>
{
private TrySetDelegate<TKey, TValue> _trySetDelegate;
public static ITrySet<TKey, TValue> FromDelegate(TrySetDelegate<TKey, TValue> trySetDelegate) => new TrySet<TKey, TValue>(trySetDelegate);
bool ITrySet<TKey, TValue>.TrySet(TKey key, TValue value) => _trySetDelegate(key, value);
private TrySet(TrySetDelegate<TKey, TValue> trySetDelegate)
{
_trySetDelegate = trySetDelegate;
}
}
internal sealed class TryGetSet<TKey, TValue> : ITryGetSet<TKey, TValue>
{
public static ITryGetSet<TKey, TValue> FromDelegates(TryGetDelegate<TKey, TValue> tryGetDelegate, TrySetDelegate<TKey, TValue> trySetDelegate) => new TryGetSet<TKey, TValue>(tryGetDelegate, trySetDelegate);
public bool TryGet(TKey key, out TValue value) => _tryGetDelegate(key, out value);
public bool TrySet(TKey key, TValue value) => _trySetDelegate(key, value);
private TryGetDelegate<TKey, TValue> _tryGetDelegate;
private TrySetDelegate<TKey, TValue> _trySetDelegate;
private TryGetSet(TryGetDelegate<TKey, TValue> tryGetDelegate, TrySetDelegate<TKey, TValue> trySetDelegate)
{
_tryGetDelegate = tryGetDelegate;
_trySetDelegate = trySetDelegate;
}
}
interface ITryGet<in TKey, TValue>
{
bool TryGet(TKey key, out TValue value);
}
interface ITrySet<in TKey, TValue>
{
bool TrySet(TKey key, TValue value);
}
interface ITryGetSet<in TKey, TValue> : ITryGet<TKey, TValue>, ITrySet<TKey, TValue>
{
}
}

View File

@@ -40,6 +40,7 @@ using PckStudio.Internal.Deserializer;
using PckStudio.Internal.Serializer;
using PckStudio.Internal.App;
using PckStudio.Internal.Skin;
using PckStudio.Interfaces;
namespace PckStudio
{
@@ -603,18 +604,18 @@ namespace PckStudio
return;
}
ModelEditor.TryGetTextureDelegate tryGetTexture = (string path, out Image img) =>
TryGetDelegate<string, Image> tryGetTexture = (string path, out Image img) =>
{
bool found = currentPCK.TryGetAsset(path + ".png", PckAssetType.TextureFile, out PckAsset asset) ||
currentPCK.TryGetAsset(path + ".tga", PckAssetType.TextureFile, out asset);
img = found ? asset.GetTexture() : default;
return found;
};
ModelEditor.TrySetTextureDelegate trySetTexture = (string path, Image img) =>
bool found = currentPCK.TryGetAsset(path + ".png", PckAssetType.TextureFile, out PckAsset asset) ||
currentPCK.TryGetAsset(path + ".tga", PckAssetType.TextureFile, out asset);
img = found ? asset.GetTexture() : default;
return found;
};
TrySetDelegate<string, Image> trySetTexture = (string path, Image img) =>
{
bool found = currentPCK.TryGetAsset(path + ".png", PckAssetType.TextureFile, out PckAsset foundAsset) ||
currentPCK.TryGetAsset(path + ".tga", PckAssetType.TextureFile, out foundAsset);
currentPCK.TryGetAsset(path + ".tga", PckAssetType.TextureFile, out foundAsset);
PckAsset asset = foundAsset ?? currentPCK.CreateNewAsset(path + ".png", PckAssetType.TextureFile);
asset.SetTexture(img);
return true;
@@ -622,10 +623,10 @@ namespace PckStudio
var editor = new ModelEditor(modelContainer, tryGetTexture, trySetTexture);
if (editor.ShowDialog() == DialogResult.OK)
{
{
return;
}
}
}
}
public void HandleBehavioursFile(PckAsset asset)
{

View File

@@ -153,6 +153,7 @@
<DependentUpon>ContributorsForm.cs</DependentUpon>
</Compile>
<Compile Include="Extensions\OpenTKExtensions.cs" />
<Compile Include="Interfaces\ITryGetSet.cs" />
<Compile Include="Forms\Editor\ModelEditor.cs">
<SubType>Form</SubType>
</Compile>

View File

@@ -11,6 +11,7 @@ using OMI.Formats.Model;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using PckStudio.Extensions;
using PckStudio.Interfaces;
using PckStudio.Internal;
using PckStudio.Internal.Json;
using PckStudio.Properties;
@@ -139,7 +140,7 @@ namespace PckStudio.Rendering
};
}
_rootCollection.AddRange(RetriveChildMeshes(modelMetaData.RootParts, Vector3.Zero, Vector3.Zero, Vector3.Zero, ref model));
_rootCollection.AddRange(BuildModelMesh(modelMetaData.RootParts, Vector3.Zero, Vector3.Zero, Vector3.Zero, TryGet<string, ModelPart>.FromDelegate(model.TryGetPart)));
if (Context.IsCurrent)
{
@@ -159,17 +160,18 @@ namespace PckStudio.Rendering
Camera.Pitch = 25f;
}
private List<GenericMesh<TextureVertex>> RetriveChildMeshes(ModelMetaDataPart[] metaDataParts, Vector3 parentTranslation, Vector3 parentRotation, Vector3 parentPivot, ref Model model)
private List<GenericMesh<TextureVertex>> BuildModelMesh(ModelMetaDataPart[] metaDataParts, Vector3 parentTranslation, Vector3 parentRotation, Vector3 parentPivot, ITryGet<string, ModelPart> model)
{
List<GenericMesh<TextureVertex>> meshes = new List<GenericMesh<TextureVertex>>();
foreach (ModelMetaDataPart metaDataPart in metaDataParts)
{
if (!model.TryGetPart(metaDataPart.Name, out ModelPart modelPart))
if (!model.TryGet(metaDataPart.Name, out ModelPart modelPart))
{
Trace.TraceError($"[{nameof(ModelRenderer)}@{nameof(RetriveChildMeshes)}] : Failed to find part: '{metaDataPart.Name}'");
Trace.TraceError($"[{nameof(ModelRenderer)}@{nameof(BuildModelMesh)}] : Failed to find part: '{metaDataPart.Name}'");
continue;
}
Vector3 translation = modelPart.Translation.ToOpenTKVector();
Vector3 pivot = parentPivot == Vector3.Zero ? translation * -1 : parentPivot;
Vector3 pivot = translation * -1 - parentPivot;
Vector3 rotation = (modelPart.Rotation.ToOpenTKVector() + modelPart.AdditionalRotation.ToOpenTKVector());
CubeMeshCollection cubeCollection = new CubeMeshCollection(modelPart.Name, translation - parentTranslation, pivot, rotation - parentRotation);
cubeCollection.FlipZMapping = true;
@@ -178,7 +180,7 @@ namespace PckStudio.Rendering
cubeCollection.AddNamed(modelPart.Name, boxes.Position.ToOpenTKVector(), boxes.Size.ToOpenTKVector(), boxes.Uv.ToOpenTKVector(), boxes.Inflate, boxes.Mirror);
}
meshes.Add(cubeCollection);
RetriveChildMeshes(metaDataPart.Children, translation, rotation, pivot, ref model).ForEach(cubeCollection.Add);
BuildModelMesh(metaDataPart.Children, translation, rotation, pivot, model).ForEach(cubeCollection.Add);
}
return meshes;
}