From 126691c780e1e55cc03870a9b64099ca623fcaf8 Mon Sep 17 00:00:00 2001 From: logchan Date: Thu, 13 Feb 2020 11:03:04 -0500 Subject: [PATCH] notify property changed with magic --- .gitignore | 3 ++- chuni-hands/Config.cs | 6 +++++- chuni-hands/Helpers.cs | 36 +++++++++++++++++++++++++++++++++- chuni-hands/MainWindow.xaml | 11 ++++++----- chuni-hands/MainWindow.xaml.cs | 5 +++++ chuni-hands/chuni-hands.csproj | 3 +++ chuni-hands/packages.config | 1 + 7 files changed, 57 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index a9b29e0..e758713 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vs/ bin/ -obj/ \ No newline at end of file +obj/ +packages/ diff --git a/chuni-hands/Config.cs b/chuni-hands/Config.cs index 1292daf..b1eb215 100644 --- a/chuni-hands/Config.cs +++ b/chuni-hands/Config.cs @@ -1,5 +1,9 @@ namespace chuni_hands { - public sealed class Config { + public sealed class Config : Helpers.PropertyChangedInvoker { + static Config() { + Helpers.PatchNotifyPropertyChanged(); + } + public int OffsetX { get; set; } public int OffsetY { get; set; } public int Exposure { get; set; } = -6; diff --git a/chuni-hands/Helpers.cs b/chuni-hands/Helpers.cs index 493a108..9c7c386 100644 --- a/chuni-hands/Helpers.cs +++ b/chuni-hands/Helpers.cs @@ -1,6 +1,11 @@ -using System.Diagnostics; +using System; +using System.ComponentModel; +using System.Diagnostics; using System.IO; +using System.Linq; using System.Reflection; +using System.Windows.Threading; +using Harmony; using Newtonsoft.Json; namespace chuni_hands { @@ -18,5 +23,34 @@ namespace chuni_hands { public static string GetVersion() { return FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion; } + + private static HarmonyInstance _harmony = HarmonyInstance.Create("co.logu.chuni-hands.helpers"); + + public static void PatchNotifyPropertyChanged() where T : PropertyChangedInvoker { + var type = typeof(T); + var patch = typeof(Helpers).GetMethod("PropertyChangedPostfix", BindingFlags.NonPublic | BindingFlags.Static); + Debug.Assert(patch != null, nameof(patch) + " != null"); + patch = patch.MakeGenericMethod(type); + + foreach (var method in type.GetProperties().Select(p => p.SetMethod)) { + _harmony.Patch(method, new HarmonyMethod(patch)); + } + } + + // ReSharper disable InconsistentNaming + // ReSharper disable once UnusedMember.Local + private static void PropertyChangedPostfix(MethodBase __originalMethod, T __instance) where T : PropertyChangedInvoker { + __instance.InvokePropertyChanged(__originalMethod.Name.Substring(4)); // remove set_ + } + // ReSharper restore InconsistentNaming + + public abstract class PropertyChangedInvoker : INotifyPropertyChanged { + public event PropertyChangedEventHandler PropertyChanged; + public virtual void InvokePropertyChanged(string propertyName) { + Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + })); + } + } } } diff --git a/chuni-hands/MainWindow.xaml b/chuni-hands/MainWindow.xaml index 72452fe..2147251 100644 --- a/chuni-hands/MainWindow.xaml +++ b/chuni-hands/MainWindow.xaml @@ -16,7 +16,7 @@ - + @@ -26,17 +26,18 @@ Distance - + X - + Y - + - Log diff + + Log diff diff --git a/chuni-hands/MainWindow.xaml.cs b/chuni-hands/MainWindow.xaml.cs index 5497731..6777f80 100644 --- a/chuni-hands/MainWindow.xaml.cs +++ b/chuni-hands/MainWindow.xaml.cs @@ -150,5 +150,10 @@ namespace chuni_hands { _config.Threshold = v; } } + + private void CenterButton_Click(object sender, RoutedEventArgs e) { + _config.OffsetX = 0; + _config.OffsetY = 0; + } } } diff --git a/chuni-hands/chuni-hands.csproj b/chuni-hands/chuni-hands.csproj index ca644f3..f54710a 100644 --- a/chuni-hands/chuni-hands.csproj +++ b/chuni-hands/chuni-hands.csproj @@ -57,6 +57,9 @@ true + + ..\packages\Lib.Harmony.1.2.0.1\lib\net472\0Harmony.dll + C:\Emgu\emgucv-windesktop 4.2.0.3662\bin\Emgu.CV.World.Netstandard.dll diff --git a/chuni-hands/packages.config b/chuni-hands/packages.config index a9de8b5..05d0f5b 100644 --- a/chuni-hands/packages.config +++ b/chuni-hands/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file