From 3e6181add6eaed47d2b46eaffee0585f8fbd675f Mon Sep 17 00:00:00 2001 From: Jos Date: Sun, 24 Apr 2022 15:42:17 +0200 Subject: [PATCH] Fix for change in nwmarketprices data format --- .../NwmarketpriceJson.cs | 4 +- NewWorldCompanion.Events/OcrEvent.cs | 3 ++ .../ScreenProcessEvent.cs | 4 ++ NewWorldCompanion.Helpers/DoubleConverter.cs | 27 ++++++++++ NewWorldCompanion.Helpers/ScreenCapture.cs | 44 +++++++++++++++ NewWorldCompanion.Interfaces/IOcrHandler.cs | 1 + .../IScreenCaptureHandler.cs | 2 + .../IScreenProcessHandler.cs | 1 + NewWorldCompanion.Services/OcrHandler.cs | 31 +++++++++++ NewWorldCompanion.Services/OverlayHandler.cs | 17 ++++-- NewWorldCompanion.Services/PriceManager.cs | 15 +++++- .../ScreenCaptureHandler.cs | 15 ++++++ .../ScreenProcessHandler.cs | 48 ++++++++++++++++- .../ViewModels/Tabs/CraftingViewModel.cs | 7 ++- .../Tabs/Debug/DebugScreenCaptureViewModel.cs | 12 +++++ .../Tabs/Debug/DebugScreenOCRViewModel.cs | 54 +++++++++++++++++++ .../Tabs/Debug/DebugScreenProcessViewModel.cs | 21 ++++++++ .../ViewModels/Tabs/StorageViewModel.cs | 2 + .../Tabs/Debug/DebugScreenCaptureView.xaml | 5 +- .../Views/Tabs/Debug/DebugScreenOCRView.xaml | 12 ++++- .../Tabs/Debug/DebugScreenProcessView.xaml | 5 +- NewWorldCompanion/common.props | 4 +- 22 files changed, 319 insertions(+), 15 deletions(-) create mode 100644 NewWorldCompanion.Helpers/DoubleConverter.cs diff --git a/NewWorldCompanion.Entities/NwmarketpriceJson.cs b/NewWorldCompanion.Entities/NwmarketpriceJson.cs index 62832ab..9c46eed 100644 --- a/NewWorldCompanion.Entities/NwmarketpriceJson.cs +++ b/NewWorldCompanion.Entities/NwmarketpriceJson.cs @@ -14,8 +14,8 @@ public class NwmarketpriceJson { public string item_name { get; set; } = string.Empty; public string last_checked { get; set; } = string.Empty; - public string recent_lowest_price { get; set; } = string.Empty; - //public double recent_lowest_price { get; set; } = 0.0; + //public string recent_lowest_price { get; set; } = string.Empty; + public double recent_lowest_price { get; set; } = 0.0; public List avg_graph_data { get; set; } = new List(); [JsonIgnore] diff --git a/NewWorldCompanion.Events/OcrEvent.cs b/NewWorldCompanion.Events/OcrEvent.cs index 44cfe99..a896b9a 100644 --- a/NewWorldCompanion.Events/OcrEvent.cs +++ b/NewWorldCompanion.Events/OcrEvent.cs @@ -5,4 +5,7 @@ namespace NewWorldCompanion.Events public class OcrTextReadyEvent : PubSubEvent { } + public class OcrTextCountReadyEvent : PubSubEvent + { + } } \ No newline at end of file diff --git a/NewWorldCompanion.Events/ScreenProcessEvent.cs b/NewWorldCompanion.Events/ScreenProcessEvent.cs index 02184cd..c874865 100644 --- a/NewWorldCompanion.Events/ScreenProcessEvent.cs +++ b/NewWorldCompanion.Events/ScreenProcessEvent.cs @@ -13,4 +13,8 @@ public class RoiImageReadyEvent : PubSubEvent public class OcrImageReadyEvent : PubSubEvent { } + + public class OcrImageCountReadyEvent : PubSubEvent + { + } } diff --git a/NewWorldCompanion.Helpers/DoubleConverter.cs b/NewWorldCompanion.Helpers/DoubleConverter.cs new file mode 100644 index 0000000..b43e49b --- /dev/null +++ b/NewWorldCompanion.Helpers/DoubleConverter.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace NewWorldCompanion.Helpers +{ + public class DoubleConverter : JsonConverter + { + public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options) => + writer.WriteNumberValue(value); + + public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => + reader.TokenType switch + { + JsonTokenType.True => 0.00, + JsonTokenType.False => 0.00, + JsonTokenType.String => double.TryParse(reader.GetString(), out var b) ? b : 0.00, + JsonTokenType.Number => reader.TryGetDouble(out double d) ? d : 0.00, + JsonTokenType.Null => 0.00, + _ => throw new JsonException(), + }; + } +} \ No newline at end of file diff --git a/NewWorldCompanion.Helpers/ScreenCapture.cs b/NewWorldCompanion.Helpers/ScreenCapture.cs index 2941982..d634539 100644 --- a/NewWorldCompanion.Helpers/ScreenCapture.cs +++ b/NewWorldCompanion.Helpers/ScreenCapture.cs @@ -94,6 +94,50 @@ public Bitmap GetScreenCapture(IntPtr windowHandle) return bitmap; } + public Bitmap? GetScreenCaptureMouseArea(IntPtr windowHandle) + { + Bitmap? bitmap = null; + + PInvoke.RECT region; + PInvoke.User32.GetWindowRect(windowHandle, out region); + + int width = 100; + int height = 50; + + PInvoke.User32.CURSORINFO cursorInfo = new PInvoke.User32.CURSORINFO(); + cursorInfo.cbSize = Marshal.SizeOf(cursorInfo); + PInvoke.User32.GetCursorInfo(ref cursorInfo); + int xPos = cursorInfo.ptScreenPos.x; + int yPos = cursorInfo.ptScreenPos.y; + + var desktopWindowHandle = PInvoke.User32.GetDesktopWindow(); + var windowDCHandle = PInvoke.User32.GetWindowDC(desktopWindowHandle); + var memoryDCHandle = PInvoke.Gdi32.CreateCompatibleDC(windowDCHandle); + var bitmapHandle = PInvoke.Gdi32.CreateCompatibleBitmap(windowDCHandle, width, height); + var bitmapOldHandle = PInvoke.Gdi32.SelectObject(memoryDCHandle, bitmapHandle); + + xPos = Math.Min(Math.Max(xPos - width / 2, region.left), region.right - width); + yPos = Math.Min(Math.Max(yPos, region.top), region.bottom - height); + bool status = PInvoke.Gdi32.BitBlt(memoryDCHandle.DangerousGetHandle(), 0, 0, width, height, windowDCHandle.DangerousGetHandle(), xPos, yPos, SRCCOPY | CAPTUREBLT); + + try + { + if (status) + { + bitmap = Image.FromHbitmap(bitmapHandle); + } + } + finally + { + PInvoke.Gdi32.SelectObject(memoryDCHandle, bitmapOldHandle); + PInvoke.Gdi32.DeleteObject(bitmapHandle); + PInvoke.Gdi32.DeleteDC(memoryDCHandle); + PInvoke.User32.ReleaseDC(desktopWindowHandle, windowDCHandle.DangerousGetHandle()); + } + + return bitmap; + } + public static BitmapSource? ImageSourceFromBitmap(Bitmap? bitmap) { if (bitmap != null) diff --git a/NewWorldCompanion.Interfaces/IOcrHandler.cs b/NewWorldCompanion.Interfaces/IOcrHandler.cs index f2c642e..8face63 100644 --- a/NewWorldCompanion.Interfaces/IOcrHandler.cs +++ b/NewWorldCompanion.Interfaces/IOcrHandler.cs @@ -3,5 +3,6 @@ public interface IOcrHandler { string OcrText { get; } + string OcrTextCount { get; } } } diff --git a/NewWorldCompanion.Interfaces/IScreenCaptureHandler.cs b/NewWorldCompanion.Interfaces/IScreenCaptureHandler.cs index d70690c..76c682b 100644 --- a/NewWorldCompanion.Interfaces/IScreenCaptureHandler.cs +++ b/NewWorldCompanion.Interfaces/IScreenCaptureHandler.cs @@ -6,6 +6,7 @@ namespace NewWorldCompanion.Interfaces public interface IScreenCaptureHandler { Bitmap? CurrentScreen { get; } + Bitmap? CurrentScreenMouseArea { get; } bool IsActive { get; set; } string MouseCoordinates { get; } string MouseCoordinatesScaled { get; } @@ -13,5 +14,6 @@ public interface IScreenCaptureHandler int OffsetY { get; } BitmapSource? ImageSourceFromScreenCapture(); + BitmapSource? ImageSourceFromScreenCaptureMouseArea(); } } diff --git a/NewWorldCompanion.Interfaces/IScreenProcessHandler.cs b/NewWorldCompanion.Interfaces/IScreenProcessHandler.cs index 4bcbe74..9e361dd 100644 --- a/NewWorldCompanion.Interfaces/IScreenProcessHandler.cs +++ b/NewWorldCompanion.Interfaces/IScreenProcessHandler.cs @@ -14,6 +14,7 @@ public interface IScreenProcessHandler Bitmap? ProcessedImage { get; } Bitmap? RoiImage { get; } Bitmap? OcrImage { get; } + Bitmap? OcrImageCount { get; } int OverlayX { get; } int OverlayY { get; } diff --git a/NewWorldCompanion.Services/OcrHandler.cs b/NewWorldCompanion.Services/OcrHandler.cs index aa1bc2a..c8eba19 100644 --- a/NewWorldCompanion.Services/OcrHandler.cs +++ b/NewWorldCompanion.Services/OcrHandler.cs @@ -22,6 +22,7 @@ public class OcrHandler : IOcrHandler private bool _isBusy = false; private string _ocrText = string.Empty; + private string _ocrTextCount = string.Empty; // Start of Constructor region @@ -32,6 +33,7 @@ public OcrHandler(IEventAggregator eventAggregator, INewWorldDataStore newWorldD // Init IEventAggregator _eventAggregator = eventAggregator; _eventAggregator.GetEvent().Subscribe(HandleOcrImageReadyEvent); + _eventAggregator.GetEvent().Subscribe(HandleOcrImageCountReadyEvent); // Init services _newWorldDataStore = newWorldDataStore; @@ -48,6 +50,7 @@ public OcrHandler(IEventAggregator eventAggregator, INewWorldDataStore newWorldD #region Properties public string OcrText { get => _ocrText; set => _ocrText = value; } + public string OcrTextCount { get => _ocrTextCount; set => _ocrTextCount = value; } #endregion @@ -86,6 +89,34 @@ private void HandleOcrImageReadyEvent() } } + private void HandleOcrImageCountReadyEvent() + { + if (!_isBusy) + { + _isBusy = true; + if (_screenProcessHandler.OcrImage != null) + { + try + { + Image image = Image.FromFile(@"ocrimages\itemcount.png"); + Tesseract tesseract = new Tesseract(); + string ocrText = tesseract.Read(image).Trim().Replace('\n', ' '); + OcrTextCount = ocrText; + + image.Dispose(); + tesseract.Dispose(); + + _eventAggregator.GetEvent().Publish(); + } + catch (Exception) + { + _isBusy = false; + } + } + _isBusy = false; + } + } + #endregion // Start of Methods region diff --git a/NewWorldCompanion.Services/OverlayHandler.cs b/NewWorldCompanion.Services/OverlayHandler.cs index b223699..e22f644 100644 --- a/NewWorldCompanion.Services/OverlayHandler.cs +++ b/NewWorldCompanion.Services/OverlayHandler.cs @@ -6,6 +6,7 @@ using Prism.Events; using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -128,6 +129,7 @@ private void DrawGraphics(object? sender, DrawGraphicsEventArgs e) private void DrawGraphicsItem(DrawGraphicsEventArgs e, string itemName) { NwmarketpriceJson nwmarketpriceJson = _priceManager.GetPriceData(itemName); + NumberStyles style = NumberStyles.AllowDecimalPoint; string infoItemName = itemName; string infoPrice = "Loading..."; @@ -137,9 +139,12 @@ private void DrawGraphicsItem(DrawGraphicsEventArgs e, string itemName) { string recentLowestPriceAvgList = nwmarketpriceJson.RecentLowestPriceAvg; - infoPrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ? - nwmarketpriceJson.recent_lowest_price : - $"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})"; + //infoPrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ? + // nwmarketpriceJson.recent_lowest_price : + // $"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})"; + infoPrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ? + decimal.Parse(nwmarketpriceJson.recent_lowest_price.ToString(), style, CultureInfo.InvariantCulture).ToString("F2"): + $"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})"; infoPriceAvg = string.IsNullOrWhiteSpace(recentLowestPriceAvgList) ? infoPriceAvg : $"{recentLowestPriceAvgList} lowest avg ({nwmarketpriceJson.last_checked})"; @@ -164,6 +169,7 @@ private void DrawGraphicsItem(DrawGraphicsEventArgs e, string itemName) private void DrawGraphicsRecipe(DrawGraphicsEventArgs e, CraftingRecipe craftingRecipe) { NwmarketpriceJson nwmarketpriceJson = _priceManager.GetPriceData(craftingRecipe.Localisation); + NumberStyles style = NumberStyles.AllowDecimalPoint; bool learnedStatus = craftingRecipe.Learned; string infoItemName = craftingRecipe.Localisation; @@ -175,8 +181,11 @@ private void DrawGraphicsRecipe(DrawGraphicsEventArgs e, CraftingRecipe crafting { string recentLowestPriceAvgList = nwmarketpriceJson.RecentLowestPriceAvg; + //infoPrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ? + // nwmarketpriceJson.recent_lowest_price : + // $"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})"; infoPrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ? - nwmarketpriceJson.recent_lowest_price : + decimal.Parse(nwmarketpriceJson.recent_lowest_price.ToString(), style, CultureInfo.InvariantCulture).ToString("F2") : $"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})"; infoPriceAvg = string.IsNullOrWhiteSpace(recentLowestPriceAvgList) ? infoPriceAvg : diff --git a/NewWorldCompanion.Services/PriceManager.cs b/NewWorldCompanion.Services/PriceManager.cs index 6f5bc63..5de997d 100644 --- a/NewWorldCompanion.Services/PriceManager.cs +++ b/NewWorldCompanion.Services/PriceManager.cs @@ -1,5 +1,6 @@ using NewWorldCompanion.Entities; using NewWorldCompanion.Events; +using NewWorldCompanion.Helpers; using NewWorldCompanion.Interfaces; using Prism.Events; using System; @@ -127,13 +128,23 @@ public void UpdatePriceData(string itemName) if (!string.IsNullOrWhiteSpace(json)) { - var nwmarketpriceJson = JsonSerializer.Deserialize(json); + // create the options + var options = new JsonSerializerOptions() + { + WriteIndented = true + }; + // register the converter + options.Converters.Add(new DoubleConverter()); + + var nwmarketpriceJson = JsonSerializer.Deserialize(json, options); if (nwmarketpriceJson != null) { Debug.WriteLine($"item_name: {nwmarketpriceJson.item_name}"); Debug.WriteLine($"recent_lowest_price: {nwmarketpriceJson.recent_lowest_price}"); Debug.WriteLine($"last_checked: {nwmarketpriceJson.last_checked}"); + nwmarketpriceJson.item_name = string.IsNullOrEmpty(nwmarketpriceJson.item_name) ? itemName : nwmarketpriceJson.item_name; + _priceCache[itemName] = nwmarketpriceJson; _eventAggregator.GetEvent().Publish(); } @@ -143,7 +154,7 @@ public void UpdatePriceData(string itemName) _priceCache[itemName] = new NwmarketpriceJson { item_name = itemName, - recent_lowest_price = "no data", + recent_lowest_price = 0.00, last_checked = "no data" }; } diff --git a/NewWorldCompanion.Services/ScreenCaptureHandler.cs b/NewWorldCompanion.Services/ScreenCaptureHandler.cs index 3c48190..84a5f5e 100644 --- a/NewWorldCompanion.Services/ScreenCaptureHandler.cs +++ b/NewWorldCompanion.Services/ScreenCaptureHandler.cs @@ -19,6 +19,7 @@ public class ScreenCaptureHandler : IScreenCaptureHandler private DispatcherTimer _captureTimer = new(); private DispatcherTimer _coordinatesTimer = new(); private Bitmap? _currentScreen = null; + private Bitmap? _currentScreenMouseArea = null; private int _delay = 100; private int _delayCoordinates = 100; private bool _isActive = true; @@ -59,6 +60,7 @@ public ScreenCaptureHandler(IEventAggregator eventAggregator) #region Properties public Bitmap? CurrentScreen { get => _currentScreen; set => _currentScreen = value; } + public Bitmap? CurrentScreenMouseArea { get => _currentScreenMouseArea; set => _currentScreenMouseArea = value; } public int Delay { get => _delay; set => _delay = value; } public int DelayCoordinates { get => _delayCoordinates; set => _delayCoordinates = value; } public bool IsActive { get => _isActive; set => _isActive = value; } @@ -118,6 +120,7 @@ private void ScreenCapture() if (windowHandle.ToInt64() > 0) { _currentScreen = _screenCapture.GetScreenCaptureMouse(windowHandle, ref _offsetX, ref _offsetY) ?? _currentScreen; + _currentScreenMouseArea = _screenCapture.GetScreenCaptureMouseArea(windowHandle) ?? _currentScreenMouseArea; _eventAggregator.GetEvent().Publish(); valid = true; @@ -178,6 +181,18 @@ private void UpdateCoordinates() } } + public BitmapSource? ImageSourceFromScreenCaptureMouseArea() + { + if (CurrentScreenMouseArea != null) + { + return Helpers.ScreenCapture.ImageSourceFromBitmap(CurrentScreenMouseArea); + } + else + { + return null; + } + } + #endregion } diff --git a/NewWorldCompanion.Services/ScreenProcessHandler.cs b/NewWorldCompanion.Services/ScreenProcessHandler.cs index 9d3666e..e8f87b5 100644 --- a/NewWorldCompanion.Services/ScreenProcessHandler.cs +++ b/NewWorldCompanion.Services/ScreenProcessHandler.cs @@ -24,8 +24,11 @@ public class ScreenProcessHandler : IScreenProcessHandler private bool _isBusy = false; private Bitmap? _capturedImage = null; + private Bitmap? _capturedImageCount = null; private Bitmap? _processedImage = null; private Bitmap? _roiImage = null; + private Bitmap? _ocrImage = null; + private Bitmap? _ocrImageCount = null; private int _overlayX = 0; private int _overlayY = 0; @@ -61,7 +64,9 @@ public ScreenProcessHandler(IEventAggregator eventAggregator, ISettingsManager s 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; } + //public Bitmap? OcrImage { get => _roiImage; set => _roiImage = value; } + public Bitmap? OcrImage { get => _ocrImage; set => _ocrImage = value; } + public Bitmap? OcrImageCount { get => _ocrImageCount; set => _ocrImageCount = value; } public int OverlayX { get => _overlayX; set => _overlayX = value; } public int OverlayY { get => _overlayY; set => _overlayY = value; } public int OverlayWidth { get => _overlayWidth; set => _overlayWidth = value; } @@ -79,8 +84,11 @@ private void HandleScreenCaptureReadyEvent() { _isBusy = true; _capturedImage = _screenCaptureHandler.CurrentScreen; + _capturedImageCount = _screenCaptureHandler.CurrentScreenMouseArea; Image imageCV = _capturedImage.ToImage(); + Image imageCountCV = _capturedImageCount.ToImage(); + ProcessImageCount(imageCountCV.Mat); ProcessImage(imageCV.Mat); _isBusy = false; @@ -214,6 +222,11 @@ private void ProcessImage(Mat img) } } + private void ProcessImageCount(Mat img) + { + ProcessImageCountOCR(img); + } + private void ProcessImageOCR(Mat img) { try @@ -247,6 +260,39 @@ private void ProcessImageOCR(Mat img) catch (Exception) { } } + private void ProcessImageCountOCR(Mat img) + { + try + { + Mat imgFilter = new Mat(img.Size, DepthType.Cv8U, 3); + + // Convert the image to grayscale + CvInvoke.CvtColor(img, imgFilter, ColorConversion.Bgr2Gray); + + // Apply threshold + //CvInvoke.Threshold(imgFilter, imgFilter, 0, 255, ThresholdType.Otsu); + //CvInvoke.Threshold(imgFilter, imgFilter, ThresholdMin, ThresholdMax, ThresholdType.Binary); + CvInvoke.Threshold(imgFilter, imgFilter, ThresholdMin, ThresholdMax, ThresholdType.BinaryInv); + + // Thinning and Skeletonization + //Mat element = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new System.Drawing.Size(2, 2), new System.Drawing.Point(-1, -1)); + //CvInvoke.Erode(imgFilter, imgFilter, element, new System.Drawing.Point(-1, -1), 1, BorderType.Constant, new MCvScalar(255, 255, 255)); + + // Filter out the noise + //CvInvoke.GaussianBlur(imgFilter, imgFilter, new System.Drawing.Size(0, 0), 1, 0, BorderType.Default); + + if (!Directory.Exists(@"ocrimages\")) + { + DirectoryInfo directoryInfo = Directory.CreateDirectory(@"ocrimages\"); + } + + OcrImageCount = imgFilter.ToBitmap(); + imgFilter.Save(@"ocrimages\itemcount.png"); + _eventAggregator.GetEvent().Publish(); + } + catch (Exception) { } + } + #endregion } } diff --git a/NewWorldCompanion/ViewModels/Tabs/CraftingViewModel.cs b/NewWorldCompanion/ViewModels/Tabs/CraftingViewModel.cs index 2c2155a..15164a9 100644 --- a/NewWorldCompanion/ViewModels/Tabs/CraftingViewModel.cs +++ b/NewWorldCompanion/ViewModels/Tabs/CraftingViewModel.cs @@ -9,6 +9,7 @@ using System.Collections.ObjectModel; using System.Diagnostics; using System.Drawing; +using System.Globalization; using System.IO; using System.Linq; using System.Reflection; @@ -258,12 +259,16 @@ public string SelectedCraftingRecipePrice { _priceManager.UpdatePriceData(SelectedCraftingRecipe.Localisation); NwmarketpriceJson nwmarketpriceJson = _priceManager.GetPriceData(SelectedCraftingRecipe.Localisation); + NumberStyles style = NumberStyles.AllowDecimalPoint; if (!string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name)) { string recentLowestPriceAvgList = nwmarketpriceJson.RecentLowestPriceAvg; + //_selectedCraftingRecipePrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ? + // nwmarketpriceJson.recent_lowest_price : + // $"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})"; _selectedCraftingRecipePrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ? - nwmarketpriceJson.recent_lowest_price : + decimal.Parse(nwmarketpriceJson.recent_lowest_price.ToString(), style, CultureInfo.InvariantCulture).ToString("F2") : $"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})"; } } diff --git a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenCaptureViewModel.cs b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenCaptureViewModel.cs index d90ba0f..5cb17ce 100644 --- a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenCaptureViewModel.cs +++ b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenCaptureViewModel.cs @@ -14,6 +14,7 @@ public class DebugScreenCaptureViewModel : BindableBase private readonly IScreenProcessHandler _screenProcessHandler; private BitmapSource? _capturedImage = null; + private BitmapSource? _capturedImageCount = null; private BitmapSource? _processedImage = null; private string _mouseCoordinates = string.Empty; private string _mouseCoordinatesScaled = string.Empty; @@ -51,6 +52,16 @@ public BitmapSource? CapturedImage } } + public BitmapSource? CapturedImageCount + { + get => _capturedImageCount; + set + { + _capturedImageCount = value; + RaisePropertyChanged(nameof(CapturedImageCount)); + } + } + public BitmapSource? ProcessedImage { get => _processedImage; @@ -93,6 +104,7 @@ private void HandleScreenCaptureReadyEvent() Application.Current?.Dispatcher?.Invoke(() => { CapturedImage = _screenCaptureHandler.ImageSourceFromScreenCapture(); + CapturedImageCount = _screenCaptureHandler.ImageSourceFromScreenCaptureMouseArea(); }); } diff --git a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs index 57f2ac2..72568b8 100644 --- a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs +++ b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs @@ -18,7 +18,9 @@ public class DebugScreenOCRViewModel : BindableBase private readonly IOcrHandler _ocrHandler; private string _itemName = string.Empty; + private string _itemCount = string.Empty; private BitmapSource? _ocrImage = null; + private BitmapSource? _ocrImageCount = null; // Start of Constructor region @@ -29,7 +31,9 @@ public DebugScreenOCRViewModel(IEventAggregator eventAggregator, ISettingsManage // Init IEventAggregator _eventAggregator = eventAggregator; _eventAggregator.GetEvent().Subscribe(HandleOcrImageReadyEvent); + _eventAggregator.GetEvent().Subscribe(HandleOcrImageCountReadyEvent); _eventAggregator.GetEvent().Subscribe(HandleOcrTextReadyEvent); + _eventAggregator.GetEvent().Subscribe(HandleOcrTextCountReadyEvent); // Init services _settingsManager = settingsManager; @@ -39,6 +43,7 @@ public DebugScreenOCRViewModel(IEventAggregator eventAggregator, ISettingsManage // Init View commands RestoreDefaultsCommand = new DelegateCommand(RestoreDefaultsExecute); CopyItemNameCommand = new DelegateCommand(CopyItemNameExecute); + CopyItemCountCommand = new DelegateCommand(CopyItemCountExecute); } #endregion @@ -49,6 +54,7 @@ public DebugScreenOCRViewModel(IEventAggregator eventAggregator, ISettingsManage public DelegateCommand RestoreDefaultsCommand { get; } public DelegateCommand CopyItemNameCommand { get; } + public DelegateCommand CopyItemCountCommand { get; } public int ThresholdMin { @@ -80,6 +86,17 @@ public string ItemName RaisePropertyChanged(nameof(ItemName)); } } + + public string ItemCount + { + get => _itemCount; + set + { + _itemCount = value; + RaisePropertyChanged(nameof(ItemCount)); + } + } + public BitmapSource? OcrImage { get => _ocrImage; @@ -90,6 +107,16 @@ public BitmapSource? OcrImage } } + public BitmapSource? OcrImageCount + { + get => _ocrImageCount; + set + { + _ocrImageCount = value; + RaisePropertyChanged(nameof(OcrImageCount)); + } + } + #endregion // Start of Events region @@ -105,6 +132,15 @@ private void HandleOcrImageReadyEvent() }); } + private void HandleOcrImageCountReadyEvent() + { + // As the view is accessed by the UI it will need to be created on the UI thread + Application.Current?.Dispatcher?.Invoke(() => + { + OcrImageCount = Helpers.ScreenCapture.ImageSourceFromBitmap(_screenProcessHandler.OcrImageCount); + }); + } + private void HandleOcrTextReadyEvent() { // As the view is accessed by the UI it will need to be created on the UI thread @@ -114,6 +150,15 @@ private void HandleOcrTextReadyEvent() }); } + private void HandleOcrTextCountReadyEvent() + { + // As the view is accessed by the UI it will need to be created on the UI thread + Application.Current?.Dispatcher?.Invoke(() => + { + ItemCount = _ocrHandler.OcrTextCount; + }); + } + #endregion // Start of Methods region @@ -135,6 +180,15 @@ private void CopyItemNameExecute() catch (Exception) { } } + private void CopyItemCountExecute() + { + try + { + System.Windows.Clipboard.SetText(ItemCount); + } + catch (Exception) { } + } + #endregion } } \ No newline at end of file diff --git a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenProcessViewModel.cs b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenProcessViewModel.cs index 8e124f3..01eca4a 100644 --- a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenProcessViewModel.cs +++ b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenProcessViewModel.cs @@ -19,6 +19,7 @@ public class DebugScreenProcessViewModel : BindableBase private BitmapSource? _processedImage = null; private BitmapSource? _roiImage = null; private BitmapSource? _ocrImage = null; + private BitmapSource? _ocrImageCount = null; // Start of Constructor region @@ -31,6 +32,7 @@ public DebugScreenProcessViewModel(IEventAggregator eventAggregator, ISettingsMa _eventAggregator.GetEvent().Subscribe(HandleProcessedImageReadyEvent); _eventAggregator.GetEvent().Subscribe(HandleRoiImageReadyEvent); _eventAggregator.GetEvent().Subscribe(HandleOcrImageReadyEvent); + _eventAggregator.GetEvent().Subscribe(HandleOcrImageCountReadyEvent); // Init services _settingsManager = settingsManager; @@ -122,6 +124,16 @@ public BitmapSource? OcrImage } } + public BitmapSource? OcrImageCount + { + get => _ocrImageCount; + set + { + _ocrImageCount = value; + RaisePropertyChanged(nameof(OcrImageCount)); + } + } + #endregion // Start of Events region @@ -155,6 +167,15 @@ private void HandleOcrImageReadyEvent() }); } + private void HandleOcrImageCountReadyEvent() + { + // As the view is accessed by the UI it will need to be created on the UI thread + Application.Current?.Dispatcher?.Invoke(() => + { + OcrImageCount = Helpers.ScreenCapture.ImageSourceFromBitmap(_screenProcessHandler.OcrImageCount); + }); + } + #endregion // Start of Methods region diff --git a/NewWorldCompanion/ViewModels/Tabs/StorageViewModel.cs b/NewWorldCompanion/ViewModels/Tabs/StorageViewModel.cs index e98db2e..1f06126 100644 --- a/NewWorldCompanion/ViewModels/Tabs/StorageViewModel.cs +++ b/NewWorldCompanion/ViewModels/Tabs/StorageViewModel.cs @@ -163,6 +163,7 @@ private void HandleOcrTextReadyEvent() if (item != null) { // TODO item count + //item.Count = int.TryParse(_ocrHandler.OcrTextCount, out int itemCount) ? itemCount : 0; item.Count = 0; } } @@ -174,6 +175,7 @@ private void HandleOcrTextReadyEvent() Items.Add(new Item() { // TODO item count + //Count = int.TryParse(_ocrHandler.OcrTextCount, out int itemCount) ? itemCount : 0; Count = 0, ItemID = itemId, Name = itemDefinition.Name, diff --git a/NewWorldCompanion/Views/Tabs/Debug/DebugScreenCaptureView.xaml b/NewWorldCompanion/Views/Tabs/Debug/DebugScreenCaptureView.xaml index c6466bf..2a1ae54 100644 --- a/NewWorldCompanion/Views/Tabs/Debug/DebugScreenCaptureView.xaml +++ b/NewWorldCompanion/Views/Tabs/Debug/DebugScreenCaptureView.xaml @@ -27,7 +27,10 @@ - + + + + diff --git a/NewWorldCompanion/Views/Tabs/Debug/DebugScreenOCRView.xaml b/NewWorldCompanion/Views/Tabs/Debug/DebugScreenOCRView.xaml index 89af5e6..daf9475 100644 --- a/NewWorldCompanion/Views/Tabs/Debug/DebugScreenOCRView.xaml +++ b/NewWorldCompanion/Views/Tabs/Debug/DebugScreenOCRView.xaml @@ -29,7 +29,10 @@ +