Skip to content

Commit

Permalink
Added settings manager
Browse files Browse the repository at this point in the history
  • Loading branch information
josdemmers committed Feb 20, 2022
1 parent f14395a commit 28c3b04
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 77 deletions.
14 changes: 8 additions & 6 deletions NewWorldCompanion.Constants/EmguConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
21 changes: 21 additions & 0 deletions NewWorldCompanion.Entities/SettingsNWC.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
12 changes: 6 additions & 6 deletions NewWorldCompanion.Interfaces/IScreenProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
16 changes: 16 additions & 0 deletions NewWorldCompanion.Interfaces/ISettingsManager.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
30 changes: 9 additions & 21 deletions NewWorldCompanion.Services/ScreenProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ScreenCaptureReadyEvent>().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
Expand All @@ -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; }
Expand Down
74 changes: 74 additions & 0 deletions NewWorldCompanion.Services/SettingsManager.cs
Original file line number Diff line number Diff line change
@@ -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<SettingsNWC>(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
}
}
1 change: 1 addition & 0 deletions NewWorldCompanion/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected override void RegisterTypes(IContainerRegistry containerRegistry)
containerRegistry.RegisterSingleton<IScreenCaptureHandler, ScreenCaptureHandler>();
containerRegistry.RegisterSingleton<IScreenProcessHandler, ScreenProcessHandler>();
containerRegistry.RegisterSingleton<IOcrHandler, OcrHandler>();
containerRegistry.RegisterSingleton<ISettingsManager, SettingsManager>();
}

protected override Window CreateShell()
Expand Down
26 changes: 11 additions & 15 deletions NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,31 @@ 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;

// Start of Constructor region

#region Constructor

public DebugScreenOCRViewModel(IEventAggregator eventAggregator, IScreenProcessHandler screenProcessHandler, IOcrHandler ocrHandler)
public DebugScreenOCRViewModel(IEventAggregator eventAggregator, ISettingsManager settingsManager, IScreenProcessHandler screenProcessHandler, IOcrHandler ocrHandler)
{
// Init IEventAggregator
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<OcrImageReadyEvent>().Subscribe(HandleOcrImageReadyEvent);
_eventAggregator.GetEvent<OcrTextReadyEvent>().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
Expand All @@ -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));
}
}
Expand Down Expand Up @@ -123,8 +119,8 @@ private void HandleOcrTextReadyEvent()

private void RestoreDefaultsExecute()
{
ThresholdMin = EmguConstants.ThresholdMin;
ThresholdMax = EmguConstants.ThresholdMax;
ThresholdMin = EmguConstants.DefaultThresholdMin;
ThresholdMax = EmguConstants.DefaultThresholdMax;
}

#endregion
Expand Down
Loading

0 comments on commit 28c3b04

Please sign in to comment.