diff --git a/.nuke b/.nuke index a12280b7..9cfbd94f 100644 Binary files a/.nuke and b/.nuke differ diff --git a/README.md b/README.md index d44d0141..92e36804 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Universal Control Remapper -[![GitHub release](https://img.shields.io/badge/release-v0.3.1-blue.svg)](https://github.com/Snoothy/UCR/releases/tag/v0.3.0) [![IOWrapper version](https://img.shields.io/badge/IOWrapper-v0.5.4-blue.svg)](https://github.com/evilC/IOWrapper) [![license](https://img.shields.io/github/license/snoothy/ucr.svg)](https://github.com/Snoothy/UCR/blob/master/LICENSE) [![Github All Releases](https://img.shields.io/github/downloads/snoothy/ucr/total.svg)](https://github.com/Snoothy/UCR/releases) +[![GitHub release](https://img.shields.io/badge/release-v0.4.0-blue.svg)](https://github.com/Snoothy/UCR/releases/tag/v0.4.0) [![IOWrapper version](https://img.shields.io/badge/IOWrapper-v0.5.7-blue.svg)](https://github.com/evilC/IOWrapper) [![license](https://img.shields.io/github/license/snoothy/ucr.svg)](https://github.com/Snoothy/UCR/blob/master/LICENSE) [![Github All Releases](https://img.shields.io/github/downloads/snoothy/ucr/total.svg)](https://github.com/Snoothy/UCR/releases) Universal Control Remapper is a complete rewrite of the original [UCR](https://github.com/evilC/UCR), created in collaboration with [evilC](https://github.com/evilC/). @@ -71,7 +71,8 @@ UCR supports input and output devices through plugins using the [IOWrapper](http - Keyboard (using [interception](https://github.com/oblitum/Interception)) - Mouse (using [interception](https://github.com/oblitum/Interception)) - +## Build ## +It is required to run the build script before building with Visual Studio. Run `.\build.ps1 InitProject` from powershell to initialize the required dependencies. All subsequent builds can be done from Visual Studio 2017 ## License ## diff --git a/UCR.Core/Managers/SubscriptionsManager.cs b/UCR.Core/Managers/SubscriptionsManager.cs index f351d8e5..38b4531d 100644 --- a/UCR.Core/Managers/SubscriptionsManager.cs +++ b/UCR.Core/Managers/SubscriptionsManager.cs @@ -13,7 +13,7 @@ public class SubscriptionsManager : IDisposable { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - private SubscriptionState SubscriptionState { get; set; } + internal SubscriptionState SubscriptionState { get; set; } private readonly Context _context; public SubscriptionsManager(Context context) @@ -49,17 +49,33 @@ public bool ActivateProfile(Profile profile) Logger.Debug("SubscriptionState successfully activated"); if (!DeactivateProfile()) Logger.Error("Failed to deactivate previous profile successfully"); + + FinalizeNewState(profile, state); - _context.IOController.SetProfileState(state.StateGuid, true); + return true; + } - SubscriptionState = state; + private void FinalizeNewState(Profile profile, SubscriptionState subscriptionState) + { + // Set new active profile + _context.IOController.SetProfileState(subscriptionState.StateGuid, true); + SubscriptionState = subscriptionState; _context.ActiveProfile = profile; + + // Activate plugins + foreach (var mapping in subscriptionState.MappingSubscriptions) + { + foreach (var pluginSubscription in mapping.PluginSubscriptions) + { + pluginSubscription.Plugin.InitializeCacheValues(); + pluginSubscription.Plugin.OnActivate(); + } + } + foreach (var action in _context.ActiveProfileCallbacks) { action(); } - - return true; } public bool DeactivateProfile() @@ -161,11 +177,6 @@ private bool ActivateSubscriptionState(SubscriptionState state) { success &= SubscribeDeviceBindingInput(state, deviceBindingSubscription); } - - foreach (var pluginSubscription in mappingSubscription.PluginSubscriptions) - { - pluginSubscription.Plugin.OnActivate(); - } } state.IsActive = true; diff --git a/UCR.Core/Models/Binding/DeviceBinding.cs b/UCR.Core/Models/Binding/DeviceBinding.cs index 57f39c35..b3cbe9fd 100644 --- a/UCR.Core/Models/Binding/DeviceBinding.cs +++ b/UCR.Core/Models/Binding/DeviceBinding.cs @@ -1,6 +1,9 @@ using System; +using System.ComponentModel; +using System.Runtime.CompilerServices; using System.Xml.Serialization; using HidWizards.IOWrapper.DataTransferObjects; +using HidWizards.UCR.Core.Annotations; namespace HidWizards.UCR.Core.Models.Binding { @@ -12,7 +15,7 @@ public enum DeviceBindingCategory Delta } - public class DeviceBinding + public class DeviceBinding : INotifyPropertyChanged { /* Persistence */ public bool IsBound { get; set; } @@ -35,11 +38,34 @@ public class DeviceBinding public delegate void ValueChanged(long value); + + private ValueChanged _callback; + [XmlIgnore] - public ValueChanged Callback { get; set; } + public ValueChanged Callback + { + get => InputChanged; + set + { + _callback = value; + OnPropertyChanged(); + } + } [XmlIgnore] public ValueChanged OutputSink { get; set; } + private long _currentValue; + [XmlIgnore] + public long CurrentValue + { + get => _currentValue; + set + { + _currentValue = value; + OnPropertyChanged(); + } + } + public DeviceBinding() { Guid = Guid.NewGuid(); @@ -105,7 +131,22 @@ public static DeviceBindingCategory MapCategory(BindingCategory bindingInfoCateg public void WriteOutput(long value) { + CurrentValue = value; OutputSink?.Invoke(value); } + + private void InputChanged(long value) + { + CurrentValue = value; + _callback(value); + } + + public event PropertyChangedEventHandler PropertyChanged; + + [NotifyPropertyChangedInvocator] + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } } } diff --git a/UCR.Core/Models/Mapping.cs b/UCR.Core/Models/Mapping.cs index fb8f4dfe..d7b436e7 100644 --- a/UCR.Core/Models/Mapping.cs +++ b/UCR.Core/Models/Mapping.cs @@ -62,6 +62,7 @@ internal void PrepareMapping() var cm = new CallbackMultiplexer(InputCache, i, Update); Multiplexer.Add(cm); DeviceBindings[i].Callback = cm.Update; + DeviceBindings[i].CurrentValue = 0; } } diff --git a/UCR.Core/Models/Plugin.cs b/UCR.Core/Models/Plugin.cs index 9242202e..50e2063d 100644 --- a/UCR.Core/Models/Plugin.cs +++ b/UCR.Core/Models/Plugin.cs @@ -91,6 +91,19 @@ public virtual void OnActivate() } + public virtual void OnPropertyChanged() + { + + } + + /* + * Called before a plugin OnActivate and OnPropertyChanged to cache run-time values + */ + public virtual void InitializeCacheValues() + { + + } + public virtual void Update(params long[] values) { @@ -120,6 +133,11 @@ protected bool GetState(Guid stateGuid) return Profile.GetRuntimeState(stateGuid); } + protected long ReadOutput(int number) + { + return Outputs[number].CurrentValue; + } + #endregion public void SetProfile(Profile profile) diff --git a/UCR.Core/Models/PluginProperty.cs b/UCR.Core/Models/PluginProperty.cs index 32748cd6..a1d0b6c0 100644 --- a/UCR.Core/Models/PluginProperty.cs +++ b/UCR.Core/Models/PluginProperty.cs @@ -1,4 +1,6 @@ using System; +using System.CodeDom; +using System.Globalization; using System.Reflection; namespace HidWizards.UCR.Core.Models @@ -17,7 +19,12 @@ public dynamic Property set { if (value.Equals(PropertyInfo.GetValue(Plugin))) return; - PropertyInfo.SetValue(Plugin, Convert.ChangeType(value, PropertyInfo.PropertyType)); + PropertyInfo.SetValue(Plugin, Convert.ChangeType(value, PropertyInfo.PropertyType, CultureInfo.InvariantCulture)); + if (Plugin.Profile.IsActive()) + { + Plugin.InitializeCacheValues(); + Plugin.OnPropertyChanged(); + } Plugin.ContextChanged(); } } diff --git a/UCR.Core/Properties/Annotations.cs b/UCR.Core/Properties/Annotations.cs new file mode 100644 index 00000000..7148462f --- /dev/null +++ b/UCR.Core/Properties/Annotations.cs @@ -0,0 +1,1065 @@ +/* MIT License + +Copyright (c) 2016 JetBrains http://www.jetbrains.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. */ + +using System; + +#pragma warning disable 1591 +// ReSharper disable UnusedMember.Global +// ReSharper disable MemberCanBePrivate.Global +// ReSharper disable UnusedAutoPropertyAccessor.Global +// ReSharper disable IntroduceOptionalParameters.Global +// ReSharper disable MemberCanBeProtected.Global +// ReSharper disable InconsistentNaming + +namespace HidWizards.UCR.Core.Annotations +{ + /// + /// Indicates that the value of the marked element could be null sometimes, + /// so the check for null is necessary before its usage. + /// + /// + /// [CanBeNull] object Test() => null; + /// + /// void UseTest() { + /// var p = Test(); + /// var s = p.ToString(); // Warning: Possible 'System.NullReferenceException' + /// } + /// + [AttributeUsage( + AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property | + AttributeTargets.Delegate | AttributeTargets.Field | AttributeTargets.Event | + AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.GenericParameter)] + public sealed class CanBeNullAttribute : Attribute { } + + /// + /// Indicates that the value of the marked element could never be null. + /// + /// + /// [NotNull] object Foo() { + /// return null; // Warning: Possible 'null' assignment + /// } + /// + [AttributeUsage( + AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property | + AttributeTargets.Delegate | AttributeTargets.Field | AttributeTargets.Event | + AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.GenericParameter)] + public sealed class NotNullAttribute : Attribute { } + + /// + /// Can be appplied to symbols of types derived from IEnumerable as well as to symbols of Task + /// and Lazy classes to indicate that the value of a collection item, of the Task.Result property + /// or of the Lazy.Value property can never be null. + /// + [AttributeUsage( + AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property | + AttributeTargets.Delegate | AttributeTargets.Field)] + public sealed class ItemNotNullAttribute : Attribute { } + + /// + /// Can be appplied to symbols of types derived from IEnumerable as well as to symbols of Task + /// and Lazy classes to indicate that the value of a collection item, of the Task.Result property + /// or of the Lazy.Value property can be null. + /// + [AttributeUsage( + AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property | + AttributeTargets.Delegate | AttributeTargets.Field)] + public sealed class ItemCanBeNullAttribute : Attribute { } + + /// + /// Indicates that the marked method builds string by format pattern and (optional) arguments. + /// Parameter, which contains format string, should be given in constructor. The format string + /// should be in -like form. + /// + /// + /// [StringFormatMethod("message")] + /// void ShowError(string message, params object[] args) { /* do something */ } + /// + /// void Foo() { + /// ShowError("Failed: {0}"); // Warning: Non-existing argument in format string + /// } + /// + [AttributeUsage( + AttributeTargets.Constructor | AttributeTargets.Method | + AttributeTargets.Property | AttributeTargets.Delegate)] + public sealed class StringFormatMethodAttribute : Attribute + { + /// + /// Specifies which parameter of an annotated method should be treated as format-string + /// + public StringFormatMethodAttribute([NotNull] string formatParameterName) + { + FormatParameterName = formatParameterName; + } + + [NotNull] public string FormatParameterName { get; private set; } + } + + /// + /// For a parameter that is expected to be one of the limited set of values. + /// Specify fields of which type should be used as values for this parameter. + /// + [AttributeUsage( + AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Field, + AllowMultiple = true)] + public sealed class ValueProviderAttribute : Attribute + { + public ValueProviderAttribute([NotNull] string name) + { + Name = name; + } + + [NotNull] public string Name { get; private set; } + } + + /// + /// Indicates that the function argument should be string literal and match one + /// of the parameters of the caller function. For example, ReSharper annotates + /// the parameter of . + /// + /// + /// void Foo(string param) { + /// if (param == null) + /// throw new ArgumentNullException("par"); // Warning: Cannot resolve symbol + /// } + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class InvokerParameterNameAttribute : Attribute { } + + /// + /// Indicates that the method is contained in a type that implements + /// System.ComponentModel.INotifyPropertyChanged interface and this method + /// is used to notify that some property value changed. + /// + /// + /// The method should be non-static and conform to one of the supported signatures: + /// + /// NotifyChanged(string) + /// NotifyChanged(params string[]) + /// NotifyChanged{T}(Expression{Func{T}}) + /// NotifyChanged{T,U}(Expression{Func{T,U}}) + /// SetProperty{T}(ref T, T, string) + /// + /// + /// + /// public class Foo : INotifyPropertyChanged { + /// public event PropertyChangedEventHandler PropertyChanged; + /// + /// [NotifyPropertyChangedInvocator] + /// protected virtual void NotifyChanged(string propertyName) { ... } + /// + /// string _name; + /// + /// public string Name { + /// get { return _name; } + /// set { _name = value; NotifyChanged("LastName"); /* Warning */ } + /// } + /// } + /// + /// Examples of generated notifications: + /// + /// NotifyChanged("Property") + /// NotifyChanged(() => Property) + /// NotifyChanged((VM x) => x.Property) + /// SetProperty(ref myField, value, "Property") + /// + /// + [AttributeUsage(AttributeTargets.Method)] + public sealed class NotifyPropertyChangedInvocatorAttribute : Attribute + { + public NotifyPropertyChangedInvocatorAttribute() { } + public NotifyPropertyChangedInvocatorAttribute([NotNull] string parameterName) + { + ParameterName = parameterName; + } + + [CanBeNull] public string ParameterName { get; private set; } + } + + /// + /// Describes dependency between method input and output. + /// + /// + ///

Function Definition Table syntax:

+ /// + /// FDT ::= FDTRow [;FDTRow]* + /// FDTRow ::= Input => Output | Output <= Input + /// Input ::= ParameterName: Value [, Input]* + /// Output ::= [ParameterName: Value]* {halt|stop|void|nothing|Value} + /// Value ::= true | false | null | notnull | canbenull + /// + /// If method has single input parameter, it's name could be omitted.
+ /// Using halt (or void/nothing, which is the same) for method output + /// means that the methos doesn't return normally (throws or terminates the process).
+ /// Value canbenull is only applicable for output parameters.
+ /// You can use multiple [ContractAnnotation] for each FDT row, or use single attribute + /// with rows separated by semicolon. There is no notion of order rows, all rows are checked + /// for applicability and applied per each program state tracked by R# analysis.
+ ///
+ /// + /// + /// [ContractAnnotation("=> halt")] + /// public void TerminationMethod() + /// + /// + /// [ContractAnnotation("halt <= condition: false")] + /// public void Assert(bool condition, string text) // regular assertion method + /// + /// + /// [ContractAnnotation("s:null => true")] + /// public bool IsNullOrEmpty(string s) // string.IsNullOrEmpty() + /// + /// + /// // A method that returns null if the parameter is null, + /// // and not null if the parameter is not null + /// [ContractAnnotation("null => null; notnull => notnull")] + /// public object Transform(object data) + /// + /// + /// [ContractAnnotation("=> true, result: notnull; => false, result: null")] + /// public bool TryParse(string s, out Person result) + /// + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] + public sealed class ContractAnnotationAttribute : Attribute + { + public ContractAnnotationAttribute([NotNull] string contract) + : this(contract, false) { } + + public ContractAnnotationAttribute([NotNull] string contract, bool forceFullStates) + { + Contract = contract; + ForceFullStates = forceFullStates; + } + + [NotNull] public string Contract { get; private set; } + + public bool ForceFullStates { get; private set; } + } + + /// + /// Indicates that marked element should be localized or not. + /// + /// + /// [LocalizationRequiredAttribute(true)] + /// class Foo { + /// string str = "my string"; // Warning: Localizable string + /// } + /// + [AttributeUsage(AttributeTargets.All)] + public sealed class LocalizationRequiredAttribute : Attribute + { + public LocalizationRequiredAttribute() : this(true) { } + + public LocalizationRequiredAttribute(bool required) + { + Required = required; + } + + public bool Required { get; private set; } + } + + /// + /// Indicates that the value of the marked type (or its derivatives) + /// cannot be compared using '==' or '!=' operators and Equals() + /// should be used instead. However, using '==' or '!=' for comparison + /// with null is always permitted. + /// + /// + /// [CannotApplyEqualityOperator] + /// class NoEquality { } + /// + /// class UsesNoEquality { + /// void Test() { + /// var ca1 = new NoEquality(); + /// var ca2 = new NoEquality(); + /// if (ca1 != null) { // OK + /// bool condition = ca1 == ca2; // Warning + /// } + /// } + /// } + /// + [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Struct)] + public sealed class CannotApplyEqualityOperatorAttribute : Attribute { } + + /// + /// When applied to a target attribute, specifies a requirement for any type marked + /// with the target attribute to implement or inherit specific type or types. + /// + /// + /// [BaseTypeRequired(typeof(IComponent)] // Specify requirement + /// class ComponentAttribute : Attribute { } + /// + /// [Component] // ComponentAttribute requires implementing IComponent interface + /// class MyComponent : IComponent { } + /// + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] + [BaseTypeRequired(typeof(Attribute))] + public sealed class BaseTypeRequiredAttribute : Attribute + { + public BaseTypeRequiredAttribute([NotNull] Type baseType) + { + BaseType = baseType; + } + + [NotNull] public Type BaseType { get; private set; } + } + + /// + /// Indicates that the marked symbol is used implicitly (e.g. via reflection, in external library), + /// so this symbol will not be marked as unused (as well as by other usage inspections). + /// + [AttributeUsage(AttributeTargets.All)] + public sealed class UsedImplicitlyAttribute : Attribute + { + public UsedImplicitlyAttribute() + : this(ImplicitUseKindFlags.Default, ImplicitUseTargetFlags.Default) { } + + public UsedImplicitlyAttribute(ImplicitUseKindFlags useKindFlags) + : this(useKindFlags, ImplicitUseTargetFlags.Default) { } + + public UsedImplicitlyAttribute(ImplicitUseTargetFlags targetFlags) + : this(ImplicitUseKindFlags.Default, targetFlags) { } + + public UsedImplicitlyAttribute(ImplicitUseKindFlags useKindFlags, ImplicitUseTargetFlags targetFlags) + { + UseKindFlags = useKindFlags; + TargetFlags = targetFlags; + } + + public ImplicitUseKindFlags UseKindFlags { get; private set; } + + public ImplicitUseTargetFlags TargetFlags { get; private set; } + } + + /// + /// Should be used on attributes and causes ReSharper to not mark symbols marked with such attributes + /// as unused (as well as by other usage inspections) + /// + [AttributeUsage(AttributeTargets.Class | AttributeTargets.GenericParameter)] + public sealed class MeansImplicitUseAttribute : Attribute + { + public MeansImplicitUseAttribute() + : this(ImplicitUseKindFlags.Default, ImplicitUseTargetFlags.Default) { } + + public MeansImplicitUseAttribute(ImplicitUseKindFlags useKindFlags) + : this(useKindFlags, ImplicitUseTargetFlags.Default) { } + + public MeansImplicitUseAttribute(ImplicitUseTargetFlags targetFlags) + : this(ImplicitUseKindFlags.Default, targetFlags) { } + + public MeansImplicitUseAttribute(ImplicitUseKindFlags useKindFlags, ImplicitUseTargetFlags targetFlags) + { + UseKindFlags = useKindFlags; + TargetFlags = targetFlags; + } + + [UsedImplicitly] public ImplicitUseKindFlags UseKindFlags { get; private set; } + + [UsedImplicitly] public ImplicitUseTargetFlags TargetFlags { get; private set; } + } + + [Flags] + public enum ImplicitUseKindFlags + { + Default = Access | Assign | InstantiatedWithFixedConstructorSignature, + /// Only entity marked with attribute considered used. + Access = 1, + /// Indicates implicit assignment to a member. + Assign = 2, + /// + /// Indicates implicit instantiation of a type with fixed constructor signature. + /// That means any unused constructor parameters won't be reported as such. + /// + InstantiatedWithFixedConstructorSignature = 4, + /// Indicates implicit instantiation of a type. + InstantiatedNoFixedConstructorSignature = 8, + } + + /// + /// Specify what is considered used implicitly when marked + /// with or . + /// + [Flags] + public enum ImplicitUseTargetFlags + { + Default = Itself, + Itself = 1, + /// Members of entity marked with attribute are considered used. + Members = 2, + /// Entity marked with attribute and all its members considered used. + WithMembers = Itself | Members + } + + /// + /// This attribute is intended to mark publicly available API + /// which should not be removed and so is treated as used. + /// + [MeansImplicitUse(ImplicitUseTargetFlags.WithMembers)] + public sealed class PublicAPIAttribute : Attribute + { + public PublicAPIAttribute() { } + + public PublicAPIAttribute([NotNull] string comment) + { + Comment = comment; + } + + [CanBeNull] public string Comment { get; private set; } + } + + /// + /// Tells code analysis engine if the parameter is completely handled when the invoked method is on stack. + /// If the parameter is a delegate, indicates that delegate is executed while the method is executed. + /// If the parameter is an enumerable, indicates that it is enumerated while the method is executed. + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class InstantHandleAttribute : Attribute { } + + /// + /// Indicates that a method does not make any observable state changes. + /// The same as System.Diagnostics.Contracts.PureAttribute. + /// + /// + /// [Pure] int Multiply(int x, int y) => x * y; + /// + /// void M() { + /// Multiply(123, 42); // Waring: Return value of pure method is not used + /// } + /// + [AttributeUsage(AttributeTargets.Method)] + public sealed class PureAttribute : Attribute { } + + /// + /// Indicates that the return value of method invocation must be used. + /// + [AttributeUsage(AttributeTargets.Method)] + public sealed class MustUseReturnValueAttribute : Attribute + { + public MustUseReturnValueAttribute() { } + + public MustUseReturnValueAttribute([NotNull] string justification) + { + Justification = justification; + } + + [CanBeNull] public string Justification { get; private set; } + } + + /// + /// Indicates the type member or parameter of some type, that should be used instead of all other ways + /// to get the value that type. This annotation is useful when you have some "context" value evaluated + /// and stored somewhere, meaning that all other ways to get this value must be consolidated with existing one. + /// + /// + /// class Foo { + /// [ProvidesContext] IBarService _barService = ...; + /// + /// void ProcessNode(INode node) { + /// DoSomething(node, node.GetGlobalServices().Bar); + /// // ^ Warning: use value of '_barService' field + /// } + /// } + /// + [AttributeUsage( + AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.Method | + AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct | AttributeTargets.GenericParameter)] + public sealed class ProvidesContextAttribute : Attribute { } + + /// + /// Indicates that a parameter is a path to a file or a folder within a web project. + /// Path can be relative or absolute, starting from web root (~). + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class PathReferenceAttribute : Attribute + { + public PathReferenceAttribute() { } + + public PathReferenceAttribute([NotNull, PathReference] string basePath) + { + BasePath = basePath; + } + + [CanBeNull] public string BasePath { get; private set; } + } + + /// + /// An extension method marked with this attribute is processed by ReSharper code completion + /// as a 'Source Template'. When extension method is completed over some expression, it's source code + /// is automatically expanded like a template at call site. + /// + /// + /// Template method body can contain valid source code and/or special comments starting with '$'. + /// Text inside these comments is added as source code when the template is applied. Template parameters + /// can be used either as additional method parameters or as identifiers wrapped in two '$' signs. + /// Use the attribute to specify macros for parameters. + /// + /// + /// In this example, the 'forEach' method is a source template available over all values + /// of enumerable types, producing ordinary C# 'foreach' statement and placing caret inside block: + /// + /// [SourceTemplate] + /// public static void forEach<T>(this IEnumerable<T> xs) { + /// foreach (var x in xs) { + /// //$ $END$ + /// } + /// } + /// + /// + [AttributeUsage(AttributeTargets.Method)] + public sealed class SourceTemplateAttribute : Attribute { } + + /// + /// Allows specifying a macro for a parameter of a source template. + /// + /// + /// You can apply the attribute on the whole method or on any of its additional parameters. The macro expression + /// is defined in the property. When applied on a method, the target + /// template parameter is defined in the property. To apply the macro silently + /// for the parameter, set the property value = -1. + /// + /// + /// Applying the attribute on a source template method: + /// + /// [SourceTemplate, Macro(Target = "item", Expression = "suggestVariableName()")] + /// public static void forEach<T>(this IEnumerable<T> collection) { + /// foreach (var item in collection) { + /// //$ $END$ + /// } + /// } + /// + /// Applying the attribute on a template method parameter: + /// + /// [SourceTemplate] + /// public static void something(this Entity x, [Macro(Expression = "guid()", Editable = -1)] string newguid) { + /// /*$ var $x$Id = "$newguid$" + x.ToString(); + /// x.DoSomething($x$Id); */ + /// } + /// + /// + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method, AllowMultiple = true)] + public sealed class MacroAttribute : Attribute + { + /// + /// Allows specifying a macro that will be executed for a source template + /// parameter when the template is expanded. + /// + [CanBeNull] public string Expression { get; set; } + + /// + /// Allows specifying which occurrence of the target parameter becomes editable when the template is deployed. + /// + /// + /// If the target parameter is used several times in the template, only one occurrence becomes editable; + /// other occurrences are changed synchronously. To specify the zero-based index of the editable occurrence, + /// use values >= 0. To make the parameter non-editable when the template is expanded, use -1. + /// > + public int Editable { get; set; } + + /// + /// Identifies the target parameter of a source template if the + /// is applied on a template method. + /// + [CanBeNull] public string Target { get; set; } + } + + [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)] + public sealed class AspMvcAreaMasterLocationFormatAttribute : Attribute + { + public AspMvcAreaMasterLocationFormatAttribute([NotNull] string format) + { + Format = format; + } + + [NotNull] public string Format { get; private set; } + } + + [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)] + public sealed class AspMvcAreaPartialViewLocationFormatAttribute : Attribute + { + public AspMvcAreaPartialViewLocationFormatAttribute([NotNull] string format) + { + Format = format; + } + + [NotNull] public string Format { get; private set; } + } + + [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)] + public sealed class AspMvcAreaViewLocationFormatAttribute : Attribute + { + public AspMvcAreaViewLocationFormatAttribute([NotNull] string format) + { + Format = format; + } + + [NotNull] public string Format { get; private set; } + } + + [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)] + public sealed class AspMvcMasterLocationFormatAttribute : Attribute + { + public AspMvcMasterLocationFormatAttribute([NotNull] string format) + { + Format = format; + } + + [NotNull] public string Format { get; private set; } + } + + [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)] + public sealed class AspMvcPartialViewLocationFormatAttribute : Attribute + { + public AspMvcPartialViewLocationFormatAttribute([NotNull] string format) + { + Format = format; + } + + [NotNull] public string Format { get; private set; } + } + + [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)] + public sealed class AspMvcViewLocationFormatAttribute : Attribute + { + public AspMvcViewLocationFormatAttribute([NotNull] string format) + { + Format = format; + } + + [NotNull] public string Format { get; private set; } + } + + /// + /// ASP.NET MVC attribute. If applied to a parameter, indicates that the parameter + /// is an MVC action. If applied to a method, the MVC action name is calculated + /// implicitly from the context. Use this attribute for custom wrappers similar to + /// System.Web.Mvc.Html.ChildActionExtensions.RenderAction(HtmlHelper, String). + /// + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)] + public sealed class AspMvcActionAttribute : Attribute + { + public AspMvcActionAttribute() { } + + public AspMvcActionAttribute([NotNull] string anonymousProperty) + { + AnonymousProperty = anonymousProperty; + } + + [CanBeNull] public string AnonymousProperty { get; private set; } + } + + /// + /// ASP.NET MVC attribute. Indicates that a parameter is an MVC area. + /// Use this attribute for custom wrappers similar to + /// System.Web.Mvc.Html.ChildActionExtensions.RenderAction(HtmlHelper, String). + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class AspMvcAreaAttribute : Attribute + { + public AspMvcAreaAttribute() { } + + public AspMvcAreaAttribute([NotNull] string anonymousProperty) + { + AnonymousProperty = anonymousProperty; + } + + [CanBeNull] public string AnonymousProperty { get; private set; } + } + + /// + /// ASP.NET MVC attribute. If applied to a parameter, indicates that the parameter is + /// an MVC controller. If applied to a method, the MVC controller name is calculated + /// implicitly from the context. Use this attribute for custom wrappers similar to + /// System.Web.Mvc.Html.ChildActionExtensions.RenderAction(HtmlHelper, String, String). + /// + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)] + public sealed class AspMvcControllerAttribute : Attribute + { + public AspMvcControllerAttribute() { } + + public AspMvcControllerAttribute([NotNull] string anonymousProperty) + { + AnonymousProperty = anonymousProperty; + } + + [CanBeNull] public string AnonymousProperty { get; private set; } + } + + /// + /// ASP.NET MVC attribute. Indicates that a parameter is an MVC Master. Use this attribute + /// for custom wrappers similar to System.Web.Mvc.Controller.View(String, String). + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class AspMvcMasterAttribute : Attribute { } + + /// + /// ASP.NET MVC attribute. Indicates that a parameter is an MVC model type. Use this attribute + /// for custom wrappers similar to System.Web.Mvc.Controller.View(String, Object). + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class AspMvcModelTypeAttribute : Attribute { } + + /// + /// ASP.NET MVC attribute. If applied to a parameter, indicates that the parameter is an MVC + /// partial view. If applied to a method, the MVC partial view name is calculated implicitly + /// from the context. Use this attribute for custom wrappers similar to + /// System.Web.Mvc.Html.RenderPartialExtensions.RenderPartial(HtmlHelper, String). + /// + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)] + public sealed class AspMvcPartialViewAttribute : Attribute { } + + /// + /// ASP.NET MVC attribute. Allows disabling inspections for MVC views within a class or a method. + /// + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] + public sealed class AspMvcSuppressViewErrorAttribute : Attribute { } + + /// + /// ASP.NET MVC attribute. Indicates that a parameter is an MVC display template. + /// Use this attribute for custom wrappers similar to + /// System.Web.Mvc.Html.DisplayExtensions.DisplayForModel(HtmlHelper, String). + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class AspMvcDisplayTemplateAttribute : Attribute { } + + /// + /// ASP.NET MVC attribute. Indicates that a parameter is an MVC editor template. + /// Use this attribute for custom wrappers similar to + /// System.Web.Mvc.Html.EditorExtensions.EditorForModel(HtmlHelper, String). + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class AspMvcEditorTemplateAttribute : Attribute { } + + /// + /// ASP.NET MVC attribute. Indicates that a parameter is an MVC template. + /// Use this attribute for custom wrappers similar to + /// System.ComponentModel.DataAnnotations.UIHintAttribute(System.String). + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class AspMvcTemplateAttribute : Attribute { } + + /// + /// ASP.NET MVC attribute. If applied to a parameter, indicates that the parameter + /// is an MVC view component. If applied to a method, the MVC view name is calculated implicitly + /// from the context. Use this attribute for custom wrappers similar to + /// System.Web.Mvc.Controller.View(Object). + /// + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)] + public sealed class AspMvcViewAttribute : Attribute { } + + /// + /// ASP.NET MVC attribute. If applied to a parameter, indicates that the parameter + /// is an MVC view component name. + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class AspMvcViewComponentAttribute : Attribute { } + + /// + /// ASP.NET MVC attribute. If applied to a parameter, indicates that the parameter + /// is an MVC view component view. If applied to a method, the MVC view component view name is default. + /// + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)] + public sealed class AspMvcViewComponentViewAttribute : Attribute { } + + /// + /// ASP.NET MVC attribute. When applied to a parameter of an attribute, + /// indicates that this parameter is an MVC action name. + /// + /// + /// [ActionName("Foo")] + /// public ActionResult Login(string returnUrl) { + /// ViewBag.ReturnUrl = Url.Action("Foo"); // OK + /// return RedirectToAction("Bar"); // Error: Cannot resolve action + /// } + /// + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)] + public sealed class AspMvcActionSelectorAttribute : Attribute { } + + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Field)] + public sealed class HtmlElementAttributesAttribute : Attribute + { + public HtmlElementAttributesAttribute() { } + + public HtmlElementAttributesAttribute([NotNull] string name) + { + Name = name; + } + + [CanBeNull] public string Name { get; private set; } + } + + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property)] + public sealed class HtmlAttributeValueAttribute : Attribute + { + public HtmlAttributeValueAttribute([NotNull] string name) + { + Name = name; + } + + [NotNull] public string Name { get; private set; } + } + + /// + /// Razor attribute. Indicates that a parameter or a method is a Razor section. + /// Use this attribute for custom wrappers similar to + /// System.Web.WebPages.WebPageBase.RenderSection(String). + /// + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)] + public sealed class RazorSectionAttribute : Attribute { } + + /// + /// Indicates how method, constructor invocation or property access + /// over collection type affects content of the collection. + /// + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Property)] + public sealed class CollectionAccessAttribute : Attribute + { + public CollectionAccessAttribute(CollectionAccessType collectionAccessType) + { + CollectionAccessType = collectionAccessType; + } + + public CollectionAccessType CollectionAccessType { get; private set; } + } + + [Flags] + public enum CollectionAccessType + { + /// Method does not use or modify content of the collection. + None = 0, + /// Method only reads content of the collection but does not modify it. + Read = 1, + /// Method can change content of the collection but does not add new elements. + ModifyExistingContent = 2, + /// Method can add new elements to the collection. + UpdatedContent = ModifyExistingContent | 4 + } + + /// + /// Indicates that the marked method is assertion method, i.e. it halts control flow if + /// one of the conditions is satisfied. To set the condition, mark one of the parameters with + /// attribute. + /// + [AttributeUsage(AttributeTargets.Method)] + public sealed class AssertionMethodAttribute : Attribute { } + + /// + /// Indicates the condition parameter of the assertion method. The method itself should be + /// marked by attribute. The mandatory argument of + /// the attribute is the assertion type. + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class AssertionConditionAttribute : Attribute + { + public AssertionConditionAttribute(AssertionConditionType conditionType) + { + ConditionType = conditionType; + } + + public AssertionConditionType ConditionType { get; private set; } + } + + /// + /// Specifies assertion type. If the assertion method argument satisfies the condition, + /// then the execution continues. Otherwise, execution is assumed to be halted. + /// + public enum AssertionConditionType + { + /// Marked parameter should be evaluated to true. + IS_TRUE = 0, + /// Marked parameter should be evaluated to false. + IS_FALSE = 1, + /// Marked parameter should be evaluated to null value. + IS_NULL = 2, + /// Marked parameter should be evaluated to not null value. + IS_NOT_NULL = 3, + } + + /// + /// Indicates that the marked method unconditionally terminates control flow execution. + /// For example, it could unconditionally throw exception. + /// + [Obsolete("Use [ContractAnnotation('=> halt')] instead")] + [AttributeUsage(AttributeTargets.Method)] + public sealed class TerminatesProgramAttribute : Attribute { } + + /// + /// Indicates that method is pure LINQ method, with postponed enumeration (like Enumerable.Select, + /// .Where). This annotation allows inference of [InstantHandle] annotation for parameters + /// of delegate type by analyzing LINQ method chains. + /// + [AttributeUsage(AttributeTargets.Method)] + public sealed class LinqTunnelAttribute : Attribute { } + + /// + /// Indicates that IEnumerable, passed as parameter, is not enumerated. + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class NoEnumerationAttribute : Attribute { } + + /// + /// Indicates that parameter is regular expression pattern. + /// + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class RegexPatternAttribute : Attribute { } + + /// + /// Prevents the Member Reordering feature from tossing members of the marked class. + /// + /// + /// The attribute must be mentioned in your member reordering patterns + /// + [AttributeUsage( + AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct | AttributeTargets.Enum)] + public sealed class NoReorderAttribute : Attribute { } + + /// + /// XAML attribute. Indicates the type that has ItemsSource property and should be treated + /// as ItemsControl-derived type, to enable inner items DataContext type resolve. + /// + [AttributeUsage(AttributeTargets.Class)] + public sealed class XamlItemsControlAttribute : Attribute { } + + /// + /// XAML attribute. Indicates the property of some BindingBase-derived type, that + /// is used to bind some item of ItemsControl-derived type. This annotation will + /// enable the DataContext type resolve for XAML bindings for such properties. + /// + /// + /// Property should have the tree ancestor of the ItemsControl type or + /// marked with the attribute. + /// + [AttributeUsage(AttributeTargets.Property)] + public sealed class XamlItemBindingOfItemsControlAttribute : Attribute { } + + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] + public sealed class AspChildControlTypeAttribute : Attribute + { + public AspChildControlTypeAttribute([NotNull] string tagName, [NotNull] Type controlType) + { + TagName = tagName; + ControlType = controlType; + } + + [NotNull] public string TagName { get; private set; } + + [NotNull] public Type ControlType { get; private set; } + } + + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method)] + public sealed class AspDataFieldAttribute : Attribute { } + + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method)] + public sealed class AspDataFieldsAttribute : Attribute { } + + [AttributeUsage(AttributeTargets.Property)] + public sealed class AspMethodPropertyAttribute : Attribute { } + + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] + public sealed class AspRequiredAttributeAttribute : Attribute + { + public AspRequiredAttributeAttribute([NotNull] string attribute) + { + Attribute = attribute; + } + + [NotNull] public string Attribute { get; private set; } + } + + [AttributeUsage(AttributeTargets.Property)] + public sealed class AspTypePropertyAttribute : Attribute + { + public bool CreateConstructorReferences { get; private set; } + + public AspTypePropertyAttribute(bool createConstructorReferences) + { + CreateConstructorReferences = createConstructorReferences; + } + } + + [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] + public sealed class RazorImportNamespaceAttribute : Attribute + { + public RazorImportNamespaceAttribute([NotNull] string name) + { + Name = name; + } + + [NotNull] public string Name { get; private set; } + } + + [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] + public sealed class RazorInjectionAttribute : Attribute + { + public RazorInjectionAttribute([NotNull] string type, [NotNull] string fieldName) + { + Type = type; + FieldName = fieldName; + } + + [NotNull] public string Type { get; private set; } + + [NotNull] public string FieldName { get; private set; } + } + + [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] + public sealed class RazorDirectiveAttribute : Attribute + { + public RazorDirectiveAttribute([NotNull] string directive) + { + Directive = directive; + } + + [NotNull] public string Directive { get; private set; } + } + + [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] + public sealed class RazorPageBaseTypeAttribute : Attribute + { + public RazorPageBaseTypeAttribute([NotNull] string baseType) + { + BaseType = baseType; + } + public RazorPageBaseTypeAttribute([NotNull] string baseType, string pageName) + { + BaseType = baseType; + PageName = pageName; + } + + [NotNull] public string BaseType { get; private set; } + [CanBeNull] public string PageName { get; private set; } + } + + [AttributeUsage(AttributeTargets.Method)] + public sealed class RazorHelperCommonAttribute : Attribute { } + + [AttributeUsage(AttributeTargets.Property)] + public sealed class RazorLayoutAttribute : Attribute { } + + [AttributeUsage(AttributeTargets.Method)] + public sealed class RazorWriteLiteralMethodAttribute : Attribute { } + + [AttributeUsage(AttributeTargets.Method)] + public sealed class RazorWriteMethodAttribute : Attribute { } + + [AttributeUsage(AttributeTargets.Parameter)] + public sealed class RazorWriteMethodParameterAttribute : Attribute { } +} \ No newline at end of file diff --git a/UCR.Core/UCR.Core.csproj b/UCR.Core/UCR.Core.csproj index 0c687e08..c8f39a93 100644 --- a/UCR.Core/UCR.Core.csproj +++ b/UCR.Core/UCR.Core.csproj @@ -84,7 +84,11 @@ + + + + diff --git a/UCR.Core/Utilities/AxisHelpers/CircularDeadZoneHelper.cs b/UCR.Core/Utilities/AxisHelpers/CircularDeadZoneHelper.cs new file mode 100644 index 00000000..2ce165e0 --- /dev/null +++ b/UCR.Core/Utilities/AxisHelpers/CircularDeadZoneHelper.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HidWizards.UCR.Core.Utilities.AxisHelpers +{ + public class CircularDeadZoneHelper + { + private double _scaleFactor; + private double _deadzoneRadius; + + public int Percentage + { + get => _percentage; + set + //TODO CHECK FOR NEG + { + _percentage = value; + PrecalculateValues(); + } + } + private int _percentage; + + public CircularDeadZoneHelper() + { + PrecalculateValues(); + } + + private void PrecalculateValues() + { + if (_percentage == 0) + { + _deadzoneRadius = 0; + _scaleFactor = 1; + } + else + { + const double max = Constants.AxisMaxValue; + _deadzoneRadius = (_percentage / 100d) * max; + _scaleFactor = max / (max - _deadzoneRadius); + } + } + + public long[] ApplyRangeDeadZone(long[] values) + { + var x = values[0]; + var y = values[1]; + + var inputRadius = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)); + var inputAngle = Math.Atan2(x, y); + if (inputRadius < _deadzoneRadius) + { + return new long[] { 0, 0 }; + } + var adjustedRadius = inputRadius - _deadzoneRadius; + var outputRadius = adjustedRadius * _scaleFactor; + + var outX = (long)(outputRadius * Math.Sin(inputAngle)); + var outY = (long)(outputRadius * Math.Cos(inputAngle)); + if (outX == -32769) outX = -32768; + if (outY == -32769) outY = -32768; + + var output = new[] { outX, outY }; + return output; + + } + } +} diff --git a/UCR.Core/Utilities/AxisHelpers/DeadZoneHelper.cs b/UCR.Core/Utilities/AxisHelpers/DeadZoneHelper.cs new file mode 100644 index 00000000..6b5ca848 --- /dev/null +++ b/UCR.Core/Utilities/AxisHelpers/DeadZoneHelper.cs @@ -0,0 +1,62 @@ +using System; +using System.Diagnostics; + +namespace HidWizards.UCR.Core.Utilities.AxisHelpers +{ + public class DeadZoneHelper + { + //private double gapPercent; + private double _scaleFactor; + private double _deadzoneCutoff; + + public int Percentage + { + get => _percentage; + set + { + if (value < 0 || value > 100) + { + throw new ArgumentOutOfRangeException(); + } + _percentage = value; + PrecalculateValues(); + } + } + private int _percentage; + + public DeadZoneHelper() + { + PrecalculateValues(); + } + + private void PrecalculateValues() + { + if (_percentage == 0) + { + _deadzoneCutoff = 0; + _scaleFactor = 1.0; + } + else + { + _deadzoneCutoff = (Constants.AxisMaxValue - (Constants.AxisMaxValue * (_percentage / 100.0))); + _scaleFactor = Math.Round(Constants.AxisMaxValue / _deadzoneCutoff); + } + } + + public long ApplyRangeDeadZone(long value) + { + var absValue = Math.Abs(value); + if (absValue < Math.Round(_deadzoneCutoff)) + { + return 0; + } + + var sign = Math.Sign(value); + var adjustedValue = (absValue - _deadzoneCutoff) * _scaleFactor; + var newValue = (long) Math.Round(adjustedValue * sign); + if (newValue == -32769) newValue = -32768; + //Debug.WriteLine($"Pre-DZ: {value}, Post-DZ: {newValue}, Cutoff: {_deadzoneCutoff}"); + return newValue; + } + } +} diff --git a/UCR.Core/Utilities/AxisHelpers/SensitivityHelper.cs b/UCR.Core/Utilities/AxisHelpers/SensitivityHelper.cs new file mode 100644 index 00000000..5407b57f --- /dev/null +++ b/UCR.Core/Utilities/AxisHelpers/SensitivityHelper.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HidWizards.UCR.Core.Utilities.AxisHelpers +{ + public class SensitivityHelper + { + private double _scaleFactor; + private double _axisRange; + private double _sens; + + public int Percentage + { + get => _percentage; + set + { + // Do NOT throw if percentage is not in range 0..100, other values are valid! + _percentage = value; + PrecalculateValues(); + } + } + + private int _percentage; + + public bool IsLinear { get; set; } + + public SensitivityHelper() + { + PrecalculateValues(); + } + + private void PrecalculateValues() + { + _scaleFactor = _percentage / 100d; + _axisRange = Constants.AxisMaxValue - Constants.AxisMinValue; + _sens = _scaleFactor / 100d; + } + + public long ApplyRangeSensitivity(long value) + { + //var sensitivityPercent = (sensitivity / 100.0); + if (IsLinear) return Functions.ClampAxisRange((long)Math.Round(value * _scaleFactor)); + + //var sens = _scaleFactor / 100d; + //double AxisRange = 1d * (Constants.AxisMaxValue - Constants.AxisMinValue); + // Map value to -1 .. 1 + double val11 = (((value - Constants.AxisMinValue) / _axisRange) * 2d) - 1d; + // calculate (Sensitivity * Value) + ( (1-Sensitivity) * Value^3 ) + double valout = (_sens * val11) + ((1d - _sens) * Math.Pow(val11, 3d)); + // Map value back to AxisRange + value = (long)Math.Round(((valout + 1d) / 2d) * _axisRange + (1d * Constants.AxisMinValue)); + + return value; + } + + } +} diff --git a/UCR.Core/Utilities/Functions.cs b/UCR.Core/Utilities/Functions.cs index 222e3623..32928369 100644 --- a/UCR.Core/Utilities/Functions.cs +++ b/UCR.Core/Utilities/Functions.cs @@ -1,40 +1,75 @@ -using System; +using System; +using System.Diagnostics; namespace HidWizards.UCR.Core.Utilities { public static class Functions { - public static long ApplyRangeDeadZone(long value, int deadZonePercentage) + /// + /// Inverts an axis. + /// Given Max or Min values, will return the opposite extreme. + /// Else returns value * -1 + /// + /// The raw value of the axis + /// The inverted value of the axis + public static long Invert(long value) { - var gap = (deadZonePercentage / 100.0) * Constants.AxisMaxValue; - var remainder = Constants.AxisMaxValue - gap; - var gapPercent = Math.Max(0, Math.Abs(value) - gap) / remainder; - return (long)(gapPercent * Constants.AxisMaxValue * Math.Sign(value)); + if (value == 0) return 0; + if (value >= Constants.AxisMaxValue) + { + return Constants.AxisMinValue; + } + + if (value <= Constants.AxisMinValue) + { + return Constants.AxisMaxValue; + } + + return value * -1; } - public static long ApplyRangeSensitivity(long value, int sensitivity, bool linear) + /// + /// Ensures that an axis value is within permitted range + /// + /// The raw axis value + /// The clamped axis value + public static long ClampAxisRange(long value) { - var sensitivityPercent = (sensitivity / 100.0); - if (linear) return (long)(value * sensitivityPercent); - // TODO https://github.com/evilC/UCR/blob/master/Libraries/StickOps/StickOps.ahk#L60 - return value; + if (value == 0) return value; + if (value <= Constants.AxisMinValue) return Constants.AxisMinValue; + return value >= Constants.AxisMaxValue ? Constants.AxisMaxValue : value; } - public static long HalfAxisToFullRange(long axis, bool positiveRange, bool invert) + /// + /// Returns either the low or high half of the axis. + /// Stretches the half axis returned to fill the full scale + /// + /// The value of the axis + /// Set to true for the high half, else the low half + /// The new value for the split axis. If axis is negative and high is specified, returns 0. If axis is positive and low is specified, returns 0 + public static long SplitAxis(long axis, bool positiveRange) { long value; + if (axis == 0) return Constants.AxisMinValue; if (positiveRange) { - value = axis > 0L ? axis : 0L; + if (axis < 0) return Constants.AxisMinValue; + value = axis; + if (value == Constants.AxisMaxValue) value++; } else { - value = axis < 0 ? axis * -1 : 0L; + if (axis > 0) return Constants.AxisMinValue; + value = axis * -1; } - - value = Constants.AxisMinValue + value * 2; - - return invert ? value * -1 : value; + + value *= 2; + value += Constants.AxisMinValue; + + if (value == 32768) value = 32767; + + return value; } + } } diff --git a/UCR.Plugins/Remapper/AxesToAxes.cs b/UCR.Plugins/Remapper/AxesToAxes.cs new file mode 100644 index 00000000..34e0cfbf --- /dev/null +++ b/UCR.Plugins/Remapper/AxesToAxes.cs @@ -0,0 +1,109 @@ +using System; +using HidWizards.UCR.Core.Attributes; +using HidWizards.UCR.Core.Models; +using HidWizards.UCR.Core.Models.Binding; +using HidWizards.UCR.Core.Utilities; +using HidWizards.UCR.Core.Utilities.AxisHelpers; + +namespace HidWizards.UCR.Plugins.Remapper +{ + [Plugin("Axes to Axes")] + [PluginInput(DeviceBindingCategory.Range, "X Axis")] + [PluginInput(DeviceBindingCategory.Range, "Y Axis")] + [PluginOutput(DeviceBindingCategory.Range, "X Axis")] + [PluginOutput(DeviceBindingCategory.Range, "Y Axis")] + public class AxesToAxes : Plugin + { + private readonly CircularDeadZoneHelper _circularDeadZoneHelper = new CircularDeadZoneHelper(); + private readonly DeadZoneHelper _deadZoneHelper = new DeadZoneHelper(); + private readonly SensitivityHelper _sensitivityHelper = new SensitivityHelper(); + private double _linearSenstitivityScaleFactor; + + [PluginGui("Invert X", ColumnOrder = 0)] + public bool InvertX { get; set; } + + [PluginGui("Invert Y", ColumnOrder = 1)] + public bool InvertY { get; set; } + + [PluginGui("Sensitivity", ColumnOrder = 2)] + public int Sensitivity { get; set; } + + [PluginGui("Linear", RowOrder = 0, ColumnOrder = 2)] + public bool Linear { get; set; } + + [PluginGui("Dead zone", RowOrder = 1, ColumnOrder = 0)] + public int DeadZone { get; set; } + + [PluginGui("Circular", RowOrder = 1, ColumnOrder = 2)] + public bool CircularDz { get; set; } + + + public AxesToAxes() + { + DeadZone = 0; + Sensitivity = 100; + } + + private void Initialize() + { + _deadZoneHelper.Percentage = DeadZone; + _circularDeadZoneHelper.Percentage = DeadZone; + _sensitivityHelper.Percentage = Sensitivity; + _linearSenstitivityScaleFactor = ((double)Sensitivity / 100); + } + + public override void Update(params long[] values) + { + var outputValues = new long[] {values[0], values[1]}; + if (DeadZone != 0) + { + if (CircularDz) + { + outputValues = _circularDeadZoneHelper.ApplyRangeDeadZone(outputValues); + } + else + { + outputValues[0] = _deadZoneHelper.ApplyRangeDeadZone(outputValues[0]); + outputValues[1] = _deadZoneHelper.ApplyRangeDeadZone(outputValues[1]); + } + + } + if (Sensitivity != 100) + { + if (Linear) + { + outputValues[0] = (long)(outputValues[0] * _linearSenstitivityScaleFactor); + outputValues[1] = (long)(outputValues[1] * _linearSenstitivityScaleFactor); + } + else + { + outputValues[0] = _sensitivityHelper.ApplyRangeSensitivity(outputValues[0]); + outputValues[1] = _sensitivityHelper.ApplyRangeSensitivity(outputValues[1]); + } + } + + outputValues[0] = Functions.ClampAxisRange(outputValues[0]); + outputValues[1] = Functions.ClampAxisRange(outputValues[1]); + + if (InvertX) outputValues[0] = Functions.Invert(outputValues[0]); + if (InvertY) outputValues[1] = Functions.Invert(outputValues[1]); + + WriteOutput(0, outputValues[0]); + WriteOutput(1, outputValues[1]); + } + + #region Event Handling + public override void OnActivate() + { + base.OnActivate(); + Initialize(); + } + + public override void OnPropertyChanged() + { + base.OnPropertyChanged(); + Initialize(); + } + #endregion + } +} diff --git a/UCR.Plugins/Remapper/AxisMerger.cs b/UCR.Plugins/Remapper/AxisMerger.cs index 33311c89..0f64744f 100644 --- a/UCR.Plugins/Remapper/AxisMerger.cs +++ b/UCR.Plugins/Remapper/AxisMerger.cs @@ -3,10 +3,11 @@ using HidWizards.UCR.Core.Models; using HidWizards.UCR.Core.Models.Binding; using HidWizards.UCR.Core.Utilities; +using HidWizards.UCR.Core.Utilities.AxisHelpers; namespace HidWizards.UCR.Plugins.Remapper { - [Plugin("Axis merger")] + [Plugin("Axis Merger")] [PluginInput(DeviceBindingCategory.Range, "Axis high")] [PluginInput(DeviceBindingCategory.Range, "Axis low")] [PluginOutput(DeviceBindingCategory.Range, "Axis")] @@ -24,6 +25,8 @@ public class AxisMerger : Plugin [PluginGui("Invert low", RowOrder = 2)] public bool InvertLow { get; set; } + private readonly DeadZoneHelper _deadZoneHelper = new DeadZoneHelper(); + public AxisMerger() { DeadZone = 0; @@ -35,8 +38,8 @@ public override void Update(params long[] values) var valueLow = values[1]; long valueOutput; - if (InvertHigh) valueHigh *= -1; - if (InvertLow) valueLow *= -1; + if (InvertHigh) valueHigh = Functions.Invert(valueHigh); + if (InvertLow) valueLow = Functions.Invert(valueLow); switch (Mode) { @@ -56,16 +59,36 @@ public override void Update(params long[] values) if (DeadZone != 0) { - valueOutput = Functions.ApplyRangeDeadZone(valueOutput, DeadZone); + valueOutput = _deadZoneHelper.ApplyRangeDeadZone(valueOutput); } WriteOutput(0, valueOutput); } + private void Initialize() + { + _deadZoneHelper.Percentage = DeadZone; + } + + public enum AxisMergerMode { Average, Greatest, Sum } + + #region Event Handling + public override void OnActivate() + { + base.OnActivate(); + Initialize(); + } + + public override void OnPropertyChanged() + { + base.OnPropertyChanged(); + Initialize(); + } + #endregion } } diff --git a/UCR.Plugins/Remapper/AxisSplitter.cs b/UCR.Plugins/Remapper/AxisSplitter.cs index c317a5f0..bde30937 100644 --- a/UCR.Plugins/Remapper/AxisSplitter.cs +++ b/UCR.Plugins/Remapper/AxisSplitter.cs @@ -2,10 +2,11 @@ using HidWizards.UCR.Core.Models; using HidWizards.UCR.Core.Models.Binding; using HidWizards.UCR.Core.Utilities; +using HidWizards.UCR.Core.Utilities.AxisHelpers; namespace HidWizards.UCR.Plugins.Remapper { - [Plugin("Axis splitter")] + [Plugin("Axis Splitter")] [PluginInput(DeviceBindingCategory.Range, "Axis")] [PluginOutput(DeviceBindingCategory.Range, "Axis high")] [PluginOutput(DeviceBindingCategory.Range, "Axis low")] @@ -20,6 +21,8 @@ public class AxisSplitter : Plugin [PluginGui("Dead zone")] public int DeadZone { get; set; } + private readonly DeadZoneHelper _deadZoneHelper = new DeadZoneHelper(); + public AxisSplitter() { DeadZone = 0; @@ -29,9 +32,33 @@ public override void Update(params long[] values) { var value = values[0]; - if (DeadZone != 0) value = Functions.ApplyRangeDeadZone(value, DeadZone); - WriteOutput(0, Functions.HalfAxisToFullRange(value, true, InvertHigh)); - WriteOutput(1, Functions.HalfAxisToFullRange(value, false, InvertLow)); + if (DeadZone != 0) value = _deadZoneHelper.ApplyRangeDeadZone(value); + + var high = Functions.SplitAxis(value, true); + var low = Functions.SplitAxis(value, false); + if (InvertHigh) high = Functions.Invert(high); + if (InvertLow) low = Functions.Invert(low); + WriteOutput(0, high); + WriteOutput(1, low); + } + + private void Initialize() + { + _deadZoneHelper.Percentage = DeadZone; + } + + #region Event Handling + public override void OnActivate() + { + base.OnActivate(); + Initialize(); + } + + public override void OnPropertyChanged() + { + base.OnPropertyChanged(); + Initialize(); } + #endregion } } diff --git a/UCR.Plugins/Remapper/AxisToAxis.cs b/UCR.Plugins/Remapper/AxisToAxis.cs index 526c1f22..e266aa05 100644 --- a/UCR.Plugins/Remapper/AxisToAxis.cs +++ b/UCR.Plugins/Remapper/AxisToAxis.cs @@ -3,10 +3,11 @@ using HidWizards.UCR.Core.Models; using HidWizards.UCR.Core.Models.Binding; using HidWizards.UCR.Core.Utilities; +using HidWizards.UCR.Core.Utilities.AxisHelpers; namespace HidWizards.UCR.Plugins.Remapper { - [Plugin("Axis to axis")] + [Plugin("Axis to Axis")] [PluginInput(DeviceBindingCategory.Range, "Axis")] [PluginOutput(DeviceBindingCategory.Range, "Axis")] public class AxisToAxis : Plugin @@ -23,6 +24,9 @@ public class AxisToAxis : Plugin [PluginGui("Sensitivity", ColumnOrder = 2)] public int Sensitivity { get; set; } + private readonly DeadZoneHelper _deadZoneHelper = new DeadZoneHelper(); + private readonly SensitivityHelper _sensitivityHelper = new SensitivityHelper(); + public AxisToAxis() { DeadZone = 0; @@ -32,11 +36,31 @@ public AxisToAxis() public override void Update(params long[] values) { var value = values[0]; - if (Invert) value *= -1; - if (DeadZone != 0) value = Functions.ApplyRangeDeadZone(value, DeadZone); - if (Sensitivity != 100) value = Functions.ApplyRangeSensitivity(value, Sensitivity, Linear); - value = Math.Min(Math.Max(value, Constants.AxisMinValue), Constants.AxisMaxValue); + if (Invert) value = Functions.Invert(value); + if (DeadZone != 0) value = _deadZoneHelper.ApplyRangeDeadZone(value); + if (Sensitivity != 100) value = _sensitivityHelper.ApplyRangeSensitivity(value); WriteOutput(0, value); } + + private void Initialize() + { + _deadZoneHelper.Percentage = DeadZone; + _sensitivityHelper.Percentage = Sensitivity; + _sensitivityHelper.IsLinear = Linear; + } + + #region Event Handling + public override void OnActivate() + { + base.OnActivate(); + Initialize(); + } + + public override void OnPropertyChanged() + { + base.OnPropertyChanged(); + Initialize(); + } + #endregion } } diff --git a/UCR.Plugins/Remapper/AxisToAxisCumulative.cs b/UCR.Plugins/Remapper/AxisToAxisCumulative.cs new file mode 100644 index 00000000..d779b770 --- /dev/null +++ b/UCR.Plugins/Remapper/AxisToAxisCumulative.cs @@ -0,0 +1,147 @@ +using System; +using System.Diagnostics; +using System.Threading; +using HidWizards.UCR.Core.Attributes; +using HidWizards.UCR.Core.Models; +using HidWizards.UCR.Core.Models.Binding; +using HidWizards.UCR.Core.Utilities; +using HidWizards.UCR.Core.Utilities.AxisHelpers; + +namespace HidWizards.UCR.Plugins.Remapper +{ + [Plugin("Axis to Axis (Cumulative)")] + [PluginInput(DeviceBindingCategory.Range, "Axis")] + [PluginOutput(DeviceBindingCategory.Range, "Axis")] + public class AxisToAxisCumulative : Plugin + { + [PluginGui("Invert", ColumnOrder = 0)] + public bool Invert { get; set; } + + [PluginGui("Linear", ColumnOrder = 3)] + public bool Linear { get; set; } + + [PluginGui("Dead zone", ColumnOrder = 1)] + public int DeadZone { get; set; } + + [PluginGui("Sensitivity", ColumnOrder = 2)] + public int Sensitivity { get; set; } + + /// + /// To constantly add current axis values to the output - WORK IN PROGRESS!!! + /// + [PluginGui("Relative Continue", ColumnOrder = 1, RowOrder = 2)] + public bool RelativeContinue { get; set; } + + [PluginGui("Relative Sensitivity", ColumnOrder = 2, RowOrder = 2)] + public decimal RelativeSensitivity { get; set; } + + private readonly DeadZoneHelper _deadZoneHelper = new DeadZoneHelper(); + private readonly SensitivityHelper _sensitivityHelper = new SensitivityHelper(); + + private long _currentOutputValue; + private long _currentInputValue; + private readonly object _threadLock = new object(); + + private Thread _relativeThread; + + public AxisToAxisCumulative() + { + DeadZone = 0; + Sensitivity = 100; + RelativeContinue = true; + RelativeSensitivity = 2; + _relativeThread = new Thread(RelativeThread); + } + + public override void Update(params long[] values) + { + var value = values[0]; + + if (Invert) value = Functions.Invert(value); + if (DeadZone != 0) value = _deadZoneHelper.ApplyRangeDeadZone(value); + if (Sensitivity != 100) value = _sensitivityHelper.ApplyRangeSensitivity(value); + + // Respect the axis min and max ranges. + value = Math.Min(Math.Max(value, Constants.AxisMinValue), Constants.AxisMaxValue); + _currentInputValue = value; + + if (RelativeContinue) + { + SetRelativeThreadState(value != 0); + } + else + { + RelativeUpdate(); + } + } + + private void Initialize() + { + _deadZoneHelper.Percentage = DeadZone; + _sensitivityHelper.Percentage = Sensitivity; + _sensitivityHelper.IsLinear = Linear; + } + + #region Event Handling + public override void OnActivate() + { + base.OnActivate(); + Initialize(); + } + + public override void OnPropertyChanged() + { + base.OnPropertyChanged(); + Initialize(); + } + + public override void OnDeactivate() + { + SetRelativeThreadState(false); + } + #endregion + + private void SetRelativeThreadState(bool state) + { + lock (_threadLock) + { + var relativeThreadActive = RelativeThreadActive(); + if (!relativeThreadActive && state) + { + _relativeThread = new Thread(RelativeThread); + _relativeThread.Start(); + Debug.WriteLine("UCR| Started Relative Thread"); + } + else if (relativeThreadActive && !state) + { + _relativeThread.Abort(); + _relativeThread.Join(); + _relativeThread = null; + Debug.WriteLine("UCR| Stopped Relative Thread"); + } + } + } + + private bool RelativeThreadActive() + { + return _relativeThread != null && _relativeThread.IsAlive; + } + + public void RelativeThread() + { + while (RelativeContinue) + { + RelativeUpdate(); + Thread.Sleep(10); + } + } + + private void RelativeUpdate() + { + var value = (long)((_currentInputValue * (RelativeSensitivity / 100)) + _currentOutputValue); + value = Math.Min(Math.Max(value, Constants.AxisMinValue), Constants.AxisMaxValue); + WriteOutput(0, value); + _currentOutputValue = value; + } + } +} \ No newline at end of file diff --git a/UCR.Plugins/Remapper/AxisToButton.cs b/UCR.Plugins/Remapper/AxisToButton.cs index 04566b6d..df4c0972 100644 --- a/UCR.Plugins/Remapper/AxisToButton.cs +++ b/UCR.Plugins/Remapper/AxisToButton.cs @@ -3,10 +3,11 @@ using HidWizards.UCR.Core.Models; using HidWizards.UCR.Core.Models.Binding; using HidWizards.UCR.Core.Utilities; +using HidWizards.UCR.Core.Utilities.AxisHelpers; namespace HidWizards.UCR.Plugins.Remapper { - [Plugin("Axis to button")] + [Plugin("Axis to Button")] [PluginInput(DeviceBindingCategory.Range, "Axis")] [PluginOutput(DeviceBindingCategory.Momentary, "Button high")] [PluginOutput(DeviceBindingCategory.Momentary, "Button low")] @@ -18,6 +19,8 @@ public class AxisToButton : Plugin [PluginGui("Dead zone", ColumnOrder = 1)] public int DeadZone { get; set; } + private readonly DeadZoneHelper _deadZoneHelper = new DeadZoneHelper(); + public AxisToButton() { DeadZone = 30; @@ -26,8 +29,8 @@ public AxisToButton() public override void Update(params long[] values) { var value = values[0]; - if (Invert) value *= -1; - value = Math.Sign(Functions.ApplyRangeDeadZone(value,DeadZone)); + if (Invert) value = Functions.Invert(value); + value = Math.Sign(_deadZoneHelper.ApplyRangeDeadZone(value)); switch (value) { case 0: @@ -44,5 +47,25 @@ public override void Update(params long[] values) break; } } + + private void Initialize() + { + _deadZoneHelper.Percentage = DeadZone; + } + + #region Event Handling + public override void OnActivate() + { + base.OnActivate(); + Initialize(); + } + + public override void OnPropertyChanged() + { + base.OnPropertyChanged(); + Initialize(); + } + #endregion + } } diff --git a/UCR.Plugins/Remapper/AxisToDelta.cs b/UCR.Plugins/Remapper/AxisToDelta.cs new file mode 100644 index 00000000..8c8481ba --- /dev/null +++ b/UCR.Plugins/Remapper/AxisToDelta.cs @@ -0,0 +1,125 @@ +using System; +using System.Diagnostics; +using System.Timers; +using HidWizards.UCR.Core.Attributes; +using HidWizards.UCR.Core.Models; +using HidWizards.UCR.Core.Models.Binding; +using HidWizards.UCR.Core.Utilities; +using HidWizards.UCR.Core.Utilities.AxisHelpers; + +namespace HidWizards.UCR.Plugins.Remapper +{ + [Plugin("Axis to Delta")] + [PluginInput(DeviceBindingCategory.Range, "Axis")] + [PluginOutput(DeviceBindingCategory.Delta, "Delta")] + public class AxisToDelta : Plugin + { + [PluginGui("Invert", RowOrder = 0, ColumnOrder = 0)] + public bool Invert { get; set; } + + [PluginGui("Dead zone", RowOrder = 0, ColumnOrder = 1)] + public int DeadZone { get; set; } + + [PluginGui("Sensitivity", RowOrder = 0, ColumnOrder = 2)] + public int Sensitivity { get; set; } + + [PluginGui("Min", RowOrder = 1, ColumnOrder = 0)] + public int Min { get; set; } + + [PluginGui("Max", RowOrder = 1, ColumnOrder = 1)] + public int Max { get; set; } + + private static Timer _absoluteModeTimer; + private long _currentDelta; + private float _scaleFactor; + private readonly DeadZoneHelper _deadZoneHelper = new DeadZoneHelper(); + private readonly SensitivityHelper _sensitivityHelper = new SensitivityHelper(); + + public AxisToDelta() + { + DeadZone = 0; + Sensitivity = 1; + Min = 1; + Max = 20; + _absoluteModeTimer = new Timer(10); + _absoluteModeTimer.Elapsed += AbsoluteModeTimerElapsed; + } + + #region Input Processing + public override void Update(params long[] values) + { + var value = values[0]; + if (value != 0) value = _deadZoneHelper.ApplyRangeDeadZone(value); + if (Invert) value = Functions.Invert(value); + if (Sensitivity != 100) value = _sensitivityHelper.ApplyRangeSensitivity(value); + + if (value == 0) + { + SetAbsoluteTimerState(false); + _currentDelta = 0; + } + else + { + var sign = Math.Sign(value); + + value = Functions.ClampAxisRange(value); + _currentDelta = (long)(Min + (Math.Abs(value) * _scaleFactor)) * sign; + //Debug.WriteLine($"New Delta: {_currentDelta}"); + SetAbsoluteTimerState(true); + } + } + + public void SetAbsoluteTimerState(bool state) + { + if (state && !_absoluteModeTimer.Enabled) + { + _absoluteModeTimer.Start(); + } + else if (!state && _absoluteModeTimer.Enabled) + { + _absoluteModeTimer.Stop(); + } + } + + private void AbsoluteModeTimerElapsed(object sender, ElapsedEventArgs e) + { + WriteOutput(0, _currentDelta); + } + #endregion + + #region Settings configuration + + private void Initialize() + { + _scaleFactor = (float)(Max - (Min - 1)) / 32769; + _deadZoneHelper.Percentage = DeadZone; + _sensitivityHelper.Percentage = Sensitivity; + } + + #endregion + + #region Event Handling + public override void OnActivate() + { + base.OnActivate(); + Initialize(); + if (_currentDelta != 0) + { + SetAbsoluteTimerState(true); + } + } + + public override void OnDeactivate() + { + base.OnDeactivate(); + SetAbsoluteTimerState(false); + } + + public override void OnPropertyChanged() + { + base.OnPropertyChanged(); + Initialize(); + } + #endregion + } +} diff --git a/UCR.Plugins/Remapper/AxisToMouse.cs b/UCR.Plugins/Remapper/AxisToMouse.cs deleted file mode 100644 index 8171a909..00000000 --- a/UCR.Plugins/Remapper/AxisToMouse.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using HidWizards.UCR.Core.Attributes; -using HidWizards.UCR.Core.Models; -using HidWizards.UCR.Core.Models.Binding; -using HidWizards.UCR.Core.Utilities; - -namespace HidWizards.UCR.Plugins.Remapper -{ - [Plugin("Axis to mouse", Disabled = true)] - [PluginInput(DeviceBindingCategory.Range, "Axis")] - [PluginOutput(DeviceBindingCategory.Delta, "Mouse")] - public class AxisToMouse : Plugin - { - [PluginGui("Invert", ColumnOrder = 0)] - public bool Invert { get; set; } - - [PluginGui("Dead zone", ColumnOrder = 1)] - public int DeadZone { get; set; } - - [PluginGui("Sensitivity", ColumnOrder = 2)] - public int Sensitivity { get; set; } - - public AxisToMouse() - { - DeadZone = 0; - Sensitivity = 1; - } - - public override void Update(params long[] values) - { - var value = values[0]; - if (Invert) value *= -1; - if (DeadZone != 0) value = Functions.ApplyRangeDeadZone(value, DeadZone); - if (Sensitivity != 100) value = Functions.ApplyRangeSensitivity(value, Sensitivity, false); - value = Math.Min(Math.Max(value, Constants.AxisMinValue), Constants.AxisMaxValue); - WriteOutput(0, value); - } - } -} diff --git a/UCR.Plugins/Remapper/ButtonToAxis.cs b/UCR.Plugins/Remapper/ButtonToAxis.cs index 88f78412..b8075561 100644 --- a/UCR.Plugins/Remapper/ButtonToAxis.cs +++ b/UCR.Plugins/Remapper/ButtonToAxis.cs @@ -5,7 +5,7 @@ namespace HidWizards.UCR.Plugins.Remapper { - [Plugin("Button to axis")] + [Plugin("Button to Axis")] [PluginInput(DeviceBindingCategory.Momentary, "Button")] [PluginOutput(DeviceBindingCategory.Range, "Axis")] public class ButtonToAxis : Plugin @@ -13,9 +13,13 @@ public class ButtonToAxis : Plugin [PluginGui("Invert", ColumnOrder = 0)] public bool Invert { get; set; } - [PluginGui("Range target", ColumnOrder = 1)] + [PluginGui("Absolute", ColumnOrder = 1)] + public bool Absolute { get; set; } + + [PluginGui("Range target", ColumnOrder = 2)] public int Range { get; set; } + public ButtonToAxis() { Range = 100; @@ -23,8 +27,21 @@ public ButtonToAxis() public override void Update(params long[] values) { - if (Invert) values[0] = values[0] * - 1; - WriteOutput(0, values[0] * (long)(Constants.AxisMaxValue * ( Range / 100.0 ))); + var value = values[0]; + + // ToDo: Review logic, move off into Utilities and unit test + if (Absolute) + { + value = (long)(Constants.AxisMinValue + value * Constants.AxisMaxValue * 2 * (Range / 100.0)); + if (Invert) value = Functions.Invert(value); + WriteOutput(0, value); + } + else + { + var inverse = value == 0 ^ Invert; + WriteOutput(0, value * (long)((inverse ? Constants.AxisMinValue : Constants.AxisMaxValue) * (Range / 100.0))); + } } + } } diff --git a/UCR.Plugins/Remapper/ButtonToButton.cs b/UCR.Plugins/Remapper/ButtonToButton.cs index a8733e1f..f41634a7 100644 --- a/UCR.Plugins/Remapper/ButtonToButton.cs +++ b/UCR.Plugins/Remapper/ButtonToButton.cs @@ -4,7 +4,7 @@ namespace HidWizards.UCR.Plugins.Remapper { - [Plugin("Button to button")] + [Plugin("Button to Button")] [PluginInput(DeviceBindingCategory.Momentary, "Button")] [PluginOutput(DeviceBindingCategory.Momentary, "Button")] public class ButtonToButton : Plugin diff --git a/UCR.Plugins/Remapper/DeltaToAxis.cs b/UCR.Plugins/Remapper/DeltaToAxis.cs new file mode 100644 index 00000000..7b9f8083 --- /dev/null +++ b/UCR.Plugins/Remapper/DeltaToAxis.cs @@ -0,0 +1,91 @@ +using System; +using System.Timers; +using HidWizards.UCR.Core.Attributes; +using HidWizards.UCR.Core.Models; +using HidWizards.UCR.Core.Models.Binding; +using HidWizards.UCR.Core.Utilities; + +namespace HidWizards.UCR.Plugins.Remapper +{ + [Plugin("Delta to Axis")] + [PluginInput(DeviceBindingCategory.Delta, "Delta")] + [PluginOutput(DeviceBindingCategory.Range, "Axis")] + public class DeltaToAxis : Plugin + { + [PluginGui("Deadzone", ColumnOrder = 0, RowOrder = 0)] + public int Deadzone { get; set; } + + [PluginGui("Relative Sensitivity", ColumnOrder = 1, RowOrder = 0)] + public double RelativeSensitivity { get; set; } + + [PluginGui("Absolute Mode", ColumnOrder = 0, RowOrder = 1)] + public bool AbsoluteMode { get; set; } + + [PluginGui("Absolute Sensitivity", ColumnOrder = 1, RowOrder = 1)] + public double AbsoluteSensitivity { get; set; } + + [PluginGui("Absolute Timeout", ColumnOrder = 2, RowOrder = 1)] + public int AbsoluteTimeout { get; set; } + + private long _currentValue; + private static Timer _absoluteModeTimer; + + public DeltaToAxis() + { + AbsoluteMode = false; + Deadzone = 0; + RelativeSensitivity = 100; + AbsoluteSensitivity = 10000; + AbsoluteTimeout = 100; + _absoluteModeTimer = new Timer(); + _absoluteModeTimer.Elapsed += AbsoluteModeTimerElapsed; + } + + private void AbsoluteModeTimerElapsed(object sender, ElapsedEventArgs e) + { + WriteOutput(0, 0); + SetAbsoluteTimerState(false); + } + + public override void Update(params long[] values) + { + if (Math.Abs(values[0]) < Deadzone) return; + long value; + if (AbsoluteMode) + { + value = (long)(values[0] * AbsoluteSensitivity); + SetAbsoluteTimerState(true); + } + else + { + value = _currentValue + (long)(values[0] * RelativeSensitivity); + } + value = Math.Min(Math.Max(value, Constants.AxisMinValue), Constants.AxisMaxValue); + _currentValue = value; + WriteOutput(0, value); + } + + public void SetAbsoluteTimerState(bool state) + { + if (state) + { + if (_absoluteModeTimer.Enabled) + { + _absoluteModeTimer.Stop(); + } + _absoluteModeTimer.Interval = AbsoluteTimeout; + _absoluteModeTimer.Start(); + } + else if (_absoluteModeTimer.Enabled) + { + _absoluteModeTimer.Stop(); + } + } + + public override void OnDeactivate() + { + base.OnDeactivate(); + SetAbsoluteTimerState(false); + } + } +} diff --git a/UCR.Plugins/Remapper/MouseToAxis.cs b/UCR.Plugins/Remapper/MouseToAxis.cs deleted file mode 100644 index 47005ac0..00000000 --- a/UCR.Plugins/Remapper/MouseToAxis.cs +++ /dev/null @@ -1,21 +0,0 @@ -using HidWizards.UCR.Core.Attributes; -using HidWizards.UCR.Core.Models; -using HidWizards.UCR.Core.Models.Binding; -using HidWizards.UCR.Core.Utilities; - -namespace HidWizards.UCR.Plugins.Remapper -{ - [Plugin("Mouse to Axis", Disabled = true)] - [PluginInput(DeviceBindingCategory.Delta, "Mouse axis")] - [PluginOutput(DeviceBindingCategory.Range, "Axis")] - public class MouseToAxis: Plugin - { - [PluginGui("Invert", RowOrder = 0, ColumnOrder = 0)] - public bool Invert { get; set; } - - public override void Update(params long[] values) - { - WriteOutput(0, values[0]*(Constants.AxisMaxValue/1000)); - } - } -} diff --git a/UCR.Plugins/UCR.Plugins.csproj b/UCR.Plugins/UCR.Plugins.csproj index 573a3c93..f8e91e68 100644 --- a/UCR.Plugins/UCR.Plugins.csproj +++ b/UCR.Plugins/UCR.Plugins.csproj @@ -48,13 +48,15 @@ + + - + + - diff --git a/UCR.Tests/ModelTests/AttributeTests.cs b/UCR.Tests/ModelTests/AttributeTests.cs index 2af0e25e..1adab77f 100644 --- a/UCR.Tests/ModelTests/AttributeTests.cs +++ b/UCR.Tests/ModelTests/AttributeTests.cs @@ -14,12 +14,12 @@ public void ButtonToButtonIoTest() var inputs = plugin.InputCategories; var outputs = plugin.OutputCategories; - Assert.AreEqual(inputs.Count, 1); - Assert.AreEqual(inputs[0].Category, DeviceBindingCategory.Momentary); - Assert.AreEqual(inputs[0].Name, "Button"); - Assert.AreEqual(outputs.Count, 1); - Assert.AreEqual(outputs[0].Category, DeviceBindingCategory.Momentary); - Assert.AreEqual(outputs[0].Name, "Button"); + Assert.That(inputs.Count, Is.EqualTo(1)); + Assert.That(inputs[0].Category, Is.EqualTo(DeviceBindingCategory.Momentary)); + Assert.That(inputs[0].Name, Is.EqualTo("Button")); + Assert.That(outputs.Count, Is.EqualTo(1)); + Assert.That(outputs[0].Category, Is.EqualTo(DeviceBindingCategory.Momentary)); + Assert.That(outputs[0].Name, Is.EqualTo("Button")); } [Test] diff --git a/UCR.Tests/ModelTests/SubscriptionTest.cs b/UCR.Tests/ModelTests/SubscriptionTest.cs new file mode 100644 index 00000000..7c2575e8 --- /dev/null +++ b/UCR.Tests/ModelTests/SubscriptionTest.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HidWizards.UCR.Core; +using HidWizards.UCR.Core.Models; +using HidWizards.UCR.Core.Models.Subscription; +using HidWizards.UCR.Plugins.Remapper; +using NUnit.Framework; + +namespace HidWizards.UCR.Tests.ModelTests +{ + [TestFixture] + internal class SubscriptionTest + { + + private Context _context; + private Profile _profile; + private Mapping _mapping; + private string _profileName; + + [SetUp] + public void Setup() + { + _context = new Context(); + _profileName = "Test"; + _context.ProfilesManager.AddProfile(_profileName); + _profile = _context.Profiles[0]; + } + + [Test] + public void TestEmptyProfile() + { + Assert.IsTrue(_context.SubscriptionsManager.ActivateProfile(_profile)); + + var state = getSubscriptionState(); + Assert.IsTrue(state.IsActive); + Assert.AreEqual(0, state.MappingSubscriptions.Count); + } + + [Test] + public void TestOneBindingProfile() + { + var mapping = _profile.AddMapping("Button"); + var plugin = new ButtonToButton(); + mapping.AddPlugin(plugin, null); + + Assert.IsTrue(_context.SubscriptionsManager.ActivateProfile(_profile)); + + var state = getSubscriptionState(); + Assert.IsTrue(state.IsActive); + Assert.AreEqual(1, state.MappingSubscriptions.Count); + Assert.AreEqual(1, state.MappingSubscriptions[0].PluginSubscriptions.Count); + Assert.AreEqual(plugin, state.MappingSubscriptions[0].PluginSubscriptions[0].Plugin); + } + + private SubscriptionState getSubscriptionState() + { + return _context.SubscriptionsManager.SubscriptionState; + } + } +} diff --git a/UCR.Tests/UCR.Tests.csproj b/UCR.Tests/UCR.Tests.csproj index af8e0ae4..dacba099 100644 --- a/UCR.Tests/UCR.Tests.csproj +++ b/UCR.Tests/UCR.Tests.csproj @@ -46,10 +46,15 @@ + + + + + diff --git a/UCR.Tests/UtilityTests/FunctionTests/FunctionTests.cs b/UCR.Tests/UtilityTests/FunctionTests/FunctionTests.cs new file mode 100644 index 00000000..0ad46771 --- /dev/null +++ b/UCR.Tests/UtilityTests/FunctionTests/FunctionTests.cs @@ -0,0 +1,44 @@ +using HidWizards.UCR.Core.Utilities; +using NUnit.Framework; + +namespace HidWizards.UCR.Tests.UtilityTests.FunctionTests +{ + [TestFixture] + public class FunctionTests + { + [TestCase(0, ExpectedResult = 0, TestName = "Invert: 0 returns 0")] + [TestCase(Constants.AxisMaxValue, ExpectedResult = Constants.AxisMinValue, TestName = "Invert: Max returns Min")] + [TestCase(Constants.AxisMinValue, ExpectedResult = Constants.AxisMaxValue, TestName = "Invert: Min returns Max")] + [TestCase(1, ExpectedResult = -1, TestName = "Invert: 1 returns -1")] + [TestCase(-1, ExpectedResult = 1, TestName = "Invert: -1 returns 1")] + public long InvertTests(long inputValue) + { + return Functions.Invert(inputValue); + } + + [TestCase(Constants.AxisMinValue - 1, ExpectedResult = Constants.AxisMinValue, TestName = "ClampAxisRange: Greater than Max returns Max")] + [TestCase(Constants.AxisMaxValue + 1, ExpectedResult = Constants.AxisMaxValue, TestName = "ClampAxisRange: Less than Min returns Min")] + [TestCase(Constants.AxisMinValue, ExpectedResult = Constants.AxisMinValue, TestName = "ClampAxisRange: Min returns Min")] + [TestCase(Constants.AxisMaxValue, ExpectedResult = Constants.AxisMaxValue, TestName = "ClampAxisRange: Max returns Max")] + [TestCase(0, ExpectedResult = 0, TestName = "ClampAxisRange: 0 returns 0")] + [TestCase(1, ExpectedResult = 1, TestName = "ClampAxisRange: 1 returns 1")] + [TestCase(-1, ExpectedResult = -1, TestName = "ClampAxisRange: -1 returns -1")] + public long ClampTests(long inputValue) + { + return Functions.ClampAxisRange(inputValue); + } + + [TestCase(Constants.AxisMaxValue, true, ExpectedResult = Constants.AxisMaxValue, TestName = "SplitAxis (High): Max returns Max")] + [TestCase(0, true, ExpectedResult = Constants.AxisMinValue, TestName = "SplitAxis (High): 0 returns Min")] + [TestCase(Constants.AxisMinValue, false, ExpectedResult = Constants.AxisMaxValue, TestName = "SplitAxis (Low): Min returns Max")] + [TestCase(0, true, ExpectedResult = Constants.AxisMinValue, TestName = "SplitAxis (Low): 0 returns Min")] + [TestCase(Constants.AxisMinValue, true, ExpectedResult = Constants.AxisMinValue, TestName = "SplitAxis (High): Negative values return Min")] + [TestCase(Constants.AxisMaxValue, false, ExpectedResult = Constants.AxisMinValue, TestName = "SplitAxis (Low): Positive values return Min")] + [TestCase(1, true, ExpectedResult = -32766, TestName = "SplitAxis (High): 1 returns -32766")] + [TestCase(-1, false, ExpectedResult = -32766, TestName = "SplitAxis (Low): -1 returns -32766")] + public long SplitTests(long inputValue, bool positiveRange) + { + return Functions.SplitAxis(inputValue, positiveRange); + } + } +} diff --git a/UCR.Tests/UtilityTests/HelperTests/CircularDeadZoneHelperTests.cs b/UCR.Tests/UtilityTests/HelperTests/CircularDeadZoneHelperTests.cs new file mode 100644 index 00000000..9f0d3ba7 --- /dev/null +++ b/UCR.Tests/UtilityTests/HelperTests/CircularDeadZoneHelperTests.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HidWizards.UCR.Core.Utilities; +using HidWizards.UCR.Core.Utilities.AxisHelpers; +using NUnit.Framework; + +namespace HidWizards.UCR.Tests.UtilityTests.HelperTests +{ + [TestFixture] + public class CircularDeadZoneHelperTests + { + [TestCase(Constants.AxisMaxValue, 0, ExpectedResult = new long[] { Constants.AxisMaxValue, 0 }, TestName = "CircularDeadZoneHelper (Init): Max returns Max")] + [TestCase(Constants.AxisMinValue, 0, ExpectedResult = new long[] { Constants.AxisMinValue, 0 }, TestName = "CircularDeadZoneHelper (Init): Min returns Min")] + [TestCase(0, 0, ExpectedResult = new long[] { 0, 0 }, TestName = "CircularDeadZoneHelper (Init): 0 returns 0")] + public long[] CircularDeadZoneInitTests(long x, long y) + { + var helper = new CircularDeadZoneHelper(); + return helper.ApplyRangeDeadZone(new[] { x, y }); + } + + [TestCase(Constants.AxisMaxValue, 0, 50, ExpectedResult = new long[] { Constants.AxisMaxValue, 0 }, + TestName = "CircularDeadZoneHelper (50): Max X returns Max")] + + [TestCase(Constants.AxisMinValue, 0, 50, ExpectedResult = new long[] { Constants.AxisMinValue, 0 }, + TestName = "CircularDeadZoneHelper (50): Min X returns Min")] + + [TestCase(0, Constants.AxisMaxValue, 50, ExpectedResult = new long[] { 0, Constants.AxisMaxValue }, + TestName = "CircularDeadZoneHelper (50): Max Y returns Max")] + + [TestCase(0, Constants.AxisMinValue, 50, ExpectedResult = new long[] { 0, Constants.AxisMinValue }, + TestName = "CircularDeadZoneHelper (50): Min Y returns Min")] + + [TestCase(0, 0, 50, ExpectedResult = new long[] { 0, 0 }, + TestName = "CircularDeadZoneHelper (50): 0 returns 0")] + + [TestCase(Constants.AxisMaxValue, 0, 20, ExpectedResult = new long[] { Constants.AxisMaxValue, 0 }, + TestName = "CircularDeadZoneHelper (20): Max X returns Max")] + + [TestCase(Constants.AxisMinValue, 0, 20, ExpectedResult = new long[] { Constants.AxisMinValue, 0 }, + TestName = "CircularDeadZoneHelper (20): Min X returns Min")] + + [TestCase(0, Constants.AxisMaxValue, 20, ExpectedResult = new long[] { 0, Constants.AxisMaxValue }, + TestName = "CircularDeadZoneHelper (20): Max Y returns Max")] + + [TestCase(0, Constants.AxisMinValue, 20, ExpectedResult = new long[] { 0, Constants.AxisMinValue }, + TestName = "CircularDeadZoneHelper (20): Min Y returns Min")] + + [TestCase(0, 0, 20, ExpectedResult = new long[] { 0, 0 }, + TestName = "CircularDeadZoneHelper (20): 0 returns 0")] + + [TestCase(16384, 0, 50, ExpectedResult = new long[] { 1, 0 }, + TestName = "CircularDeadZoneHelper (50): Positive X values above 16383 are outside DZ")] + + [TestCase(-16384, 0, 50, ExpectedResult = new long[] { -1, 0 }, + TestName = "CircularDeadZoneHelper (50): Negative X values below 16383 are outside DZ")] + + [TestCase(16383, 0, 50, ExpectedResult = new long[] { 0, 0 }, + TestName = "CircularDeadZoneHelper (50): Positive X values below 16384 are inside DZ")] + + [TestCase(-16383, 0, 50, ExpectedResult = new long[] { 0, 0 }, + TestName = "CircularDeadZoneHelper (50): Negative X values below 16384 are inside DZ")] + + [TestCase(0, 16384, 50, ExpectedResult = new long[] { 0, 1 }, + TestName = "CircularDeadZoneHelper (50): Positive Y values above 16383 are outside DZ")] + + [TestCase(0, -16384, 50, ExpectedResult = new long[] { 0, -1 }, + TestName = "CircularDeadZoneHelper (50): Negative Y values below 16383 are outside DZ")] + + [TestCase(0, 16383, 50, ExpectedResult = new long[] { 0, 0 }, + TestName = "CircularDeadZoneHelper (50): Positive Y values below 16384 are inside DZ")] + + [TestCase(0, -16383, 50, ExpectedResult = new long[] { 0, 0 }, + TestName = "CircularDeadZoneHelper (50): Negative Y values below 16384 are inside DZ")] + + [TestCase(0, -16383, 50, ExpectedResult = new long[] { 0, 0 }, + TestName = "CircularDeadZoneHelper (50): Negative Y values below 16384 are inside DZ")] + + public long[] CircularDeadZoneValueTests(long x, long y, int percentage) + { + var helper = new CircularDeadZoneHelper { Percentage = percentage }; + return helper.ApplyRangeDeadZone(new[] { x, y }); + } + + // These test check that the deadzone is somewhat circular... + // If passing +/- 16383 was INSIDE the DZ when passed for one axis... + // (As tested by other tests) + // ... but OUTSIDE the DZ when passed for both axes... + // ... then the DZ cannot be square. + [TestCase(-16383, -16383, 50, + TestName = "CircularDeadZoneHelper (50): Deadzone appears Circular (Negative)")] + [TestCase(16383, 16383, 50, + TestName = "CircularDeadZoneHelper (50): Deadzone appears Circular (Positive)")] + public void CircularDzOutsideTests(long x, long y, int percentage) + { + var helper = new CircularDeadZoneHelper { Percentage = percentage }; + var result = helper.ApplyRangeDeadZone(new long[] { -16383, -16383 }); + Assert.AreNotEqual(new long[] { 0, 0 }, result); + } + } +} diff --git a/UCR.Tests/UtilityTests/HelperTests/DeadZoneHelperTests.cs b/UCR.Tests/UtilityTests/HelperTests/DeadZoneHelperTests.cs new file mode 100644 index 00000000..1624632f --- /dev/null +++ b/UCR.Tests/UtilityTests/HelperTests/DeadZoneHelperTests.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HidWizards.UCR.Core.Utilities; +using HidWizards.UCR.Core.Utilities.AxisHelpers; +using NUnit.Framework; + +namespace HidWizards.UCR.Tests.UtilityTests.HelperTests +{ + [TestFixture] + public class DeadZoneHelperTests + { + [TestCase(Constants.AxisMaxValue, ExpectedResult = Constants.AxisMaxValue, TestName = "DeadZoneHelper (Init): Max returns Max")] + [TestCase(Constants.AxisMinValue, ExpectedResult = Constants.AxisMinValue, TestName = "DeadZoneHelper (Init): Min returns Min")] + [TestCase(0, ExpectedResult = 0, TestName = "DeadZoneHelper (Init): 0 returns 0")] + [TestCase(1, ExpectedResult = 1, TestName = "DeadZoneHelper (Init): 1 returns 1")] + [TestCase(-1, ExpectedResult = -1, TestName = "DeadZoneHelper (Init): -1 returns -1")] + public long DeadZoneHelperInitTests(long inputValue) + { + var helper = new DeadZoneHelper(); + return helper.ApplyRangeDeadZone(inputValue); + } + + [TestCase(Constants.AxisMaxValue, 50, ExpectedResult = Constants.AxisMaxValue, TestName = "DeadZoneHelper (50): Max returns Max")] + [TestCase(Constants.AxisMinValue, 50, ExpectedResult = Constants.AxisMinValue, TestName = "DeadZoneHelper (50): Min returns Min")] + [TestCase(0, 50, ExpectedResult = 0, TestName = "DeadZoneHelper (50): 0 returns 0")] + [TestCase(16384, 50, ExpectedResult = 1, TestName = "DeadZoneHelper (50): Half Positive deflection is outside DZ")] + [TestCase(-16384, 50, ExpectedResult = -1, TestName = "DeadZoneHelper (50): Half Negative deflection is outside DZ")] + [TestCase(16383, 50, ExpectedResult = 0, TestName = "DeadZoneHelper (50): Below Half Positive deflection is inside DZ")] + [TestCase(-16383, 50, ExpectedResult = 0, TestName = "DeadZoneHelper (50): Below Half Negative deflection is inside DZ")] + public long DeadZoneHelperValueTests(long inputValue, int percentage) + { + var helper = new DeadZoneHelper { Percentage = percentage }; + return helper.ApplyRangeDeadZone(inputValue); + } + + [TestCase(101, TestName = "DeadZoneHelper (101): Percentages over 100 should throw an exception")] + [TestCase(-1, TestName = "DeadZoneHelper (-1): Percentages below 0 should throw an exception")] + public void DeadZoneValidationTest(int percentage) + { + var helper = new DeadZoneHelper(); + Assert.Throws(() => helper.Percentage = percentage); + } + } +} diff --git a/UCR.Tests/UtilityTests/HelperTests/SensitivityHelperTests.cs b/UCR.Tests/UtilityTests/HelperTests/SensitivityHelperTests.cs new file mode 100644 index 00000000..022b5435 --- /dev/null +++ b/UCR.Tests/UtilityTests/HelperTests/SensitivityHelperTests.cs @@ -0,0 +1,46 @@ +using HidWizards.UCR.Core.Utilities; +using HidWizards.UCR.Core.Utilities.AxisHelpers; +using NUnit.Framework; + +namespace HidWizards.UCR.Tests.UtilityTests.HelperTests +{ + [TestFixture] + public class SensitivityHelperTests + { + [TestCase(Constants.AxisMaxValue, ExpectedResult = Constants.AxisMaxValue, TestName = "SensitivityHelper (Init): Max returns Max")] + [TestCase(Constants.AxisMinValue, ExpectedResult = Constants.AxisMinValue, TestName = "SensitivityHelper (Init): Min returns Min")] + [TestCase(0, ExpectedResult = 0, TestName = "SensitivityHelper (Init): 0 returns 0")] + public long SensitivityHelperInitTests(long inputValue) + { + var helper = new SensitivityHelper(); + return helper.ApplyRangeSensitivity(inputValue); + } + + [TestCase(Constants.AxisMaxValue, 50, ExpectedResult = Constants.AxisMaxValue, TestName = "SensitivityHelper (50): Max returns Max")] + [TestCase(Constants.AxisMinValue, 50, ExpectedResult = Constants.AxisMinValue, TestName = "SensitivityHelper (50): Min returns Min")] + [TestCase(Constants.AxisMaxValue, 20, ExpectedResult = Constants.AxisMaxValue, TestName = "SensitivityHelper (20): Max returns Max")] + [TestCase(Constants.AxisMinValue, 20, ExpectedResult = Constants.AxisMinValue, TestName = "SensitivityHelper (20): Min returns Min")] + [TestCase(Constants.AxisMaxValue, 200, ExpectedResult = Constants.AxisMaxValue, TestName = "SensitivityHelper (200): Min returns Min")] + [TestCase(Constants.AxisMinValue, 200, ExpectedResult = Constants.AxisMinValue, TestName = "SensitivityHelper (200): Min returns Min")] + public long SensitivityHelperValueTests(long inputValue, int percentage) + { + var helper = new SensitivityHelper {Percentage = percentage}; + return helper.ApplyRangeSensitivity(inputValue); + } + + [TestCase(Constants.AxisMaxValue, 100, ExpectedResult = Constants.AxisMaxValue, TestName = "SensitivityHelper (100, Linear): Max returns Max")] + [TestCase(Constants.AxisMinValue, 100, ExpectedResult = Constants.AxisMinValue, TestName = "SensitivityHelper (100, Linear): Min returns Min")] + [TestCase(Constants.AxisMaxValue, 100, ExpectedResult = Constants.AxisMaxValue, TestName = "SensitivityHelper (100, Linear): Max returns Max")] + [TestCase(Constants.AxisMinValue, 100, ExpectedResult = Constants.AxisMinValue, TestName = "SensitivityHelper (100, Linear): Min returns Min")] + [TestCase(Constants.AxisMaxValue, 200, ExpectedResult = Constants.AxisMaxValue, TestName = "SensitivityHelper (200, Linear): Min returns Min")] + [TestCase(Constants.AxisMinValue, 200, ExpectedResult = Constants.AxisMinValue, TestName = "SensitivityHelper (200, Linear): Min returns Min")] + [TestCase(16384, 200, ExpectedResult = Constants.AxisMaxValue, TestName = "SensitivityHelper (200, Linear): Half (Positive) Deflection returns Max")] + [TestCase(-16384, 200, ExpectedResult = Constants.AxisMinValue, TestName = "SensitivityHelper (200, Linear): Half (Negative) Deflection returns Min")] + public long SensitivityHelperValueLinearTests(long inputValue, int percentage) + { + var helper = new SensitivityHelper { Percentage = percentage, IsLinear = true}; + return helper.ApplyRangeSensitivity(inputValue); + } + + } +} diff --git a/UCR.sln.DotSettings b/UCR.sln.DotSettings index 2f4bbee6..048f113f 100644 --- a/UCR.sln.DotSettings +++ b/UCR.sln.DotSettings @@ -1,2 +1,3 @@  + True True \ No newline at end of file diff --git a/UCR/UCR.csproj b/UCR/UCR.csproj index 1a346a5c..43b67ea1 100644 --- a/UCR/UCR.csproj +++ b/UCR/UCR.csproj @@ -87,6 +87,8 @@ + + @@ -112,11 +114,17 @@ PluginControl.xaml + EnumControl.xaml + + PreviewControl.xaml + + + StateControl.xaml @@ -187,6 +195,10 @@ MSBuild:Compile Designer + + Designer + MSBuild:Compile + Designer MSBuild:Compile diff --git a/UCR/Utilities/Validators/DecimalBindingValidator.cs b/UCR/Utilities/Validators/DecimalBindingValidator.cs new file mode 100644 index 00000000..97a73047 --- /dev/null +++ b/UCR/Utilities/Validators/DecimalBindingValidator.cs @@ -0,0 +1,20 @@ +using System.Globalization; +using System.Text.RegularExpressions; +using System.Windows.Controls; + +namespace HidWizards.UCR.Utilities.Validators +{ + public class DecimalBindingValidator : ValidationRule + { + public override ValidationResult Validate(object value, CultureInfo cultureInfo) + { + var text = (string) value; + if (text == null) return new ValidationResult(false, "No input"); + + var regex = new Regex(@"^[-+]?[0-9]+\.?[0-9]+?$"); + if (!regex.IsMatch(text)) return new ValidationResult(false, "Invalid input for decimal"); + + return ValidationResult.ValidResult; + } + } +} \ No newline at end of file diff --git a/UCR/Utilities/Validators/NumberBindingValidator.cs b/UCR/Utilities/Validators/NumberBindingValidator.cs new file mode 100644 index 00000000..03b71e2f --- /dev/null +++ b/UCR/Utilities/Validators/NumberBindingValidator.cs @@ -0,0 +1,20 @@ +using System.Globalization; +using System.Text.RegularExpressions; +using System.Windows.Controls; + +namespace HidWizards.UCR.Utilities.Validators +{ + public class NumberBindingValidator : ValidationRule + { + public override ValidationResult Validate(object value, CultureInfo cultureInfo) + { + var text = (string) value; + if (text == null) return new ValidationResult(false, "No input"); + + var regex = new Regex("[0-9]+"); + if (!regex.IsMatch(text)) return new ValidationResult(false, "Invalid input for number"); + + return ValidationResult.ValidResult; + } + } +} \ No newline at end of file diff --git a/UCR/ViewModels/ProfileViewModels/DeviceBindingViewModel.cs b/UCR/ViewModels/ProfileViewModels/DeviceBindingViewModel.cs index 5c9d11ef..018e06ea 100644 --- a/UCR/ViewModels/ProfileViewModels/DeviceBindingViewModel.cs +++ b/UCR/ViewModels/ProfileViewModels/DeviceBindingViewModel.cs @@ -1,11 +1,51 @@ -using HidWizards.UCR.Core.Models.Binding; +using System; +using System.ComponentModel; +using System.Runtime.CompilerServices; +using HidWizards.UCR.Core.Annotations; +using HidWizards.UCR.Core.Models.Binding; namespace HidWizards.UCR.ViewModels.ProfileViewModels { - public class DeviceBindingViewModel + public class DeviceBindingViewModel : INotifyPropertyChanged { public string DeviceBindingName { get; set; } public DeviceBindingCategory DeviceBindingCategory { get; set; } - public DeviceBinding DeviceBinding { get; set; } + + private DeviceBinding _deviceBinding; + public DeviceBinding DeviceBinding + { + get => _deviceBinding; + set + { + _deviceBinding = value; + _deviceBinding.PropertyChanged += DeviceBindingOnPropertyChanged; + CurrentValue = _deviceBinding.CurrentValue; + } + } + + private long _currentValue; + public long CurrentValue + { + get => _currentValue; + set + { + _currentValue = value; + OnPropertyChanged(); + } + } + + private void DeviceBindingOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) + { + var deviceBinding = (DeviceBinding) sender; + CurrentValue = deviceBinding.CurrentValue; + } + + public event PropertyChangedEventHandler PropertyChanged; + + [NotifyPropertyChangedInvocator] + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } } } diff --git a/UCR/Views/AboutWindow.xaml.cs b/UCR/Views/AboutWindow.xaml.cs index 2c1fce88..a9f2c9df 100644 --- a/UCR/Views/AboutWindow.xaml.cs +++ b/UCR/Views/AboutWindow.xaml.cs @@ -22,7 +22,7 @@ private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation. private string GetVersion() { - return "v0.3.1"; + return "v0.4.0"; // TODO System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location); diff --git a/UCR/Views/Controls/DeviceBindingControl.xaml b/UCR/Views/Controls/DeviceBindingControl.xaml index f0388a47..464e7929 100644 --- a/UCR/Views/Controls/DeviceBindingControl.xaml +++ b/UCR/Views/Controls/DeviceBindingControl.xaml @@ -11,6 +11,7 @@ + + diff --git a/UCR/Views/Controls/DeviceBindingControl.xaml.cs b/UCR/Views/Controls/DeviceBindingControl.xaml.cs index 260711bd..f60ee0b8 100644 --- a/UCR/Views/Controls/DeviceBindingControl.xaml.cs +++ b/UCR/Views/Controls/DeviceBindingControl.xaml.cs @@ -4,7 +4,6 @@ using System.Windows; using System.Windows.Controls; using HidWizards.UCR.Core.Models.Binding; -using HidWizards.UCR.Core.Utilities; using HidWizards.UCR.Utilities.Commands; using HidWizards.UCR.ViewModels; @@ -27,7 +26,6 @@ public partial class DeviceBindingControl : UserControl private bool HasLoaded = false; public static readonly DependencyProperty CategoryProperty = DependencyProperty.Register("Category", typeof(DeviceBindingCategory?), typeof(DeviceBindingControl), new PropertyMetadata(default(DeviceBindingCategory?))); - public DeviceBindingControl() { BindMenu = new ObservableCollection(); diff --git a/UCR/Views/Controls/MappingControl.xaml b/UCR/Views/Controls/MappingControl.xaml index 1eda0d28..030b7a07 100644 --- a/UCR/Views/Controls/MappingControl.xaml +++ b/UCR/Views/Controls/MappingControl.xaml @@ -62,7 +62,7 @@ - + diff --git a/UCR/Views/Controls/PluginControl.xaml b/UCR/Views/Controls/PluginControl.xaml index 8d4ff355..6378230a 100644 --- a/UCR/Views/Controls/PluginControl.xaml +++ b/UCR/Views/Controls/PluginControl.xaml @@ -5,6 +5,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:HidWizards.UCR.Views.Controls" xmlns:controls="clr-namespace:HidWizards.UCR.Views.Controls" + xmlns:validators="clr-namespace:HidWizards.UCR.Utilities.Validators" mc:Ignorable="d" d:DesignHeight="100" d:DesignWidth="300" x:Name="PluginViewName" Margin="0,0,0,5"> @@ -56,7 +57,7 @@ - + @@ -80,7 +81,28 @@ - + + + + + + + + + + + + + + + + + + + + + + diff --git a/UCR/Views/Controls/PluginControl.xaml.cs b/UCR/Views/Controls/PluginControl.xaml.cs index c83e29ad..8cf240b9 100644 --- a/UCR/Views/Controls/PluginControl.xaml.cs +++ b/UCR/Views/Controls/PluginControl.xaml.cs @@ -26,5 +26,11 @@ private void NumberValidationTextBox(object sender, TextCompositionEventArgs e) Regex regex = new Regex("[^0-9]+"); e.Handled = regex.IsMatch(e.Text); } + + private void DecimalValidationTextBox(object sender, TextCompositionEventArgs e) + { + Regex regex = new Regex(@"([0-9]|\.)+"); + e.Handled = !regex.IsMatch(e.Text); + } } } diff --git a/UCR/Views/Controls/PluginGuiTemplateSelector.cs b/UCR/Views/Controls/PluginGuiTemplateSelector.cs index 8fc990b3..7f31a11f 100644 --- a/UCR/Views/Controls/PluginGuiTemplateSelector.cs +++ b/UCR/Views/Controls/PluginGuiTemplateSelector.cs @@ -29,6 +29,9 @@ public override DataTemplate SelectTemplate(object item, DependencyObject contai return element.FindResource("EnumTemplate") as DataTemplate; } return element.FindResource("NumberTemplate") as DataTemplate; + case TypeCode.Decimal: + case TypeCode.Double: + return element.FindResource("DecimalTemplate") as DataTemplate; case TypeCode.String: return element.FindResource("StringTemplate") as DataTemplate; case TypeCode.Object: diff --git a/UCR/Views/Controls/PreviewControl.xaml b/UCR/Views/Controls/PreviewControl.xaml new file mode 100644 index 00000000..c9bd57eb --- /dev/null +++ b/UCR/Views/Controls/PreviewControl.xaml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/UCR/Views/Controls/PreviewControl.xaml.cs b/UCR/Views/Controls/PreviewControl.xaml.cs new file mode 100644 index 00000000..a2e2b3fe --- /dev/null +++ b/UCR/Views/Controls/PreviewControl.xaml.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace HidWizards.UCR.Views.Controls +{ + /// + /// Interaction logic for PreviewControl.xaml + /// + public partial class PreviewControl : UserControl + { + public PreviewControl() + { + InitializeComponent(); + } + } +} diff --git a/UCR/Views/Controls/PreviewControlTemplateSelector.cs b/UCR/Views/Controls/PreviewControlTemplateSelector.cs new file mode 100644 index 00000000..2db4b487 --- /dev/null +++ b/UCR/Views/Controls/PreviewControlTemplateSelector.cs @@ -0,0 +1,33 @@ +using System; +using System.Windows; +using System.Windows.Controls; +using HidWizards.UCR.Core.Models.Binding; +using HidWizards.UCR.ViewModels.ProfileViewModels; + +namespace HidWizards.UCR.Views.Controls +{ + class PreviewControlTemplateSelector : DataTemplateSelector + { + public override DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) + { + var element = container as FrameworkElement; + + if (element == null || !(item is DeviceBindingViewModel)) return null; + var deviceBindingViewModel = (DeviceBindingViewModel) item; + + switch (deviceBindingViewModel.DeviceBindingCategory) + { + case DeviceBindingCategory.Event: + return element.FindResource("EventPreview") as DataTemplate; + case DeviceBindingCategory.Momentary: + return element.FindResource("MomentaryPreview") as DataTemplate; + case DeviceBindingCategory.Range: + return element.FindResource("RangePreview") as DataTemplate; + case DeviceBindingCategory.Delta: + return element.FindResource("DeltaPreview") as DataTemplate; + default: + return null; + } + } + } +} diff --git a/UCR/Views/Controls/preview/MomentaryConverter.cs b/UCR/Views/Controls/preview/MomentaryConverter.cs new file mode 100644 index 00000000..5b5d97b8 --- /dev/null +++ b/UCR/Views/Controls/preview/MomentaryConverter.cs @@ -0,0 +1,22 @@ +using System; +using System.Globalization; + +using System.Windows.Data; +using HidWizards.UCR.Core.Utilities; + +namespace HidWizards.UCR.Views.Controls.preview +{ + class MomentaryConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + var pressed = (long) value; + return 100.0 * pressed; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/UCR/Views/Controls/preview/RangeConverter.cs b/UCR/Views/Controls/preview/RangeConverter.cs new file mode 100644 index 00000000..9302e841 --- /dev/null +++ b/UCR/Views/Controls/preview/RangeConverter.cs @@ -0,0 +1,22 @@ +using System; +using System.Globalization; + +using System.Windows.Data; +using HidWizards.UCR.Core.Utilities; + +namespace HidWizards.UCR.Views.Controls.preview +{ + class RangeConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + var range = (long) value; + return 50.0 + ((double)range / Constants.AxisMaxValue) * 50; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/build.ps1 b/build.ps1 index 2a581ef0..2758f36a 100644 --- a/build.ps1 +++ b/build.ps1 @@ -1,52 +1,65 @@ [CmdletBinding()] Param( - [switch]$NoInit, + #[switch]$CustomParam, [Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)] [string[]]$BuildArguments ) -Set-StrictMode -Version 2.0; $ErrorActionPreference = "Stop"; $ConfirmPreference = "None"; trap { $host.SetShouldExit(0) } +Write-Output "Windows PowerShell $($Host.Version)" + +Set-StrictMode -Version 2.0; $ErrorActionPreference = "Stop"; $ConfirmPreference = "None"; trap { $host.SetShouldExit(1) } $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent ########################################################################### # CONFIGURATION ########################################################################### -$NuGetVersion = "latest" -$SolutionDirectory = "$PSScriptRoot\..\UCR" -$BuildProjectFile = "$PSScriptRoot\.\build\.build.csproj" -$BuildExeFile = "$PSScriptRoot\.\build\bin\debug\.build.exe" +$BuildProjectFile = "$PSScriptRoot\build\.build.csproj" +$TempDirectory = "$PSScriptRoot\\.tmp" -$TempDirectory = "$PSScriptRoot\.tmp" +$DotNetGlobalFile = "$PSScriptRoot\\global.json" +$DotNetInstallUrl = "https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.ps1" +$DotNetReleasesUrl = "https://raw.githubusercontent.com/dotnet/core/master/release-notes/releases.json" -$NuGetUrl = "https://dist.nuget.org/win-x86-commandline/$NuGetVersion/nuget.exe" -$NuGetFile = "$TempDirectory\nuget.exe" -$env:NUGET_EXE = $NuGetFile +$env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE = 1 +$env:DOTNET_CLI_TELEMETRY_OPTOUT = 1 +$env:NUGET_XMLDOC_MODE = "skip" ########################################################################### -# PREPARE BUILD +# EXECUTION ########################################################################### function ExecSafe([scriptblock] $cmd) { & $cmd - if ($LastExitCode -ne 0) { throw "The following call failed with exit code $LastExitCode. '$cmd'" } + if ($LASTEXITCODE) { exit $LASTEXITCODE } } -if (!$NoInit) { - md -force $TempDirectory > $null - - if (!(Test-Path $NuGetFile)) { (New-Object System.Net.WebClient).DownloadFile($NuGetUrl, $NuGetFile) } - elseif ($NuGetVersion -eq "latest") { & $NuGetFile update -Self } +# If global.json exists, load expected version +if (Test-Path $DotNetGlobalFile) { + $DotNetVersion = $(Get-Content $DotNetGlobalFile | Out-String | ConvertFrom-Json).sdk.version +} - ExecSafe { & $NuGetFile restore $BuildProjectFile -SolutionDirectory $SolutionDirectory } - ExecSafe { & $NuGetFile install Nuke.MSBuildLocator -ExcludeVersion -OutputDirectory $TempDirectory -SolutionDirectory $SolutionDirectory } +# If dotnet is installed locally, and expected version is not set or installation matches the expected version +if ((Get-Command "dotnet" -ErrorAction SilentlyContinue) -ne $null -and ` + (!(Test-Path variable:DotNetVersion) -or $(& dotnet --version) -eq $DotNetVersion)) { + $env:DOTNET_EXE = (Get-Command "dotnet").Path } +else { + $DotNetDirectory = "$TempDirectory\dotnet-win" + $env:DOTNET_EXE = "$DotNetDirectory\dotnet.exe" -$MSBuildFile = & "$TempDirectory\Nuke.MSBuildLocator\tools\Nuke.MSBuildLocator.exe" -ExecSafe { & $MSBuildFile $BuildProjectFile } + # If expected version is not set, get latest version + if (!(Test-Path variable:DotNetVersion)) { + $DotNetVersion = $(Invoke-WebRequest -UseBasicParsing $DotNetReleasesUrl | ConvertFrom-Json)[0]."version-sdk" + } -########################################################################### -# EXECUTE BUILD -########################################################################### + # Download and execute install script + $DotNetInstallFile = "$TempDirectory\dotnet-install.ps1" + md -force $TempDirectory > $null + (New-Object System.Net.WebClient).DownloadFile($DotNetInstallUrl, $DotNetInstallFile) + ExecSafe { & $DotNetInstallFile -InstallDir $DotNetDirectory -Version $DotNetVersion -NoPath } +} + +Write-Output "Microsoft (R) .NET Core SDK version $(& $env:DOTNET_EXE --version)" -ExecSafe { & $BuildExeFile $BuildArguments } +ExecSafe { & $env:DOTNET_EXE run --project $BuildProjectFile -- $BuildArguments } diff --git a/build.sh b/build.sh index 659e9b09..40b3305a 100644 --- a/build.sh +++ b/build.sh @@ -1,10 +1,12 @@ #!/usr/bin/env bash -NOINIT=0 +echo $(bash --version 2>&1 | head -n 1) + +#CUSTOMPARAM=0 BUILD_ARGUMENTS=() for i in "$@"; do case $(echo $1 | awk '{print tolower($0)}') in - -noinit) NOINIT=1;; + # -custom-param) CUSTOMPARAM=1;; *) BUILD_ARGUMENTS+=("$1") ;; esac shift @@ -17,34 +19,50 @@ SCRIPT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd) # CONFIGURATION ########################################################################### -NUGET_VERSION="latest" -SOLUTION_DIRECTORY="$SCRIPT_DIR/../UCR" -BUILD_PROJECT_FILE="$SCRIPT_DIR/./build/.build.csproj" -BUILD_EXE_FILE="$SCRIPT_DIR/./build/bin/Debug/.build.exe" +BUILD_PROJECT_FILE="$SCRIPT_DIR/build/.build.csproj" +TEMP_DIRECTORY="$SCRIPT_DIR//.tmp" -TEMP_DIRECTORY="$SCRIPT_DIR/.tmp" +DOTNET_GLOBAL_FILE="$SCRIPT_DIR//global.json" +DOTNET_INSTALL_URL="https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.sh" +DOTNET_RELEASES_URL="https://raw.githubusercontent.com/dotnet/core/master/release-notes/releases.json" -NUGET_URL="https://dist.nuget.org/win-x86-commandline/$NUGET_VERSION/nuget.exe" -NUGET_FILE="$TEMP_DIRECTORY/nuget.exe" -export NUGET_EXE="$NUGET_FILE" +export DOTNET_CLI_TELEMETRY_OPTOUT=1 +export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1 +export NUGET_XMLDOC_MODE="skip" ########################################################################### -# PREPARE BUILD +# EXECUTION ########################################################################### -if ! ((NOINIT)); then - mkdir -p "$TEMP_DIRECTORY" - - if [ ! -f "$NUGET_FILE" ]; then curl -Lsfo "$NUGET_FILE" $NUGET_URL; - elif [ $NUGET_VERSION == "latest" ]; then mono "$NUGET_FILE" update -Self; fi +function FirstJsonValue { + perl -nle 'print $1 if m{"'$1'": "([^"\-]+)",?}' <<< ${@:2} +} - mono "$NUGET_FILE" restore "$BUILD_PROJECT_FILE" -SolutionDirectory $SOLUTION_DIRECTORY +# If global.json exists, load expected version +if [ -f "$DOTNET_GLOBAL_FILE" ]; then + DOTNET_VERSION=$(FirstJsonValue "version" $(cat "$DOTNET_GLOBAL_FILE")) fi -msbuild "$BUILD_PROJECT_FILE" +# If dotnet is installed locally, and expected version is not set or installation matches the expected version +if [[ -x "$(command -v dotnet)" && (-z ${DOTNET_VERSION+x} || $(dotnet --version) == "$DOTNET_VERSION") ]]; then + export DOTNET_EXE="$(command -v dotnet)" +else + DOTNET_DIRECTORY="$TEMP_DIRECTORY/dotnet-unix" + export DOTNET_EXE="$DOTNET_DIRECTORY/dotnet" + + # If expected version is not set, get latest version + if [ -z ${DOTNET_VERSION+x} ]; then + DOTNET_VERSION=$(FirstJsonValue "version-sdk" $(curl -s "$DOTNET_RELEASES_URL")) + fi + + # Download and execute install script + DOTNET_INSTALL_FILE="$TEMP_DIRECTORY/dotnet-install.sh" + mkdir -p "$TEMP_DIRECTORY" + curl -Lsfo "$DOTNET_INSTALL_FILE" "$DOTNET_INSTALL_URL" + chmod +x "$DOTNET_INSTALL_FILE" + "$DOTNET_INSTALL_FILE" --install-dir "$DOTNET_DIRECTORY" --version "$DOTNET_VERSION" --no-path +fi -########################################################################### -# EXECUTE BUILD -########################################################################### +echo "Microsoft (R) .NET Core SDK version $("$DOTNET_EXE" --version)" -mono "$BUILD_EXE_FILE" ${BUILD_ARGUMENTS[@]} +"$DOTNET_EXE" run --project "$BUILD_PROJECT_FILE" -- ${BUILD_ARGUMENTS[@]} diff --git a/build/.build.csproj b/build/.build.csproj index 84ad6b3c..ce83cb9b 100644 --- a/build/.build.csproj +++ b/build/.build.csproj @@ -2,30 +2,37 @@ Exe - net461 + netcoreapp2.1 false False CS0649;CS0169 - - - * - False - - - - - - + + + + + + - + + - + + + + + + + + + + + diff --git a/build/.build.csproj.DotSettings b/build/.build.csproj.DotSettings index 5af2bdc9..96e392e6 100644 --- a/build/.build.csproj.DotSettings +++ b/build/.build.csproj.DotSettings @@ -1,4 +1,4 @@ - + False Implicit Implicit diff --git a/build/Build.cs b/build/Build.cs index 00aef004..5675a97d 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -1,43 +1,28 @@ -using System; +using System; using System.Linq; using Nuke.Common; -using Nuke.Common.Git; -using Nuke.Common.Gitter; -using Nuke.Common.Tools.DotNet; -using Nuke.Common.Tools.GitVersion; +using Nuke.Common.ProjectModel; using Nuke.Common.Tools.MSBuild; using Nuke.Common.Tools.NuGet; -using Nuke.Core; using Nuke.Core.Tooling; using static Nuke.Common.Tools.Git.GitTasks; using static Nuke.Common.Tools.MSBuild.MSBuildTasks; -using static Nuke.Core.IO.FileSystemTasks; -using static Nuke.Core.IO.PathConstruction; -using static Nuke.Core.EnvironmentInfo; +using static Nuke.Common.EnvironmentInfo; +using static Nuke.Common.IO.FileSystemTasks; +using static Nuke.Common.IO.PathConstruction; +using ToolSettingsExtensions = Nuke.Common.Tooling.ToolSettingsExtensions; class Build : NukeBuild { - // Console application entry. Also defines the default target. - public static int Main() => Execute(x => x.Test); + public static int Main () => Execute(x => x.Test); - // Auto-injection fields: - - // [GitVersion] readonly GitVersion GitVersion; - // Semantic versioning. Must have 'GitVersion.CommandLine' referenced. - - // [GitRepository] readonly GitRepository GitRepository; - // Parses origin, branch name and head from git config. - - // [Parameter] readonly string MyGetApiKey; - // Returns command-line arguments and environment variables. + [Solution] readonly Solution Solution; string IoWrapper => "IOWrapper"; AbsolutePath IoWrapperDirectory => RootDirectory / "submodules" / IoWrapper; AbsolutePath IoWrapperSolution => IoWrapperDirectory / (IoWrapper + ".sln"); - MSBuildSettings DefaultIoWrapperMSBuild => DefaultMSBuild - .SetWorkingDirectory(IoWrapperDirectory) - .SetSolutionFile(IoWrapperSolution); + MSBuildSettings DefaultIoWrapperMSBuild => ToolSettingsExtensions.SetWorkingDirectory(DefaultMSBuildCompile, IoWrapperDirectory).SetSolutionFile(IoWrapperSolution); Target Clean => _ => _ @@ -72,23 +57,23 @@ class Build : NukeBuild .DependsOn(RestoreSubmodules) .DependsOn(CompileSubmodules) .Executes(() => - { - CopyRecursively(IoWrapperDirectory / "Artifacts", SolutionDirectory / "dependencies", FileExistsPolicy.Overwrite); - }); + { + CopyDirectoryRecursively(IoWrapperDirectory / "Artifacts", SolutionDirectory / "dependencies", FileExistsPolicy.Overwrite); + }); Target Restore => _ => _ - .DependsOn(Clean) - .Executes(() => - { - MSBuild(s => DefaultMSBuildRestore); - }); + .DependsOn(Clean) + .Executes(() => + { + MSBuild(s => DefaultMSBuildRestore); + }); Target Compile => _ => _ - .DependsOn(Restore) - .Executes(() => - { - MSBuild(s => DefaultMSBuildCompile); - }); + .DependsOn(Restore) + .Executes(() => + { + MSBuild(s => DefaultMSBuildCompile); + }); Target Test => _ => _ .DependsOn(Compile) diff --git a/submodules/IOWrapper b/submodules/IOWrapper index f2e9f2b0..34088383 160000 --- a/submodules/IOWrapper +++ b/submodules/IOWrapper @@ -1 +1 @@ -Subproject commit f2e9f2b0fab682e710a1e450794a45427065c2f4 +Subproject commit 340883836a0cdc93a03d0e43a2f6b39b7c81b1a7