Skip to content

Commit

Permalink
Added ability to use mouse wheel for editing number input's
Browse files Browse the repository at this point in the history
  • Loading branch information
CPKreu committed Dec 10, 2021
1 parent 54f5f21 commit 14f8f5e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
2 changes: 1 addition & 1 deletion PixiEditor/Views/UserControls/NumberInput.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
mc:Ignorable="d"
d:DesignHeight="20" d:DesignWidth="40" x:Name="numberInput">
<TextBox TextAlignment="Center" Style="{StaticResource DarkTextBoxStyle}" Focusable="True"
InputScope="Number"
InputScope="Number" MouseWheel="TextBox_MouseWheel"
PreviewTextInput="TextBox_PreviewTextInput" Text="{Binding ElementName=numberInput, Path=Value}">
<i:Interaction.Behaviors>
<behaviours:TextBoxFocusBehavior/>
Expand Down
18 changes: 18 additions & 0 deletions PixiEditor/Views/UserControls/NumberInput.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,23 @@ private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !regex.IsMatch((sender as TextBox).Text.Insert((sender as TextBox).SelectionStart, e.Text));
}

private void TextBox_MouseWheel(object sender, MouseWheelEventArgs e)
{
int step = e.Delta / 100;

if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
Value += step * 2f;
}
else if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
{
Value += step / 2f;
}
else
{
Value += step;
}
}
}
}
17 changes: 5 additions & 12 deletions PixiEditor/Views/UserControls/SizeInput.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

<Border BorderThickness="1" CornerRadius="3.5"
x:Name="border"
Cursor="IBeam" MouseLeftButtonDown="Border_MouseLeftButtonDown">
Cursor="IBeam" MouseLeftButtonDown="Border_MouseLeftButtonDown"
MouseWheel="Border_MouseWheel">
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
Expand All @@ -37,17 +38,9 @@
InputScope="Number" BorderThickness="0" Background="{x:Null}"
Foreground="{Binding Foreground, ElementName=uc}" Focusable="True"
Margin="0,0,5,0" VerticalAlignment="Center"
x:Name="textBox">
<TextBox.Text>
<Binding ElementName="uc"
Path="Size" Mode="TwoWay"
Converter="{converters:ToolSizeToIntConverter}">
<Binding.ValidationRules>
<validators:SizeValidationRule ValidatesOnTargetUpdated="True" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
<d:TextBox.Text>22</d:TextBox.Text>
x:Name="textBox"
Text="{Binding Size, ElementName=uc, Converter={converters:ToolSizeToIntConverter}}"
d:Text="22">
<i:Interaction.Behaviors>
<behaviors:GlobalShortcutFocusBehavior/>
<behaviors:TextBoxFocusBehavior FillSize="True" />
Expand Down
49 changes: 38 additions & 11 deletions PixiEditor/Views/UserControls/SizeInput.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using PixiEditor.Helpers;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace PixiEditor.Views
{
Expand Down Expand Up @@ -75,18 +77,18 @@ public int AspectRatioValue

private static void InputSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((int)e.NewValue > (int)d.GetValue(MaxSizeProperty))
int newValue = (int)e.NewValue;
int maxSize = (int)d.GetValue(MaxSizeProperty);

if (newValue > maxSize)
{
int? oldValue = e.OldValue as int?;
d.SetValue(SizeProperty, maxSize);

if (oldValue is null)
{
d.SetValue(SizeProperty, 0);
}
else
{
d.SetValue(SizeProperty, oldValue.Value);
}
return;
}
else if (newValue <= 0)
{
d.SetValue(SizeProperty, 1);

return;
}
Expand Down Expand Up @@ -125,5 +127,30 @@ private void Border_MouseLeftButtonDown(object sender, System.Windows.Input.Mous
{
textBox.Focus();
}

private void Border_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
int step = e.Delta / 100;

if (Keyboard.IsKeyDown(Key.LeftShift))
{
Size += step * 2;
}
else if (Keyboard.IsKeyDown(Key.LeftCtrl))
{
if (step < 0)
{
Size /= 2;
}
else
{
Size *= 2;
}
}
else
{
Size += step;
}
}
}
}

0 comments on commit 14f8f5e

Please sign in to comment.