2024-05-11 11:58:55 -03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
using System.Windows;
|
2024-05-14 12:10:32 -03:00
|
|
|
|
using System.Windows.Media;
|
2024-05-11 11:58:55 -03:00
|
|
|
|
|
|
|
|
|
namespace CtrEditor.Convertidores
|
|
|
|
|
{
|
2024-05-14 13:17:46 -03:00
|
|
|
|
|
|
|
|
|
public class WidthPercentageConverter : IValueConverter
|
|
|
|
|
{
|
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
if (value is double width)
|
|
|
|
|
{
|
|
|
|
|
return width * 0.25;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class LevelToHeightMultiConverter : IMultiValueConverter
|
|
|
|
|
{
|
|
|
|
|
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
if (values[0] is float level && values[1] is double containerHeight)
|
|
|
|
|
{
|
|
|
|
|
return containerHeight * (level / 100);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-05-14 12:10:32 -03:00
|
|
|
|
public class BrushToColorNameConverter : IValueConverter
|
|
|
|
|
{
|
|
|
|
|
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
if (value is SolidColorBrush brush)
|
|
|
|
|
{
|
|
|
|
|
if (brush == Brushes.Red) return "Rojo";
|
|
|
|
|
if (brush == Brushes.Blue) return "Azul";
|
|
|
|
|
if (brush == Brushes.Black) return "Negro";
|
|
|
|
|
if (brush == Brushes.Green) return "Verde";
|
|
|
|
|
if (brush == Brushes.Gray) return "Gris";
|
|
|
|
|
}
|
|
|
|
|
return "Unknown";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
if (value is string colorName)
|
|
|
|
|
{
|
|
|
|
|
switch (colorName)
|
|
|
|
|
{
|
|
|
|
|
case "Rojo":
|
|
|
|
|
return Brushes.Red;
|
|
|
|
|
case "Azul":
|
|
|
|
|
return Brushes.Blue;
|
|
|
|
|
case "Negro":
|
|
|
|
|
return Brushes.Black;
|
|
|
|
|
case "Verde":
|
|
|
|
|
return Brushes.Green;
|
|
|
|
|
case "Gris":
|
|
|
|
|
return Brushes.Gray;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Brushes.Transparent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-11 11:58:55 -03:00
|
|
|
|
public class PixelToMeter
|
|
|
|
|
{
|
|
|
|
|
// Instancia privada estática, parte del patrón Singleton
|
|
|
|
|
private static PixelToMeter? _instance;
|
|
|
|
|
public UnitConverter calc = new UnitConverter(0.01f);
|
|
|
|
|
|
|
|
|
|
// Propiedad pública estática para acceder a la instancia
|
|
|
|
|
public static PixelToMeter Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
|
|
|
|
_instance = new PixelToMeter();
|
|
|
|
|
}
|
|
|
|
|
return _instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MeterToPixelConverter : IValueConverter
|
|
|
|
|
{
|
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
float meters = (float)value;
|
|
|
|
|
float factor = 1;
|
|
|
|
|
if (parameter != null)
|
|
|
|
|
if (parameter.ToString() == "0.5") factor = 0.5f;
|
|
|
|
|
else if (parameter.ToString() == "-0.5") factor = -0.5f;
|
2024-05-14 09:15:10 -03:00
|
|
|
|
else if (parameter.ToString() == "1.5") factor = 1.5f;
|
2024-05-11 11:58:55 -03:00
|
|
|
|
|
|
|
|
|
return PixelToMeter.Instance.calc.MetersToPixels(meters) * factor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
float pixels = (float)value;
|
|
|
|
|
float factor = 1;
|
|
|
|
|
if (parameter != null)
|
|
|
|
|
if (parameter.ToString() == "0.5") factor = 0.5f;
|
|
|
|
|
else if (parameter.ToString() == "-0.5") factor = -0.5f;
|
2024-05-14 09:15:10 -03:00
|
|
|
|
else if (parameter.ToString() == "1.5") factor = 1.5f;
|
2024-05-11 11:58:55 -03:00
|
|
|
|
|
|
|
|
|
return PixelToMeter.Instance.calc.PixelsToMeters(pixels) * factor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DistanceToMarginConverter : IValueConverter
|
|
|
|
|
{
|
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
if (value is double distance)
|
|
|
|
|
{
|
|
|
|
|
return new Thickness(0, 0, 0, PixelToMeter.Instance.calc.MetersToPixels((float)distance)); // Ajustar Bottom a 'distance'
|
|
|
|
|
}
|
|
|
|
|
return new Thickness();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException("ConvertBack is not supported.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class FloatToFormattedStringConverter : IValueConverter
|
|
|
|
|
{
|
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
if (value is float floatValue)
|
|
|
|
|
{
|
|
|
|
|
return floatValue.ToString("0.00", culture); // Formatear a dos decimales
|
|
|
|
|
}
|
|
|
|
|
return value; // Devolver el valor original si no es un float
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
if (value is string stringValue && float.TryParse(stringValue, NumberStyles.Float, culture, out float result))
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
return value; // Devolver el valor original si no se puede convertir
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class UnitConverter
|
|
|
|
|
{
|
|
|
|
|
// La escala representa cuántos metros hay en un píxel
|
|
|
|
|
public float Scale { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public UnitConverter(float scale)
|
|
|
|
|
{
|
|
|
|
|
if (scale <= 0)
|
|
|
|
|
throw new ArgumentException("Scale must be greater than zero.");
|
|
|
|
|
|
|
|
|
|
Scale = scale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convierte una distancia en metros a píxeles
|
|
|
|
|
public float MetersToPixels(float meters)
|
|
|
|
|
{
|
|
|
|
|
return meters / Scale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convierte una distancia en píxeles a metros
|
|
|
|
|
public float PixelsToMeters(float pixels)
|
|
|
|
|
{
|
|
|
|
|
return pixels * Scale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Configurar o ajustar la escala
|
|
|
|
|
public void SetScale(float newScale)
|
|
|
|
|
{
|
|
|
|
|
if (newScale <= 0)
|
|
|
|
|
throw new ArgumentException("Scale must be greater than zero.");
|
|
|
|
|
|
|
|
|
|
Scale = newScale;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|