Skip to content

Commit

Permalink
Window will refuse to bind one key on more than one action.
Browse files Browse the repository at this point in the history
  • Loading branch information
bcrosnier committed Jun 12, 2013
1 parent 56e4262 commit 109d022
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions RideOnMotion/RideOnMotion/KeyboardSettingsWindow.xaml.cs
Expand Up @@ -66,6 +66,28 @@ private void LoadSettings()
RefreshLayoutKeys();
}

private bool HasDuplicateKeys()
{
List<Key> keyList = new List<Key>();
keyList.Add( PitchUpKey );
keyList.Add( PitchDownKey );
keyList.Add( RollLeftKey );
keyList.Add( RollRightKey );
keyList.Add( YawLeftKey );
keyList.Add( YawRightKey );
keyList.Add( GazUpKey );
keyList.Add( GazDownKey );
keyList.Add( TakeoffKey );
keyList.Add( LandKey );
keyList.Add( HoverKey );
keyList.Add( CameraSwapKey );
keyList.Add( EmergencyKey );
keyList.Add( FlatTrimKey );
keyList.Add( SpecialActionKey );

return keyList.GroupBy( x => x ).Any(c => c.Count() > 1);
}

private void SaveSettings()
{
Settings.Default.PitchUp = (int)PitchUpKey;
Expand Down Expand Up @@ -98,7 +120,7 @@ private void RestoreDefaultSettings()
* [TODO]
* Note: With WPF, this SHOULD refresh all bounds listeners.
* However, there is no telling what will happen with other PropertyChanged listeners.
* So, yes. This is an ugly hack, and done with full knowledge of it. :)
* So, yes. This is an ugly hack, and one done with full knowledge of it. :)
* -- Benjamin
* */
PropertyChanged( this, new PropertyChangedEventArgs( null ) );
Expand Down Expand Up @@ -136,6 +158,11 @@ private void RaisePropertyChanged( [System.Runtime.CompilerServices.CallerMember
}
#endregion INotifyPropertyChanged utilities

private void DuplicateKeyAlert()
{
MessageBox.Show( "You cannot use the same key for more than one action.", "Duplicate keys", MessageBoxButton.OK );
}

private void ButtonRestoreDefaults_Click( object sender, RoutedEventArgs e )
{
RestoreDefaultSettings();
Expand All @@ -145,7 +172,12 @@ private void ButtonRestoreDefaults_Click( object sender, RoutedEventArgs e )

private void ButtonApply_Click( object sender, RoutedEventArgs e )
{
SaveSettings();
if ( !HasDuplicateKeys() )
{
SaveSettings();
} else {
DuplicateKeyAlert();
}
RefreshLayoutKeys();
}

Expand All @@ -156,8 +188,15 @@ private void ButtonCancel_Click( object sender, RoutedEventArgs e )

private void ButtonOK_Click( object sender, RoutedEventArgs e )
{
SaveSettings();
this.Close();
if ( !HasDuplicateKeys() )
{
SaveSettings();
this.Close();
}
else
{
DuplicateKeyAlert();
}
}

private void TextBox_KeyDownReplace( object sender, KeyEventArgs e )
Expand Down

0 comments on commit 109d022

Please sign in to comment.