From 28c3b04503d4d84e82638b688daa890248d04486 Mon Sep 17 00:00:00 2001 From: Jos Date: Sun, 20 Feb 2022 12:00:44 +0100 Subject: [PATCH] Added settings manager --- NewWorldCompanion.Constants/EmguConstants.cs | 14 ++-- NewWorldCompanion.Entities/SettingsNWC.cs | 21 ++++++ .../IScreenProcessHandler.cs | 12 +-- .../ISettingsManager.cs | 16 ++++ .../ScreenProcessHandler.cs | 30 +++----- NewWorldCompanion.Services/SettingsManager.cs | 74 +++++++++++++++++++ NewWorldCompanion/App.xaml.cs | 1 + .../Tabs/Debug/DebugScreenOCRViewModel.cs | 26 +++---- .../Tabs/Debug/DebugScreenProcessViewModel.cs | 48 +++++------- 9 files changed, 165 insertions(+), 77 deletions(-) create mode 100644 NewWorldCompanion.Entities/SettingsNWC.cs create mode 100644 NewWorldCompanion.Interfaces/ISettingsManager.cs create mode 100644 NewWorldCompanion.Services/SettingsManager.cs diff --git a/NewWorldCompanion.Constants/EmguConstants.cs b/NewWorldCompanion.Constants/EmguConstants.cs index be5e94e..8aa5b0d 100644 --- a/NewWorldCompanion.Constants/EmguConstants.cs +++ b/NewWorldCompanion.Constants/EmguConstants.cs @@ -2,12 +2,14 @@ { public class EmguConstants { - public const int AreaLower = 10000; - public const int AreaUpper = 15000; - public const int HysteresisLower = 10; - public const int HysteresisUpper = 400; + // Shape detection + public const int DefaultAreaLower = 10000; + public const int DefaultAreaUpper = 15000; + public const int DefaultHysteresisLower = 10; + public const int DefaultHysteresisUpper = 400; - public const int ThresholdMin = 90; - public const int ThresholdMax = 255; + // OCR + public const int DefaultThresholdMin = 90; + public const int DefaultThresholdMax = 255; } } \ No newline at end of file diff --git a/NewWorldCompanion.Entities/SettingsNWC.cs b/NewWorldCompanion.Entities/SettingsNWC.cs new file mode 100644 index 0000000..9597b5c --- /dev/null +++ b/NewWorldCompanion.Entities/SettingsNWC.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NewWorldCompanion.Entities +{ + public class SettingsNWC + { + // Shape detection + public int EmguAreaLower { get; set; } = 10000; + public int EmguAreaUpper { get; set; } = 15000; + public int EmguHysteresisLower { get; set; } = 10; + public int EmguHysteresisUpper { get; set; } = 400; + + // OCR + public int EmguThresholdMin { get; set; } = 90; + public int EmguThresholdMax { get; set; } = 255; + } +} diff --git a/NewWorldCompanion.Interfaces/IScreenProcessHandler.cs b/NewWorldCompanion.Interfaces/IScreenProcessHandler.cs index 40f8d41..6ae08fc 100644 --- a/NewWorldCompanion.Interfaces/IScreenProcessHandler.cs +++ b/NewWorldCompanion.Interfaces/IScreenProcessHandler.cs @@ -4,12 +4,12 @@ namespace NewWorldCompanion.Interfaces { public interface IScreenProcessHandler { - int AreaLower { get; set; } - int AreaUpper { get; set; } - int HysteresisLower { get; set; } - int HysteresisUpper { get; set; } - int ThresholdMin { get; set; } - int ThresholdMax { get; set; } + int AreaLower { get; } + int AreaUpper { get; } + int HysteresisLower { get; } + int HysteresisUpper { get; } + int ThresholdMin { get; } + int ThresholdMax { get; } Bitmap? ProcessedImage { get; } Bitmap? RoiImage { get; } diff --git a/NewWorldCompanion.Interfaces/ISettingsManager.cs b/NewWorldCompanion.Interfaces/ISettingsManager.cs new file mode 100644 index 0000000..1e56d5f --- /dev/null +++ b/NewWorldCompanion.Interfaces/ISettingsManager.cs @@ -0,0 +1,16 @@ +using NewWorldCompanion.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NewWorldCompanion.Interfaces +{ + public interface ISettingsManager + { + public SettingsNWC Settings { get; } + + public void SaveSettings(); + } +} diff --git a/NewWorldCompanion.Services/ScreenProcessHandler.cs b/NewWorldCompanion.Services/ScreenProcessHandler.cs index 4a83f50..370689d 100644 --- a/NewWorldCompanion.Services/ScreenProcessHandler.cs +++ b/NewWorldCompanion.Services/ScreenProcessHandler.cs @@ -19,14 +19,9 @@ namespace NewWorldCompanion.Services public class ScreenProcessHandler : IScreenProcessHandler { private readonly IEventAggregator _eventAggregator; + private readonly ISettingsManager _settingsManager; private readonly IScreenCaptureHandler _screenCaptureHandler; - private int _areaLower; - private int _areaUpper; - private int _hysteresisLower; - private int _hysteresisUpper; - private int _thresholdMin; - private int _thresholdMax; private bool _isBusy = false; private Bitmap? _capturedImage = null; private Bitmap? _processedImage = null; @@ -36,22 +31,15 @@ public class ScreenProcessHandler : IScreenProcessHandler #region Constructor - public ScreenProcessHandler(IEventAggregator eventAggregator, IScreenCaptureHandler screenCaptureHandler) + public ScreenProcessHandler(IEventAggregator eventAggregator, ISettingsManager settingsManager, IScreenCaptureHandler screenCaptureHandler) { // Init IEventAggregator _eventAggregator = eventAggregator; _eventAggregator.GetEvent().Subscribe(HandleScreenCaptureReadyEvent); // Init services + _settingsManager = settingsManager; _screenCaptureHandler = screenCaptureHandler; - - // Restore defaults - AreaLower = EmguConstants.AreaLower; - AreaUpper = EmguConstants.AreaUpper; - HysteresisLower = EmguConstants.HysteresisLower; - HysteresisUpper = EmguConstants.HysteresisUpper; - ThresholdMin = EmguConstants.ThresholdMin; - ThresholdMax = EmguConstants.ThresholdMax; } #endregion @@ -60,12 +48,12 @@ public ScreenProcessHandler(IEventAggregator eventAggregator, IScreenCaptureHand #region Properties - public int AreaLower { get => _areaLower; set => _areaLower = value; } - public int AreaUpper { get => _areaUpper; set => _areaUpper = value; } - public int HysteresisLower { get => _hysteresisLower; set => _hysteresisLower = value; } - public int HysteresisUpper { get => _hysteresisUpper; set => _hysteresisUpper = value; } - public int ThresholdMin { get => _thresholdMin; set => _thresholdMin = value; } - public int ThresholdMax { get => _thresholdMax; set => _thresholdMax = value; } + public int AreaLower { get => _settingsManager.Settings.EmguAreaLower; } + public int AreaUpper { get => _settingsManager.Settings.EmguAreaUpper; } + public int HysteresisLower { get => _settingsManager.Settings.EmguHysteresisLower; } + public int HysteresisUpper { get => _settingsManager.Settings.EmguHysteresisUpper; } + public int ThresholdMin { get => _settingsManager.Settings.EmguThresholdMin; } + public int ThresholdMax { get => _settingsManager.Settings.EmguThresholdMax; } public Bitmap? ProcessedImage { get => _processedImage; set => _processedImage = value; } public Bitmap? RoiImage { get => _roiImage; set => _roiImage = value; } public Bitmap? OcrImage { get => _roiImage; set => _roiImage = value; } diff --git a/NewWorldCompanion.Services/SettingsManager.cs b/NewWorldCompanion.Services/SettingsManager.cs new file mode 100644 index 0000000..586e936 --- /dev/null +++ b/NewWorldCompanion.Services/SettingsManager.cs @@ -0,0 +1,74 @@ +using NewWorldCompanion.Entities; +using NewWorldCompanion.Interfaces; +using Prism.Events; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace NewWorldCompanion.Services +{ + public class SettingsManager : ISettingsManager + { + private readonly IEventAggregator _eventAggregator; + + private SettingsNWC _settings = new SettingsNWC(); + + // Start of Constructor region + + #region Constructor + + public SettingsManager(IEventAggregator eventAggregator) + { + // Init IEventAggregator + _eventAggregator = eventAggregator; + + // Init settings + LoadSettings(); + } + + #endregion + + // Start of Properties region + + #region Properties + + public SettingsNWC Settings { get => _settings; set => _settings = value; } + + #endregion + + // Start of Events region + + #region Events + + #endregion + + // Start of Methods region + #region Methods + + private void LoadSettings() + { + string fileName = "Settings.json"; + if (File.Exists(fileName)) + { + using FileStream stream = File.OpenRead(fileName); + _settings = JsonSerializer.Deserialize(stream) ?? new SettingsNWC(); + } + + SaveSettings(); + } + + public void SaveSettings() + { + string fileName = "Settings.json"; + using FileStream stream = File.Create(fileName); + var options = new JsonSerializerOptions { WriteIndented = true }; + JsonSerializer.Serialize(stream, _settings, options); + } + + #endregion + } +} diff --git a/NewWorldCompanion/App.xaml.cs b/NewWorldCompanion/App.xaml.cs index cadf218..eef3355 100644 --- a/NewWorldCompanion/App.xaml.cs +++ b/NewWorldCompanion/App.xaml.cs @@ -26,6 +26,7 @@ protected override void RegisterTypes(IContainerRegistry containerRegistry) containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); } protected override Window CreateShell() diff --git a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs index 4a49bb1..6040855 100644 --- a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs +++ b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs @@ -12,11 +12,10 @@ namespace NewWorldCompanion.ViewModels.Tabs.Debug public class DebugScreenOCRViewModel : BindableBase { private readonly IEventAggregator _eventAggregator; + private readonly ISettingsManager _settingsManager; private readonly IScreenProcessHandler _screenProcessHandler; private readonly IOcrHandler _ocrHandler; - private int _thresholdMin; - private int _thresholdMax; private string _itemName = string.Empty; private BitmapSource? _ocrImage = null; @@ -24,7 +23,7 @@ public class DebugScreenOCRViewModel : BindableBase #region Constructor - public DebugScreenOCRViewModel(IEventAggregator eventAggregator, IScreenProcessHandler screenProcessHandler, IOcrHandler ocrHandler) + public DebugScreenOCRViewModel(IEventAggregator eventAggregator, ISettingsManager settingsManager, IScreenProcessHandler screenProcessHandler, IOcrHandler ocrHandler) { // Init IEventAggregator _eventAggregator = eventAggregator; @@ -32,15 +31,12 @@ public DebugScreenOCRViewModel(IEventAggregator eventAggregator, IScreenProcessH _eventAggregator.GetEvent().Subscribe(HandleOcrTextReadyEvent); // Init services + _settingsManager = settingsManager; _screenProcessHandler = screenProcessHandler; _ocrHandler = ocrHandler; // Init View commands RestoreDefaultsCommand = new DelegateCommand(RestoreDefaultsExecute); - - // Restore defaults - ThresholdMin = EmguConstants.ThresholdMin; - ThresholdMax = EmguConstants.ThresholdMax; } #endregion @@ -53,21 +49,21 @@ public DebugScreenOCRViewModel(IEventAggregator eventAggregator, IScreenProcessH public int ThresholdMin { - get => _thresholdMin; + get => _settingsManager.Settings.EmguThresholdMin; set { - _thresholdMin = value; - _screenProcessHandler.ThresholdMin = value; + _settingsManager.Settings.EmguThresholdMin = value; + _settingsManager.SaveSettings(); RaisePropertyChanged(nameof(ThresholdMin)); } } public int ThresholdMax { - get => _thresholdMax; + get => _settingsManager.Settings.EmguThresholdMax; set { - _thresholdMax = value; - _screenProcessHandler.ThresholdMax = value; + _settingsManager.Settings.EmguThresholdMax = value; + _settingsManager.SaveSettings(); RaisePropertyChanged(nameof(ThresholdMax)); } } @@ -123,8 +119,8 @@ private void HandleOcrTextReadyEvent() private void RestoreDefaultsExecute() { - ThresholdMin = EmguConstants.ThresholdMin; - ThresholdMax = EmguConstants.ThresholdMax; + ThresholdMin = EmguConstants.DefaultThresholdMin; + ThresholdMax = EmguConstants.DefaultThresholdMax; } #endregion diff --git a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenProcessViewModel.cs b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenProcessViewModel.cs index b72a75e..a7d5bc1 100644 --- a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenProcessViewModel.cs +++ b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenProcessViewModel.cs @@ -12,12 +12,9 @@ namespace NewWorldCompanion.ViewModels.Tabs.Debug public class DebugScreenProcessViewModel : BindableBase { private readonly IEventAggregator _eventAggregator; + private readonly ISettingsManager _settingsManager; private readonly IScreenProcessHandler _screenProcessHandler; - private int _areaLower; - private int _areaUpper; - private int _hysteresisLower; - private int _hysteresisUpper; private BitmapSource? _processedImage = null; private BitmapSource? _roiImage = null; private BitmapSource? _ocrImage = null; @@ -26,7 +23,7 @@ public class DebugScreenProcessViewModel : BindableBase #region Constructor - public DebugScreenProcessViewModel(IEventAggregator eventAggregator, IScreenProcessHandler screenProcessHandler) + public DebugScreenProcessViewModel(IEventAggregator eventAggregator, ISettingsManager settingsManager, IScreenProcessHandler screenProcessHandler) { // Init IEventAggregator _eventAggregator = eventAggregator; @@ -35,16 +32,11 @@ public DebugScreenProcessViewModel(IEventAggregator eventAggregator, IScreenProc _eventAggregator.GetEvent().Subscribe(HandleOcrImageReadyEvent); // Init services + _settingsManager = settingsManager; _screenProcessHandler = screenProcessHandler; // Init View commands RestoreDefaultsCommand = new DelegateCommand(RestoreDefaultsExecute); - - // Restore defaults - AreaLower = EmguConstants.AreaLower; - AreaUpper = EmguConstants.AreaUpper; - HysteresisLower = EmguConstants.HysteresisLower; - HysteresisUpper = EmguConstants.HysteresisUpper; } #endregion @@ -57,41 +49,41 @@ public DebugScreenProcessViewModel(IEventAggregator eventAggregator, IScreenProc public int AreaLower { - get => _areaLower; + get => _settingsManager.Settings.EmguAreaLower; set { - _areaLower = value; - _screenProcessHandler.AreaLower = value; + _settingsManager.Settings.EmguAreaLower = value; + _settingsManager.SaveSettings(); RaisePropertyChanged(nameof(AreaLower)); } } public int AreaUpper { - get => _areaUpper; + get => _settingsManager.Settings.EmguAreaUpper; set { - _areaUpper = value; - _screenProcessHandler.AreaUpper = value; + _settingsManager.Settings.EmguAreaUpper = value; + _settingsManager.SaveSettings(); RaisePropertyChanged(nameof(AreaUpper)); } } public int HysteresisLower { - get => _hysteresisLower; + get => _settingsManager.Settings.EmguHysteresisLower; set { - _hysteresisLower = value; - _screenProcessHandler.HysteresisLower = value; + _settingsManager.Settings.EmguHysteresisLower = value; + _settingsManager.SaveSettings(); RaisePropertyChanged(nameof(HysteresisLower)); } } public int HysteresisUpper { - get => _hysteresisUpper; + get => _settingsManager.Settings.EmguHysteresisUpper; set { - _hysteresisUpper = value; - _screenProcessHandler.HysteresisUpper = value; + _settingsManager.Settings.EmguHysteresisUpper = value; + _settingsManager.SaveSettings(); RaisePropertyChanged(nameof(HysteresisUpper)); } } @@ -167,14 +159,12 @@ private void HandleOcrImageReadyEvent() private void RestoreDefaultsExecute() { - AreaLower = EmguConstants.AreaLower; - AreaUpper = EmguConstants.AreaUpper; - HysteresisLower = EmguConstants.HysteresisLower; - HysteresisUpper = EmguConstants.HysteresisUpper; + AreaLower = EmguConstants.DefaultAreaLower; + AreaUpper = EmguConstants.DefaultAreaUpper; + HysteresisLower = EmguConstants.DefaultHysteresisLower; + HysteresisUpper = EmguConstants.DefaultHysteresisUpper; } #endregion - - } } \ No newline at end of file