Browse Source

Number Input expressions wip

Krzysztof Krysiński 1 year ago
parent
commit
deebedc4ff

+ 3 - 0
src/PixiEditor.AvaloniaUI/PixiEditor.AvaloniaUI.csproj

@@ -98,6 +98,9 @@
       <Reference Include="Microsoft.Xaml.Behaviors">
       <Reference Include="Microsoft.Xaml.Behaviors">
         <HintPath>..\..\..\..\Users\flubb\.nuget\packages\microsoft.xaml.behaviors.wpf\1.1.31\lib\net5.0-windows7.0\Microsoft.Xaml.Behaviors.dll</HintPath>
         <HintPath>..\..\..\..\Users\flubb\.nuget\packages\microsoft.xaml.behaviors.wpf\1.1.31\lib\net5.0-windows7.0\Microsoft.Xaml.Behaviors.dll</HintPath>
       </Reference>
       </Reference>
+      <Reference Include="PixiDocks.Avalonia">
+        <HintPath>..\..\..\PixiDocks\src\PixiDocks.Avalonia\bin\Debug\net8.0\PixiDocks.Avalonia.dll</HintPath>
+      </Reference>
       <Reference Include="PixiDocks.Core">
       <Reference Include="PixiDocks.Core">
         <HintPath>..\..\..\PixiDocks\src\PixiDocks.Core\bin\Debug\net8.0\PixiDocks.Core.dll</HintPath>
         <HintPath>..\..\..\PixiDocks\src\PixiDocks.Core\bin\Debug\net8.0\PixiDocks.Core.dll</HintPath>
       </Reference>
       </Reference>

+ 42 - 4
src/PixiEditor.AvaloniaUI/Views/Input/NumberInput.axaml.cs

@@ -1,4 +1,5 @@
-using System.Globalization;
+using System.Data;
+using System.Globalization;
 using System.Linq;
 using System.Linq;
 using System.Text.RegularExpressions;
 using System.Text.RegularExpressions;
 using Avalonia;
 using Avalonia;
@@ -125,16 +126,53 @@ internal partial class NumberInput : UserControl
     private static bool TryParse(string s, out double value)
     private static bool TryParse(string s, out double value)
     {
     {
         s = s.Replace(",", ".");
         s = s.Replace(",", ".");
-        return double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out value);
+
+        if (double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out value))
+        {
+            return true;
+        }
+        else
+        {
+            return TryEvaluateExpression(s, out value);
+        }
+
+        return false;
+    }
+
+    private static bool TryEvaluateExpression(string s, out double value)
+    {
+        try
+        {
+            DataTable dt = new DataTable();
+            var computed = dt.Compute(s, "");
+            if (IsNumber(computed))
+            {
+                value = Convert.ChangeType(computed, typeof(double)) as double? ?? 0;
+                return true;
+            }
+
+            value = 0;
+            return false;
+        }
+        catch
+        {
+            value = 0;
+            return false;
+        }
+    }
+
+    private static bool IsNumber(object value)
+    {
+        return value is sbyte or byte or short or ushort or int or uint or long or ulong or float or double or decimal;
     }
     }
 
 
     private static void FormattedValueChanged(AvaloniaPropertyChangedEventArgs<string> e)
     private static void FormattedValueChanged(AvaloniaPropertyChangedEventArgs<string> e)
     {
     {
         NumberInput input = (NumberInput)e.Sender;
         NumberInput input = (NumberInput)e.Sender;
-        if(ContainsInvalidCharacter(e.NewValue.Value))
+        /*if(ContainsInvalidCharacter(e.NewValue.Value))
         {
         {
             input.FormattedValue = e.OldValue.Value;
             input.FormattedValue = e.OldValue.Value;
-        }
+        }*/
     }
     }
 
 
     private static bool ContainsInvalidCharacter(string text)
     private static bool ContainsInvalidCharacter(string text)