Se añadió la funcionalidad para cargar datos de imágenes desde archivos JSON en la clase DatosDeTrabajo, mejorando la gestión de imágenes. Se implementó un nuevo método para obtener configuraciones de serialización JSON y se mejoró la lógica de carga de datos, incluyendo compatibilidad con versiones anteriores. Además, se actualizó el método de obtención de nombres de imágenes en MainViewModel para incluir etiquetas, y se ajustó el convertidor correspondiente en ImageDisplayNameConverter.
This commit is contained in:
parent
354b4a8acf
commit
ca70f66ff1
|
@ -14,7 +14,7 @@ namespace CtrEditor.Converters
|
||||||
// Buscar el MainViewModel en el Application.Current.MainWindow.DataContext
|
// Buscar el MainViewModel en el Application.Current.MainWindow.DataContext
|
||||||
if (Application.Current?.MainWindow?.DataContext is MainViewModel viewModel)
|
if (Application.Current?.MainWindow?.DataContext is MainViewModel viewModel)
|
||||||
{
|
{
|
||||||
return viewModel.GetImageDisplayName(imageName);
|
return viewModel.GetImageDisplayNameWithTags(imageName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
|
|
|
@ -5,6 +5,7 @@ using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace CtrEditor
|
namespace CtrEditor
|
||||||
{
|
{
|
||||||
|
@ -68,6 +69,8 @@ namespace CtrEditor
|
||||||
|
|
||||||
private void CargarDatosImagenesExistentes()
|
private void CargarDatosImagenesExistentes()
|
||||||
{
|
{
|
||||||
|
var jsonSerializerSettings = GetJsonSerializerSettings();
|
||||||
|
|
||||||
foreach (var imageName in Imagenes.Keys)
|
foreach (var imageName in Imagenes.Keys)
|
||||||
{
|
{
|
||||||
string jsonPath = ObtenerPathImagenConExtension(imageName, ".json");
|
string jsonPath = ObtenerPathImagenConExtension(imageName, ".json");
|
||||||
|
@ -75,17 +78,65 @@ namespace CtrEditor
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Solo cargamos los datos de imagen si existe el archivo JSON
|
// Cargar datos completos del archivo JSON
|
||||||
// El StateSerializer se encargará de cargar los datos completos
|
string jsonString = File.ReadAllText(jsonPath);
|
||||||
_mainViewModel.GetOrCreateImageData(imageName);
|
var simulationData = Newtonsoft.Json.JsonConvert.DeserializeObject<SimulationData>(jsonString, jsonSerializerSettings);
|
||||||
|
|
||||||
|
// Cargar datos de imágenes si existen en el archivo
|
||||||
|
if (simulationData?.ImageDataDictionary != null)
|
||||||
|
{
|
||||||
|
foreach (var imageDataEntry in simulationData.ImageDataDictionary)
|
||||||
|
{
|
||||||
|
// Solo cargar si no existe ya en el diccionario principal
|
||||||
|
if (!_mainViewModel._imageDataDictionary.ContainsKey(imageDataEntry.Key))
|
||||||
|
{
|
||||||
|
_mainViewModel._imageDataDictionary[imageDataEntry.Key] = imageDataEntry.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compatibilidad con versiones anteriores (ImageCustomNames)
|
||||||
|
#pragma warning disable CS0618 // Type or member is obsolete
|
||||||
|
if (simulationData?.ImageCustomNames != null)
|
||||||
|
{
|
||||||
|
foreach (var customName in simulationData.ImageCustomNames)
|
||||||
|
{
|
||||||
|
var imageData = _mainViewModel.GetOrCreateImageData(customName.Key);
|
||||||
|
if (string.IsNullOrEmpty(imageData.CustomName)) // Solo actualizar si no tiene nombre personalizado
|
||||||
|
{
|
||||||
|
imageData.CustomName = customName.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#pragma warning restore CS0618
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
// Log del error pero no fallar la carga completa
|
// Log del error pero no fallar la carga completa
|
||||||
System.Diagnostics.Debug.WriteLine($"Error al pre-cargar datos de imagen {imageName}: {ex.Message}");
|
System.Diagnostics.Debug.WriteLine($"Error al cargar datos de imagen desde {jsonPath}: {ex.Message}");
|
||||||
|
|
||||||
|
// Como fallback, crear una instancia vacía
|
||||||
|
_mainViewModel.GetOrCreateImageData(imageName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Si no existe archivo JSON, crear instancia vacía
|
||||||
|
_mainViewModel.GetOrCreateImageData(imageName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Newtonsoft.Json.JsonSerializerSettings GetJsonSerializerSettings()
|
||||||
|
{
|
||||||
|
return new Newtonsoft.Json.JsonSerializerSettings
|
||||||
|
{
|
||||||
|
Formatting = Newtonsoft.Json.Formatting.Indented,
|
||||||
|
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
|
||||||
|
TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto,
|
||||||
|
ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace,
|
||||||
|
ConstructorHandling = Newtonsoft.Json.ConstructorHandling.AllowNonPublicDefaultConstructor
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -448,6 +448,22 @@ namespace CtrEditor
|
||||||
return imageName;
|
return imageName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string GetImageDisplayNameWithTags(string imageName)
|
||||||
|
{
|
||||||
|
if (_imageDataDictionary.TryGetValue(imageName, out var imageData))
|
||||||
|
{
|
||||||
|
var displayName = imageData.DisplayName;
|
||||||
|
var tags = imageData.Etiquetas;
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(tags))
|
||||||
|
{
|
||||||
|
return $"{displayName} {tags}";
|
||||||
|
}
|
||||||
|
return displayName;
|
||||||
|
}
|
||||||
|
return imageName;
|
||||||
|
}
|
||||||
|
|
||||||
public Models.ImageData GetOrCreateImageData(string imageName)
|
public Models.ImageData GetOrCreateImageData(string imageName)
|
||||||
{
|
{
|
||||||
if (!_imageDataDictionary.TryGetValue(imageName, out var imageData))
|
if (!_imageDataDictionary.TryGetValue(imageName, out var imageData))
|
||||||
|
|
Loading…
Reference in New Issue