Interactive 2D wave optics simulator. Turn any image into a refractive scene, add sources, watch waves refract, diffract and interfere. Probes, intensity maps, video export.
C#
Number box scrubbing: a drag step and precision per box, wrapping at the monitor edge, text selection on a focused box
Alexander Graef3541518ca679
diff --git a/WaveDudeNext.WPF/Controls/NumberBox.cs b/WaveDudeNext.WPF/Controls/NumberBox.csindex f065880..e586006 100644--- a/WaveDudeNext.WPF/Controls/NumberBox.cs+++ b/WaveDudeNext.WPF/Controls/NumberBox.cs@@ -16,6 +16,7 @@// <https://www.gnu.org/licenses/>.using System.Globalization;+using System.Runtime.InteropServices;using System.Windows;using System.Windows.Controls;using System.Windows.Controls.Primitives;@@ -29,14 +30,17 @@ namespace WaveDudeNext.WPF.Controls;/// change the value by <see cref="Increment"/>, PageUp/PageDown by ten increments, the mouse wheel/// does the same while the box has keyboard focus, and the two repeat buttons on the right do it with/// the mouse, and a horizontal drag on the text itself scrubs the value, Blender style (the user's request,-/// 2026-10-18): press, move sideways past the drag threshold and the value follows the mouse, one increment-/// per <see cref="PixelsPerStep"/> pixels, ten times finer with Shift, ten times coarser with Ctrl; Escape during-/// the drag restores the value the drag started from; a press without movement stays an ordinary click that-/// places the caret. Every step clamps to <see cref="Minimum"/>..<see cref="Maximum"/>, writes the text back-/// through the <see cref="TextBox.Text"/> binding at once and selects it, so a source moves on the-/// picture with each key press. Typing stays plain text: the binding converts and the model clamps as-/// before, and Enter/Escape come from <see cref="EditBehavior"/> through the base text box style.-/// Numbers are parsed and written in the current culture, like the bindings. UI thread.+/// 2026-10-18): press, move sideways past the drag threshold and the value follows the mouse by+/// <see cref="DragStep"/> per pixel (by default one increment per <see cref="PixelsPerStep"/> pixels), ten times+/// finer with Shift, ten times coarser with Ctrl, rounded to <see cref="Decimals"/> places (by default the+/// increment's, so a whole-number box never writes a fraction); a drag that reaches the edge of the monitor wraps+/// the cursor to the opposite edge and goes on (2026-10-19, as Blender does); Escape during the drag restores the+/// value the drag started from; a press without movement stays an ordinary click that places the caret. Every step+/// clamps to <see cref="Minimum"/>..<see cref="Maximum"/>, writes the text back through the+/// <see cref="TextBox.Text"/> binding at once and selects it, so a source moves on the picture with each key+/// press. Typing stays plain text: the binding converts and the model clamps as before, and Enter/Escape come+/// from <see cref="EditBehavior"/> through the base text box style. Numbers are parsed and written in the current+/// culture, like the bindings. UI thread./// </summary>public sealed class NumberBox : TextBox{@@ -52,13 +56,26 @@ public sealed class NumberBox : TextBoxpublic static readonly DependencyProperty IncrementProperty = DependencyProperty.Register(nameof(Increment), typeof(double), typeof(NumberBox), new PropertyMetadata(1.0));+ /// <summary>Identifies the <see cref="DragStep"/> property.</summary>+ public static readonly DependencyProperty DragStepProperty = DependencyProperty.Register(+ nameof(DragStep), typeof(double), typeof(NumberBox), new PropertyMetadata(double.NaN));++ /// <summary>Identifies the <see cref="Decimals"/> property.</summary>+ public static readonly DependencyProperty DecimalsProperty = DependencyProperty.Register(+ nameof(Decimals), typeof(int), typeof(NumberBox), new PropertyMetadata(-1));+/// <summary>Steps per PageUp/PageDown and per wheel notch with Shift.</summary>private const int PageSteps = 10;- /// <summary>Mouse pixels per increment while scrubbing; Shift makes it ten times as many, Ctrl a tenth.</summary>+ /// <summary>Mouse pixels per increment while scrubbing when <see cref="DragStep"/> is not set.</summary>public const double PixelsPerStep = 2.0;+ /// <summary>Pixels the cursor is set in from the far edge when a drag wraps around the monitor, so it does not wrap straight back.</summary>+ private const int WrapInset = 2;+private Point? _pressPoint;+ private double _pressScreenX;+ private double _wrapOffset;private double _pressValue;private bool _scrubbing;@@ -83,6 +100,27 @@ public sealed class NumberBox : TextBoxset => SetValue(IncrementProperty, value);}+ /// <summary>+ /// Change of the value per pixel of a horizontal drag (in device-independent pixels), before the Shift and Ctrl+ /// factors; NaN, the default, means one <see cref="Increment"/> per <see cref="PixelsPerStep"/> pixels.+ /// </summary>+ public double DragStep+ {+ get => (double)GetValue(DragStepProperty);+ set => SetValue(DragStepProperty, value);+ }++ /// <summary>+ /// Decimal places a step or drag writes; -1, the default, means as many as <see cref="Increment"/> has, so a box+ /// bound to an integer never writes a fraction. A box bound to a real-valued property sets it to let a fine+ /// drag (Shift) reach below its increment.+ /// </summary>+ public int Decimals+ {+ get => (int)GetValue(DecimalsProperty);+ set => SetValue(DecimalsProperty, value);+ }+/// <summary>The value the text holds, or null while it does not parse.</summary>public double? Value => double.TryParse(Text, NumberStyles.Float, CultureInfo.CurrentCulture, out double value) ? value : null;@@ -100,10 +138,10 @@ public sealed class NumberBox : TextBoxWrite((Value ?? 0.0) + steps * Increment);}- /// <summary>Clamps <paramref name="value"/>, rounds it to the increment's precision, writes the text, pushes it into the binding and selects it.</summary>+ /// <summary>Clamps <paramref name="value"/>, rounds it to the box's precision, writes the text, pushes it into the binding and selects it.</summary>private void Write(double value){- int decimals = Math.Clamp(DecimalsOf(Increment), 0, 10);+ int decimals = Math.Clamp(Decimals >= 0 ? Decimals : DecimalsOf(Increment), 0, 10);value = Math.Round(Math.Clamp(value, Minimum, Maximum), decimals);Text = value.ToString(CultureInfo.CurrentCulture);GetBindingExpression(TextProperty)?.UpdateSource();@@ -152,12 +190,16 @@ public sealed class NumberBox : TextBox{base.OnPreviewMouseLeftButtonDown(e);_pressPoint = null;- // a press on the repeat buttons is theirs; one on the text may become a scrub, or stay a click- if (e.Handled || IsReadOnly || !IsEnabled || IsOnRepeatButton(e.OriginalSource))+ // a press on the repeat buttons is theirs; one on the text of a box that already has the focus selects text+ // as in any text box (Blender's rule, 2026-10-19: a drag scrubbed where a selection was meant); one on an+ // unfocused box may become a scrub, or stay a click that focuses it and places the caret+ if (e.Handled || IsReadOnly || !IsEnabled || IsOnRepeatButton(e.OriginalSource) || IsKeyboardFocusWithin){return;}_pressPoint = e.GetPosition(this);+ _pressScreenX = PointToScreen(_pressPoint.Value).X;+ _wrapOffset = 0.0;_pressValue = Value ?? 0.0;}@@ -165,11 +207,17 @@ public sealed class NumberBox : TextBoxprotected override void OnPreviewMouseMove(MouseEventArgs e){base.OnPreviewMouseMove(e);- if (_pressPoint is not { } start || e.LeftButton != MouseButtonState.Pressed)+ // the text box captured the mouse on the press, so a release always reaches this box or ends the capture;+ // the button state is read as well in case neither happened+ if (_pressPoint == null || (e.LeftButton != MouseButtonState.Pressed && !IsMouseCaptureWithin)){return;}- double dx = e.GetPosition(this).X - start.X;+ // measured on the screen (device pixels) so a wrap of the cursor can be taken out again; the box's own+ // coordinates would also shift if the drag scrolled or re-laid out the form+ Point screen = PointToScreen(e.GetPosition(this));+ double dpi = VisualTreeHelper.GetDpi(this).DpiScaleX;+ double dx = (screen.X + _wrapOffset - _pressScreenX) / dpi;if (!_scrubbing){if (Math.Abs(dx) < SystemParameters.MinimumHorizontalDragDistance)@@ -184,19 +232,94 @@ public sealed class NumberBox : TextBoxFocus();}}- double pixels = PixelsPerStep;+ double step = double.IsNaN(DragStep) ? Increment / PixelsPerStep : DragStep;if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0){- pixels *= 10.0;+ step /= 10.0;}else if ((Keyboard.Modifiers & ModifierKeys.Control) != 0){- pixels /= 10.0;+ step *= 10.0;}- Write(_pressValue + Math.Round(dx / pixels) * Increment);+ Write(_pressValue + dx * step);+ WrapAtMonitorEdge(screen);e.Handled = true; // not a text selection}+ /// <summary>+ /// A drag that reaches the left or right edge of the monitor under the cursor jumps the cursor to the opposite+ /// edge, remembering the jump in <see cref="_wrapOffset"/> so the drag distance runs on unbroken; otherwise a+ /// value could only be dragged as far as the screen allowed.+ /// </summary>+ private void WrapAtMonitorEdge(Point screen)+ {+ var at = new NativePoint { X = (int)Math.Round(screen.X), Y = (int)Math.Round(screen.Y) };+ IntPtr monitor = MonitorFromPoint(at, MonitorDefaultToNearest);+ var info = new MonitorInfo { Size = Marshal.SizeOf<MonitorInfo>() };+ if (monitor == IntPtr.Zero || !GetMonitorInfo(monitor, ref info))+ {+ return;+ }+ int left = info.Monitor.Left, right = info.Monitor.Right; // right is exclusive+ if (right - left <= 2 * WrapInset + 1)+ {+ return;+ }+ int target;+ if (at.X <= left)+ {+ target = right - 1 - WrapInset;+ }+ else if (at.X >= right - 1)+ {+ target = left + WrapInset;+ }+ else+ {+ return;+ }+ if (SetCursorPos(target, at.Y))+ {+ _wrapOffset += at.X - target;+ }+ }++ private const uint MonitorDefaultToNearest = 2;++ [StructLayout(LayoutKind.Sequential)]+ private struct NativePoint+ {+ public int X;+ public int Y;+ }++ [StructLayout(LayoutKind.Sequential)]+ private struct NativeRect+ {+ public int Left;+ public int Top;+ public int Right;+ public int Bottom;+ }++ [StructLayout(LayoutKind.Sequential)]+ private struct MonitorInfo+ {+ public int Size;+ public NativeRect Monitor;+ public NativeRect Work;+ public uint Flags;+ }++ [DllImport("user32.dll")]+ private static extern IntPtr MonitorFromPoint(NativePoint point, uint flags);++ [DllImport("user32.dll")]+ private static extern bool GetMonitorInfo(IntPtr monitor, ref MonitorInfo info);++ [DllImport("user32.dll")]+ private static extern bool SetCursorPos(int x, int y);+/// <inheritdoc/>protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e){@@ -217,6 +340,7 @@ public sealed class NumberBox : TextBox{EndScrub();}+ _pressPoint = null;}private void EndScrub()diff --git a/WaveDudeNext.WPF/Views/PartCommonView.xaml b/WaveDudeNext.WPF/Views/PartCommonView.xamlindex ac92251..0d9c03a 100644--- a/WaveDudeNext.WPF/Views/PartCommonView.xaml+++ b/WaveDudeNext.WPF/Views/PartCommonView.xaml@@ -44,7 +44,7 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or<controls:NumberBox Text="{Binding Y}" Minimum="0" ToolTip="{meta:Hint model:OpticalPart.Y}" AutomationProperties.AutomationId="PartY" /><TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:OpticalPart.Angle}" />- <controls:NumberBox Text="{Binding Angle}" Minimum="-180" Maximum="180" Increment="5" ToolTip="{meta:Hint model:OpticalPart.Angle}" AutomationProperties.AutomationId="PartAngleBox" />+ <controls:NumberBox Text="{Binding Angle}" Minimum="-180" Maximum="180" Increment="5" DragStep="0.5" Decimals="1" ToolTip="{meta:Hint model:OpticalPart.Angle}" AutomationProperties.AutomationId="PartAngleBox" /></controls:FormPanel></StackPanel></UserControl>diff --git a/WaveDudeNext.WPF/Views/SimulationView.xaml b/WaveDudeNext.WPF/Views/SimulationView.xamlindex d39c4ec..c0a6d92 100644--- a/WaveDudeNext.WPF/Views/SimulationView.xaml+++ b/WaveDudeNext.WPF/Views/SimulationView.xaml@@ -44,7 +44,7 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or<controls:NumberBox Text="{Binding WaveSpeed}" Minimum="0.000001" Maximum="1000000" Increment="0.01" ToolTip="{meta:Hint model:Simulation.WaveSpeed}" /><TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:Simulation.TimeDecay}" />- <controls:NumberBox Text="{Binding TimeDecay}" Minimum="1" Maximum="100" ToolTip="{meta:Hint model:Simulation.TimeDecay}" />+ <controls:NumberBox Text="{Binding TimeDecay}" Minimum="1" Maximum="100" DragStep="0.1" Decimals="1" ToolTip="{meta:Hint model:Simulation.TimeDecay}" /><TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:Simulation.SourceFrequencyScale}" /><controls:NumberBox Text="{Binding SourceFrequencyScale}" Minimum="0" Maximum="1000" Increment="0.1" ToolTip="{meta:Hint model:Simulation.SourceFrequencyScale}" />diff --git a/WaveDudeNext.WPF/Views/SourceView.xaml b/WaveDudeNext.WPF/Views/SourceView.xamlindex aea6e9c..0d74b8b 100644--- a/WaveDudeNext.WPF/Views/SourceView.xaml+++ b/WaveDudeNext.WPF/Views/SourceView.xaml@@ -69,13 +69,13 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or<Label Style="{StaticResource FormSection}" Content="{DynamicResource SectionWave}" /><controls:FormPanel><TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:Source.Frequency}" />- <controls:NumberBox Text="{Binding Frequency}" Minimum="0.01" Maximum="255" ToolTip="{meta:Hint model:Source.Frequency}" />+ <controls:NumberBox Text="{Binding Frequency}" Minimum="0.01" Maximum="255" DragStep="0.1" Decimals="2" ToolTip="{meta:Hint model:Source.Frequency}" /><TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:Source.Amplitude}" />- <controls:NumberBox Text="{Binding Amplitude}" Minimum="0" Maximum="100" ToolTip="{meta:Hint model:Source.Amplitude}" />+ <controls:NumberBox Text="{Binding Amplitude}" Minimum="0" Maximum="100" DragStep="0.1" Decimals="2" ToolTip="{meta:Hint model:Source.Amplitude}" /><TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:Source.Phase}" />- <controls:NumberBox Text="{Binding Phase}" Minimum="-180" Maximum="180" ToolTip="{meta:Hint model:Source.Phase}" />+ <controls:NumberBox Text="{Binding Phase}" Minimum="-180" Maximum="180" DragStep="0.5" Decimals="2" ToolTip="{meta:Hint model:Source.Phase}" /><!-- read-only: the wavelength this frequency gives with the project's wave speed (Simulation.Wavelength) --><TextBlock Style="{StaticResource FormLabel}" Text="{DynamicResource LabelWavelength}" />@@ -133,7 +133,7 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or<controls:NumberBox Text="{Binding SpacingY}" ToolTip="{meta:Hint model:SourceArray.SpacingY}" /><TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:SourceArray.Angle}" />- <controls:NumberBox Text="{Binding Angle}" Minimum="-180" Maximum="180" Increment="5" ToolTip="{meta:Hint model:SourceArray.Angle}" />+ <controls:NumberBox Text="{Binding Angle}" Minimum="-180" Maximum="180" Increment="5" DragStep="0.5" Decimals="1" ToolTip="{meta:Hint model:SourceArray.Angle}" /><!-- the four random amounts are offsets added per emitter on top of the values above (the author's point 9) --><Label controls:FormPanel.FullWidth="True" Style="{StaticResource FormSection}" Content="{DynamicResource SectionRandom}" />diff --git a/WaveDudeNext.WPF/Views/TrianglePartView.xaml b/WaveDudeNext.WPF/Views/TrianglePartView.xamlindex 286cfa3..816d7b5 100644--- a/WaveDudeNext.WPF/Views/TrianglePartView.xaml+++ b/WaveDudeNext.WPF/Views/TrianglePartView.xaml@@ -65,10 +65,10 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or<controls:NumberBox Text="{Binding Base}" Minimum="2" Increment="10" ToolTip="{meta:Hint model:TrianglePart.Base}" AutomationProperties.AutomationId="PartBase" /><TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:TrianglePart.AngleA}" />- <controls:NumberBox Style="{StaticResource AngleABox}" Text="{Binding AngleA}" Minimum="1" Maximum="178" ToolTip="{meta:Hint model:TrianglePart.AngleA}" AutomationProperties.AutomationId="PartAngleA" />+ <controls:NumberBox Style="{StaticResource AngleABox}" Text="{Binding AngleA}" Minimum="1" Maximum="178" DragStep="0.5" Decimals="1" ToolTip="{meta:Hint model:TrianglePart.AngleA}" AutomationProperties.AutomationId="PartAngleA" /><TextBlock Style="{StaticResource FormLabel}" Text="{meta:Label model:TrianglePart.AngleB}" />- <controls:NumberBox Style="{StaticResource AngleBBox}" Text="{Binding AngleB}" Minimum="1" Maximum="178" ToolTip="{meta:Hint model:TrianglePart.AngleB}" AutomationProperties.AutomationId="PartAngleB" />+ <controls:NumberBox Style="{StaticResource AngleBBox}" Text="{Binding AngleB}" Minimum="1" Maximum="178" DragStep="0.5" Decimals="1" ToolTip="{meta:Hint model:TrianglePart.AngleB}" AutomationProperties.AutomationId="PartAngleB" /><!-- read-only: what the two base angles leave for the apex --><TextBlock Style="{StaticResource FormLabel}" Text="{DynamicResource LabelApexAngle}" />