Merge remote-tracking branch 'origin/master'

This commit is contained in:
SpoinkyNL committed 2020-07-08 22:36:28 +02:00
commit 70aaa645fb
21 files changed
+282 -131

No files matched your search

@@ -49,6 +49,7 @@
LostFocus="InputLostFocus"
KeyDown="InputKeyDown"
Visibility="Collapsed"
RequestBringIntoView="Input_OnRequestBringIntoView" />
RequestBringIntoView="Input_OnRequestBringIntoView"
PreviewTextInput="Input_PreviewTextInput"/>
</Grid>
</UserControl>
@@ -1,6 +1,8 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
@@ -81,7 +83,7 @@ namespace Artemis.UI.Shared.Controls
private void InputMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
_startValue = Value;
((IInputElement) sender).CaptureMouse();
_mouseDragStartPoint = e.GetPosition((IInputElement) sender);
@@ -109,7 +111,7 @@ namespace Artemis.UI.Shared.Controls
return;
e.Handled = true;
if (!_calledDragStarted)
{
OnDragStarted();
@@ -162,6 +164,13 @@ namespace Artemis.UI.Shared.Controls
e.Handled = true;
}
private void Input_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
var seperator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
var regex = new Regex("^[" + seperator + "][0-9]+$|^[0-9]*[" + seperator + "]{0,1}[0-9]*$");
e.Handled = !regex.IsMatch(e.Text);
}
private static decimal UltimateRoundingFunction(decimal amountToRound, decimal nearstOf, decimal fairness)
{
return Math.Floor(amountToRound / nearstOf + fairness) * nearstOf;
@@ -46,24 +46,6 @@ namespace Artemis.UI.Shared.DataModelVisualization
OnCancel();
UpdateCallback(InputValue, false);
}
/// <summary>
/// May be called to convert an object to one of the types defined in CompatibleConversionTypes
/// </summary>
/// <param name="source">The object to convert, will always be of a type contained in CompatibleConversionTypes</param>
/// <returns>The converted value</returns>
protected virtual T ConvertToSupportedType(object source)
{
return default;
}
internal override object InternalConvertToSupportedType(object source)
{
if (CompatibleConversionTypes != null && CompatibleConversionTypes.Contains(source.GetType()))
return ConvertToSupportedType(source);
throw new ArtemisSharedUIException($"Cannot convert source of type {source.GetType().Name} because the data model input view model does not support it.");
}
}
/// <summary>
@@ -80,7 +62,7 @@ namespace Artemis.UI.Shared.DataModelVisualization
internal Action<object, bool> UpdateCallback { get; set; }
/// <summary>
/// Gets the types this input view model can support through type conversion
/// Gets the types this input view model can support through type conversion. This list is defined when registering the view model.
/// </summary>
internal IReadOnlyCollection<Type> CompatibleConversionTypes { get; set; }
@@ -124,7 +106,5 @@ namespace Artemis.UI.Shared.DataModelVisualization
protected virtual void OnCancel()
{
}
internal abstract object InternalConvertToSupportedType(object source);
}
}
@@ -64,8 +64,10 @@ namespace Artemis.UI.Shared.Services
return _dataModelService.GetPluginExtendsDataModel(plugin);
}
public DataModelVisualizationRegistration RegisterDataModelInput<T>(PluginInfo pluginInfo, IReadOnlyCollection<Type> compatibleConversionTypes) where T : DataModelInputViewModel
public DataModelVisualizationRegistration RegisterDataModelInput<T>(PluginInfo pluginInfo, IReadOnlyCollection<Type> compatibleConversionTypes = null) where T : DataModelInputViewModel
{
if (compatibleConversionTypes == null)
compatibleConversionTypes = new List<Type>();
var viewModelType = typeof(T);
lock (_registeredDataModelEditors)
{
@@ -158,7 +160,12 @@ namespace Artemis.UI.Shared.Services
{
lock (_registeredDataModelEditors)
{
// Prefer a VM that natively supports the type
var match = _registeredDataModelEditors.FirstOrDefault(d => d.SupportedType == propertyType);
// Fall back on a VM that supports the type through conversion
if (match == null)
match = _registeredDataModelEditors.FirstOrDefault(d => d.CompatibleConversionTypes.Contains(propertyType));
if (match != null)
{
var viewModel = InstantiateDataModelInputViewModel(match, description, initialValue);
@@ -173,8 +180,14 @@ namespace Artemis.UI.Shared.Services
private DataModelInputViewModel InstantiateDataModelInputViewModel(DataModelVisualizationRegistration registration, DataModelPropertyAttribute description, object initialValue)
{
// The view models expecting value types shouldn't be given null, avoid that
if (initialValue == null && registration.SupportedType.IsValueType)
initialValue = Activator.CreateInstance(registration.SupportedType);
if (registration.SupportedType.IsValueType)
{
if (initialValue == null)
initialValue = Activator.CreateInstance(registration.SupportedType);
}
// This assumes the type can be converted, that has been checked when the VM was created
if (initialValue != null && initialValue.GetType() != registration.SupportedType)
initialValue = Convert.ChangeType(initialValue, registration.SupportedType);
var parameters = new IParameter[]
{