Created
March 10, 2020 18:30
-
-
Save pbhogan/6ad47df1331da9353cbd3717effbc7a1 to your computer and use it in GitHub Desktop.
ComboBindingSource for InControl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Area51 | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using InControl; | |
public class ComboBindingSource : BindingSource | |
{ | |
public enum EventType : int | |
{ | |
IsPressed = 0, | |
IsNotPressed, | |
WasPressed, | |
WasReleased | |
} | |
class Term | |
{ | |
public EventType eventType; | |
public BindingSource bindingSource; | |
public bool thisState; | |
public bool lastState; | |
public bool Query( InputDevice device ) | |
{ | |
lastState = thisState; | |
thisState = bindingSource.GetState( device ); | |
switch (eventType) | |
{ | |
case EventType.IsPressed: | |
return thisState; | |
case EventType.IsNotPressed: | |
return !thisState; | |
case EventType.WasPressed: | |
return thisState && !lastState; | |
case EventType.WasReleased: | |
return !thisState && lastState; | |
} | |
return false; | |
} | |
public bool IsSameAs( Term other ) | |
{ | |
if (other == null) | |
{ | |
return false; | |
} | |
return (eventType == other.eventType && bindingSource == other.bindingSource); | |
} | |
} | |
List<Term> terms; | |
public ComboBindingSource() | |
{ | |
terms = new List<Term>(); | |
} | |
public ComboBindingSource When( EventType eventType, BindingSource bindingSource ) | |
{ | |
var term = new Term() | |
{ | |
eventType = eventType, bindingSource = bindingSource | |
}; | |
terms.Add( term ); | |
return this; | |
} | |
#region DeviceBindingSource | |
public ComboBindingSource When( EventType eventType, InputControlType control ) | |
{ | |
return When( eventType, new DeviceBindingSource( control ) ); | |
} | |
public ComboBindingSource WhenIsPressed( InputControlType control ) | |
{ | |
return When( EventType.IsPressed, new DeviceBindingSource( control ) ); | |
} | |
public ComboBindingSource WhenIsNotPressed( InputControlType control ) | |
{ | |
return When( EventType.IsNotPressed, new DeviceBindingSource( control ) ); | |
} | |
public ComboBindingSource WhenWasPressed( InputControlType control ) | |
{ | |
return When( EventType.WasPressed, new DeviceBindingSource( control ) ); | |
} | |
public ComboBindingSource WhenWasReleased( InputControlType control ) | |
{ | |
return When( EventType.WasReleased, new DeviceBindingSource( control ) ); | |
} | |
#endregion | |
#region MouseBindingSource | |
public ComboBindingSource When( EventType eventType, Mouse control ) | |
{ | |
return When( eventType, new MouseBindingSource( control ) ); | |
} | |
public ComboBindingSource WhenIsPressed( Mouse control ) | |
{ | |
return When( EventType.IsPressed, new MouseBindingSource( control ) ); | |
} | |
public ComboBindingSource WhenIsNotPressed( Mouse control ) | |
{ | |
return When( EventType.IsNotPressed, new MouseBindingSource( control ) ); | |
} | |
public ComboBindingSource WhenWasPressed( Mouse control ) | |
{ | |
return When( EventType.WasPressed, new MouseBindingSource( control ) ); | |
} | |
public ComboBindingSource WhenWasReleased( Mouse control ) | |
{ | |
return When( EventType.WasReleased, new MouseBindingSource( control ) ); | |
} | |
#endregion | |
#region KeyBindingSource | |
public ComboBindingSource When( EventType eventType, KeyCombo keyCombo ) | |
{ | |
return When( eventType, new KeyBindingSource( keyCombo ) ); | |
} | |
public ComboBindingSource WhenIsPressed( KeyCombo keyCombo ) | |
{ | |
return When( EventType.IsPressed, new KeyBindingSource( keyCombo ) ); | |
} | |
public ComboBindingSource WhenIsNotPressed( KeyCombo keyCombo ) | |
{ | |
return When( EventType.IsNotPressed, new KeyBindingSource( keyCombo ) ); | |
} | |
public ComboBindingSource WhenWasPressed( KeyCombo keyCombo ) | |
{ | |
return When( EventType.WasPressed, new KeyBindingSource( keyCombo ) ); | |
} | |
public ComboBindingSource WhenWasReleased( KeyCombo keyCombo ) | |
{ | |
return When( EventType.WasReleased, new KeyBindingSource( keyCombo ) ); | |
} | |
public ComboBindingSource When( EventType eventType, params Key[] keys ) | |
{ | |
return When( eventType, new KeyBindingSource( keys ) ); | |
} | |
public ComboBindingSource WhenIsPressed( params Key[] keys ) | |
{ | |
return When( EventType.IsPressed, new KeyBindingSource( keys ) ); | |
} | |
public ComboBindingSource WhenIsNotPressed( params Key[] keys ) | |
{ | |
return When( EventType.IsNotPressed, new KeyBindingSource( keys ) ); | |
} | |
public ComboBindingSource WhenWasPressed( params Key[] keys ) | |
{ | |
return When( EventType.WasPressed, new KeyBindingSource( keys ) ); | |
} | |
public ComboBindingSource WhenWasReleased( params Key[] keys ) | |
{ | |
return When( EventType.WasReleased, new KeyBindingSource( keys ) ); | |
} | |
#endregion | |
public override float GetValue( InputDevice inputDevice ) | |
{ | |
return GetState( inputDevice ) ? 1.0f : 0.0f; | |
} | |
public override bool GetState( InputDevice inputDevice ) | |
{ | |
var termsCount = terms.Count; | |
if (termsCount == 0) | |
{ | |
return false; | |
} | |
for (var i = 0; i < termsCount; i++) | |
{ | |
if (!terms[i].Query( inputDevice )) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
public override string Name | |
{ | |
get | |
{ | |
return "ComboBindingSource"; | |
} | |
} | |
public override string DeviceName | |
{ | |
get | |
{ | |
return ""; | |
} | |
} | |
public override InputDeviceClass DeviceClass | |
{ | |
get | |
{ | |
return InputDeviceClass.Unknown; | |
} | |
} | |
public override InputDeviceStyle DeviceStyle | |
{ | |
get | |
{ | |
return InputDeviceStyle.Unknown; | |
} | |
} | |
bool AllTermsAreEqual( List<Term> otherTerms ) | |
{ | |
if (otherTerms == null || terms.Count != otherTerms.Count) | |
{ | |
return false; | |
} | |
var termsCount = terms.Count; | |
for (var i = 0; i < termsCount; i++) | |
{ | |
if (!terms[i].IsSameAs( otherTerms[i] )) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
public override bool Equals( BindingSource other ) | |
{ | |
if (other == null) | |
{ | |
return false; | |
} | |
var bindingSource = other as ComboBindingSource; | |
if (bindingSource != null) | |
{ | |
return AllTermsAreEqual( bindingSource.terms ); | |
} | |
return false; | |
} | |
public override bool Equals( object other ) | |
{ | |
if (other == null) | |
{ | |
return false; | |
} | |
var bindingSource = other as ComboBindingSource; | |
if (bindingSource != null) | |
{ | |
return AllTermsAreEqual( bindingSource.terms ); | |
} | |
return false; | |
} | |
public override int GetHashCode() | |
{ | |
return terms.GetHashCode(); | |
} | |
public override BindingSourceType BindingSourceType | |
{ | |
get | |
{ | |
return BindingSourceType.None; // ComboBindingSource | |
} | |
} | |
public override void Save( BinaryWriter writer ) {} | |
public override void Load( BinaryReader reader, UInt16 dataFormatVersion ) {} | |
} | |
} | |
/**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment