Add component editors and UI controls
Added the `HierarchyEditor` and `LocalToWorldEditor` classes to implement custom component editing functionality. Added the `Vector3Field` control for 3D vector manipulation and its corresponding XAML definition. Added the `ComponentDataView` and `ComponentObject` classes to manage component data display and access. Added the `CustomEditorAttribute` to mark classes as custom editors for specific components. Changed the `IInspectable` interface to use properties for `Icon`, `HeaderContent`, and `InspectorContent`. Changed the `PropertyField` class to enhance UI control binding capabilities. Changed the `EditorWorldManager` to improve world data loading and deserialization processes. Changed the `EntityNode` and `WorldNode` classes to update entity construction and component querying. Changed the `StaticResource` class to include new binding flags for component properties. Changed the `InspectorService` to remove old contract references and adopt new interfaces. Changed the `QueryEnumerable` and related files to update generic constraints for improved type safety. Changed the `QueryItem` class to reflect new generic constraints and enhance deconstruction. Changed the `World.Query` methods to utilize the updated generic constraints. Updated the `SerializationTest` to align with new entity creation and management practices.
This commit is contained in:
@@ -1,10 +1,30 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Controls.Primitives;
|
||||
using Microsoft.UI.Xaml.Data;
|
||||
using System.Reflection;
|
||||
using Windows.Globalization.NumberFormatting;
|
||||
|
||||
namespace Ghost.Editor.Controls;
|
||||
|
||||
public sealed partial class PropertyField : ContentControl
|
||||
{
|
||||
private static readonly Dictionary<Type, DependencyProperty> _valueProperties = new()
|
||||
{
|
||||
{ typeof(TextBox), TextBox.TextProperty },
|
||||
{ typeof(NumberBox), NumberBox.ValueProperty },
|
||||
{ typeof(ToggleButton), ToggleButton.IsCheckedProperty },
|
||||
{ typeof(ToggleSwitch), ToggleSwitch.IsOnProperty },
|
||||
{ typeof(ComboBox), Selector.SelectedValueProperty },
|
||||
{ typeof(RangeBase), RangeBase.ValueProperty },
|
||||
};
|
||||
|
||||
private object? sourceObject;
|
||||
private FieldInfo? propertyInfo;
|
||||
private Type? _fieldType;
|
||||
|
||||
private object? _lastValue;
|
||||
|
||||
public string Label
|
||||
{
|
||||
get => (string)GetValue(LabelProperty);
|
||||
@@ -21,4 +41,106 @@ public sealed partial class PropertyField : ContentControl
|
||||
{
|
||||
DefaultStyleKey = typeof(PropertyField);
|
||||
}
|
||||
|
||||
private static DependencyProperty? GetValueProperty(Type? fieldType)
|
||||
{
|
||||
while (fieldType != null)
|
||||
{
|
||||
if (_valueProperties.TryGetValue(fieldType, out var dp))
|
||||
{
|
||||
return dp;
|
||||
}
|
||||
fieldType = fieldType.BaseType;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static TField ConfigureField<TField>(PropertyField propertyField, FieldInfo fieldInfo, object sourceObject, Func<TField> factory)
|
||||
where TField : FrameworkElement
|
||||
{
|
||||
propertyField.sourceObject = sourceObject;
|
||||
propertyField.propertyInfo = fieldInfo;
|
||||
propertyField._fieldType = typeof(TField);
|
||||
|
||||
var field = factory();
|
||||
|
||||
var dp = GetValueProperty(typeof(TField));
|
||||
field.SetBinding(dp, new Binding
|
||||
{
|
||||
Source = sourceObject,
|
||||
Path = new PropertyPath(fieldInfo.Name),
|
||||
Mode = BindingMode.TwoWay,
|
||||
});
|
||||
return field;
|
||||
}
|
||||
|
||||
public static PropertyField Create(string label, FieldInfo fieldInfo, object sourceObject)
|
||||
{
|
||||
var propertyField = new PropertyField
|
||||
{
|
||||
Label = label
|
||||
};
|
||||
|
||||
FrameworkElement content;
|
||||
switch (fieldInfo.FieldType)
|
||||
{
|
||||
case Type t when t == typeof(string):
|
||||
content = ConfigureField(propertyField, fieldInfo, sourceObject, () => new TextBox());
|
||||
break;
|
||||
case Type t when t == typeof(int) || t == typeof(float) || t == typeof(double):
|
||||
content = ConfigureField(propertyField, fieldInfo, sourceObject, () => new NumberBox
|
||||
{
|
||||
SpinButtonPlacementMode = NumberBoxSpinButtonPlacementMode.Hidden,
|
||||
AcceptsExpression = true,
|
||||
NumberFormatter = new DecimalFormatter
|
||||
{
|
||||
FractionDigits = t == typeof(int) ? 0 : 9,
|
||||
}
|
||||
});
|
||||
break;
|
||||
case Type t when t == typeof(bool):
|
||||
content = ConfigureField(propertyField, fieldInfo, sourceObject, () => new ToggleSwitch());
|
||||
break;
|
||||
case Type t when t == typeof(Enum):
|
||||
content = ConfigureField(propertyField, fieldInfo, sourceObject, () => new ComboBox
|
||||
{
|
||||
ItemsSource = Enum.GetValues(t),
|
||||
SelectedValuePath = "Value",
|
||||
});
|
||||
break;
|
||||
default:
|
||||
content = new TextBlock
|
||||
{
|
||||
Text = $"Unsupported type: {fieldInfo.FieldType.Name}",
|
||||
VerticalAlignment = VerticalAlignment.Center,
|
||||
Foreground = new Microsoft.UI.Xaml.Media.SolidColorBrush(Microsoft.UI.Colors.Red)
|
||||
};
|
||||
break;
|
||||
}
|
||||
|
||||
propertyField.Content = content;
|
||||
return propertyField;
|
||||
}
|
||||
|
||||
public void UpdateValue()
|
||||
{
|
||||
if (sourceObject == null || propertyInfo == null || _fieldType == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var currentValue = propertyInfo.GetValue(sourceObject);
|
||||
if (Equals(currentValue, _lastValue))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var dp = GetValueProperty(_fieldType);
|
||||
if (dp != null)
|
||||
{
|
||||
SetValue(dp, propertyInfo.GetValue(sourceObject));
|
||||
_lastValue = currentValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user