Skip to content

Commit

Permalink
Feature/rework override default (#54)
Browse files Browse the repository at this point in the history
* Resolves #43

* allow better blocking mouse buttons and moves (#43)

* Remove now unused wndproc handling to simplify code
  • Loading branch information
luttje authored Oct 22, 2023
1 parent 6b38fe9 commit 93ef399
Show file tree
Hide file tree
Showing 19 changed files with 286 additions and 154 deletions.
7 changes: 6 additions & 1 deletion Core/Key2Joy.Contracts/Mapping/AbstractMappingAspect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Policy;
using System.Text.Json.Serialization;

namespace Key2Joy.Contracts.Mapping;
Expand Down Expand Up @@ -87,7 +88,11 @@ protected virtual void LoadOptionSetProperty(MappingAspectOptions options, Prope

propertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType;

if (propertyType.IsEnum)
if (value == null)
{
value = null;
}
else if (propertyType.IsEnum)
{
value = Enum.Parse(propertyType, (string)value);
}
Expand Down
33 changes: 0 additions & 33 deletions Core/Key2Joy.Contracts/Mapping/IWndProcHandler.cs

This file was deleted.

49 changes: 49 additions & 0 deletions Core/Key2Joy.Contracts/Util/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ namespace Key2Joy.Contracts.Util;

public static class StringExtensions
{
/// <summary>
/// Ellipsizes the string to the specified length, adding ... at the end
/// </summary>
/// <param name="input"></param>
/// <param name="maxLength"></param>
/// <returns></returns>
/// <exception cref="System.ArgumentOutOfRangeException"></exception>
public static string Ellipsize(this string input, int maxLength)
{
if (input == null)
Expand All @@ -26,4 +33,46 @@ public static string Ellipsize(this string input, int maxLength)

return input.Substring(0, maxLength - 3) + "...";
}

/// <summary>
/// Places newlines in the string to make it fit the specified width
/// </summary>
/// <param name="input"></param>
/// <param name="maxCharLength"></param>
/// <returns></returns>
public static string Wrap(this string input, int maxCharLength)
{
if (input == null)
{
return null;
}

if (maxCharLength <= 0)
{
throw new System.ArgumentOutOfRangeException(nameof(maxCharLength));
}

if (input.Length <= maxCharLength)
{
return input;
}

var sb = new System.Text.StringBuilder();
var words = input.Split(' ');
var lineLength = 0;
foreach (var word in words)
{
if (lineLength + word.Length > maxCharLength)
{
sb.AppendLine();
lineLength = 0;
}

sb.Append(word);
sb.Append(' ');
lineLength += word.Length + 1;
}

return sb.ToString().TrimEnd();
}
}
1 change: 1 addition & 0 deletions Core/Key2Joy.Core/Config/ConfigControlAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Key2Joy.Config;
public abstract class ConfigControlAttribute : Attribute
{
public string Text { get; set; }
public string Hint { get; set; } = null;

/// <summary>
/// Gets all configs and their property
Expand Down
87 changes: 75 additions & 12 deletions Core/Key2Joy.Core/Config/ConfigState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public string LastInstallPath
private string lastInstallPath;

[EnumConfigControl(
Text = "Style mapped options are grouped by",
Text = "Mapped options grouping style",
EnumType = typeof(ViewMappingGroupType)
)]
public ViewMappingGroupType SelectedViewMappingGroupType
Expand All @@ -45,17 +45,6 @@ public bool ShouldCloseButtonMinimize

private bool shouldCloseButtonMinimize;

[BooleanConfigControl(
Text = "Override default behaviour when trigger action is executed"
)]
public bool OverrideDefaultTriggerBehaviour
{
get => this.overrideDefaultTriggerBehaviour;
set => this.SaveIfInitialized(this.overrideDefaultTriggerBehaviour = value);
}

private bool overrideDefaultTriggerBehaviour = true;

[TextConfigControl(
Text = "Last loaded mapping profile file location"
)]
Expand All @@ -67,6 +56,80 @@ public string LastLoadedProfile

private string lastLoadedProfile;

#region Listener Overrides

[BooleanConfigControl(
Text = "While armed override the default Keyboard behaviour for mapped keys"
)]
public bool ListenerOverrideDefaultKeyboard
{
get => this.listenerOverrideDefaultKeyboard;
set => this.SaveIfInitialized(this.listenerOverrideDefaultKeyboard = value);
}

private bool listenerOverrideDefaultKeyboard = true;

[BooleanConfigControl(
Text = "While armed override the default Keyboard behaviour for all keys",
Hint = "Make sure you map an 'Abort' action to a key, so you can disarm the mappings."
)]
public bool ListenerOverrideDefaultKeyboardAll
{
get => this.listenerOverrideDefaultKeyboardAll;
set => this.SaveIfInitialized(this.listenerOverrideDefaultKeyboardAll = value);
}

private bool listenerOverrideDefaultKeyboardAll = false;

[BooleanConfigControl(
Text = "While armed override the default Mouse behaviour for mapped buttons"
)]
public bool ListenerOverrideDefaultMouse
{
get => this.listenerOverrideDefaultMouse;
set => this.SaveIfInitialized(this.listenerOverrideDefaultMouse = value);
}

private bool listenerOverrideDefaultMouse = true;

[BooleanConfigControl(
Text = "While armed override the default Mouse behaviour for all buttons",
Hint = "Make sure you map an 'Abort' action to a key, otherwise you can't click the disarm checkbox!"
)]
public bool ListenerOverrideDefaultMouseAll
{
get => this.listenerOverrideDefaultMouseAll;
set => this.SaveIfInitialized(this.listenerOverrideDefaultMouseAll = value);
}

private bool listenerOverrideDefaultMouseAll = false;

[BooleanConfigControl(
Text = "While armed override the default Mouse Move behaviour for mapped moves",
Hint = "This cannot properly override moves in a specific direction. Use the 'Any' mouse move direction instead."
)]
public bool ListenerOverrideDefaultMouseMove
{
get => this.listenerOverrideDefaultMouseMove;
set => this.SaveIfInitialized(this.listenerOverrideDefaultMouseMove = value);
}

private bool listenerOverrideDefaultMouseMove = false;

[BooleanConfigControl(
Text = "While armed override the default Mouse Move behaviour for all moves",
Hint = "Make sure you map an 'Abort' action to a key, otherwise you can't move to click the disarm checkbox!"
)]
public bool ListenerOverrideDefaultMouseMoveAll
{
get => this.listenerOverrideDefaultMouseMoveAll;
set => this.SaveIfInitialized(this.listenerOverrideDefaultMouseMoveAll = value);
}

private bool listenerOverrideDefaultMouseMoveAll = false;

#endregion Listener Overrides

public Dictionary<string, string> EnabledPlugins { get; set; } = new Dictionary<string, string>();

private void SaveIfInitialized(object changedValue = null)
Expand Down
16 changes: 8 additions & 8 deletions Core/Key2Joy.Core/IAcceptAppCommands.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Key2Joy.Mapping.Actions.Logic;

namespace Key2Joy;

public interface IAcceptAppCommands
{
bool RunAppCommand(AppCommand command);
}
using Key2Joy.Mapping.Actions.Logic;

namespace Key2Joy;

public interface IAcceptAppCommands
{
bool RunAppCommand(AppCommand command);
}
3 changes: 0 additions & 3 deletions Core/Key2Joy.Core/Key2Joy.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@
<PackageReference Include="NLua">
<Version>1.6.3</Version>
</PackageReference>
<PackageReference Include="RawInput.Sharp">
<Version>0.1.1</Version>
</PackageReference>
<PackageReference Include="SimWinGamePad">
<Version>1.1.1</Version>
</PackageReference>
Expand Down
37 changes: 1 addition & 36 deletions Core/Key2Joy.Core/Key2JoyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using CommonServiceLocator;
using Key2Joy.Config;
using Key2Joy.Contracts.Mapping;
using Key2Joy.Contracts.Mapping.Actions;
using Key2Joy.Contracts.Mapping.Triggers;
using Key2Joy.Interop;
Expand All @@ -25,7 +23,7 @@ namespace Key2Joy;

public delegate bool AppCommandRunner(AppCommand command);

public class Key2JoyManager : IKey2JoyManager, IMessageFilter
public class Key2JoyManager : IKey2JoyManager
{
/// <summary>
/// Directory where plugins are located
Expand Down Expand Up @@ -59,7 +57,6 @@ public static Key2JoyManager Instance
private static AppCommandRunner commandRunner;
private MappingProfile armedProfile;
private IHaveHandleAndInvoke handleAndInvoker;
private readonly List<IWndProcHandler> wndProcListeners = new();

private Key2JoyManager()
{ }
Expand Down Expand Up @@ -144,29 +141,9 @@ public static void InitSafely(AppCommandRunner commandRunner, Action<PluginSet>

internal static bool RunAppCommand(AppCommand command) => commandRunner != null && commandRunner(command);

public bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
for (var i = 0; i < this.wndProcListeners.Count; i++)
{
// Check if the proc listeners haven't changed (this can happen when a plugin opens a MessageBox, the user aborts, and we then close the messagebox)
if (i >= this.wndProcListeners.Count)
{
Debug.WriteLine("Key2JoyManager.PreFilterMessage: wndProcListeners changed while processing message!");
break;
}

var wndProcListener = this.wndProcListeners[i];

wndProcListener.WndProc(new Contracts.Mapping.Message(m.HWnd, m.Msg, m.WParam, m.LParam));
}

return false;
}

public void SetHandlerWithInvoke(IHaveHandleAndInvoke handleAndInvoker)
{
this.handleAndInvoker = handleAndInvoker;
Application.AddMessageFilter(this);

Console.WriteLine(READY_MESSAGE);
}
Expand Down Expand Up @@ -213,11 +190,6 @@ public void ArmMappings(MappingProfile profile)
allListeners.Add(listener);
}

if (listener is IWndProcHandler listenerWndProcHAndler)
{
this.wndProcListeners.Add(listenerWndProcHAndler);
}

mappedOption.Action.OnStartListening(listener, ref allActions);
listener.AddMappedOption(mappedOption);
}
Expand All @@ -226,11 +198,6 @@ public void ArmMappings(MappingProfile profile)

foreach (var listener in allListeners)
{
if (listener is IWndProcHandler listenerWndProcHAndler)
{
listenerWndProcHAndler.Handle = this.handleAndInvoker.Handle;
}

listener.StartListening(ref allListenersForSharing);
}

Expand All @@ -242,7 +209,6 @@ public void ArmMappings(MappingProfile profile)
}
catch (MappingArmingFailedException ex)
{
//cleanup
this.DisarmMappings();
throw ex;
}
Expand All @@ -251,7 +217,6 @@ public void ArmMappings(MappingProfile profile)
public void DisarmMappings()
{
var listeners = this.ExplicitTriggerListeners;
this.wndProcListeners.Clear();

// Clear all intervals
IdPool.CancelAll();
Expand Down
10 changes: 5 additions & 5 deletions Core/Key2Joy.Core/Mapping/JsonMappingAspectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ private AbstractMappingAspect ParseJson(Type typeToConvert, JsonDocument json, J

foreach (var property in aspectRootProperty.EnumerateObject())
{
if (property.Value.ValueKind == JsonValueKind.Object)
if (property.Value.ValueKind == JsonValueKind.Null)
{
mappingAspectOptions.Add(property.Name, null);
}
else if (property.Value.ValueKind == JsonValueKind.Object)
{
throw new NotImplementedException($"The value kind {property.Value.ValueKind} is not yet supported.");
}
Expand All @@ -127,10 +131,6 @@ private AbstractMappingAspect ParseJson(Type typeToConvert, JsonDocument json, J
{
mappingAspectOptions.Add(property.Name, property.Value.GetBoolean());
}
else if (property.Value.ValueKind == JsonValueKind.Null)
{
mappingAspectOptions.Add(property.Name, null);
}
else if (property.Value.ValueKind == JsonValueKind.Array)
{
List<object> list = new();
Expand Down
Loading

0 comments on commit 93ef399

Please sign in to comment.