Changeset - 7117d2e703c8
[Not reviewed]
default
1 12 0
Tess Snider (Malkyne) - 3 years ago 2021-07-19 09:49:18
this@malkyne.org
Updated scanner for Tale 10, and fixed several bugs.
13 files changed with 181 insertions and 217 deletions:
0 comments (0 inline, 0 general)
Models/InterfaceSize.cs
Show inline comments
 
deleted file
Services/ReactionScannerService.cs
Show inline comments
...
 
@@ -16,105 +16,50 @@ namespace DesertPaintCodex.Services
 
        {
 
            Low,
 
            Normal,
 
            High,
 
            Excessive
 
        }
 
        
 
        // Swatch is 301x20 solid color, 1px darker left/top, 1px black outer left/top, 1px light right/bottom, 1px bright right/bottom
 
        // top-right and bottom-left are bright instead of black
 

	
 
        // Color bars are 4x302 solid color, 2px border on all sides (darker left/top, lighter bottom/right
 

	
 
        private static readonly int[] SWATCH_HEIGHT = 
 
        {
 
            24, // tiny
 
            24, // small
 
            24, // medium
 
            24, // large
 
            24 // huge
 
        };
 

	
 
        private static readonly int[] SWATCH_WIDTH =
 
        {
 
            306, // tiny
 
            306, // small
 
            306, // medium
 
            320, // large
 
            350 // huge
 
        };
 

	
 
        private static readonly int[] COLOR_BAR_WIDTH =
 
        {
 
            306, // tiny
 
            306, // small -- includes left and right borders
 
            306, // medium
 
            320, // large
 
            350 // huge
 
        };
 

	
 
        private static readonly int[] RED_BAR_SPACING =
 
        {
 
            32, // tiny
 
            32, // small
 
            32, // medium
 
            32, // large
 
            32 // huge
 
        };
 

	
 
        private static readonly int[] GREEN_BAR_SPACING =
 
        {
 
            42, // tiny
 
            42, // small
 
            42, // medium
 
            42, // large
 
            42 // huge
 
        };
 
        private const int SwatchHeight    = 24;
 
        private const int RedBarSpacing   = 32;
 
        private const int GreenBarSpacing = 42;
 
        private const int BlueBarSpacing  = 52;
 

	
 
        private static readonly int[] BLUE_BAR_SPACING =
 
        {
 
            52, // tiny
 
            52, // small
 
            52, // medium
 
            52, // large
 
            52 // huge
 
        };
 
        // width to test on ends of swatch (10% on either end)
 
        private static readonly int[] SWATCH_TEST_WIDTH =
 
        {
 
            26, // tiny
 
            26, // small
 
            26, // medium
 
            28, // large
 
            31  // huge
 
        };
 
        private const int MinSwatchWidth = 306;
 
        private const int MaxSwatchWidth = 411;
 

	
 
        private static ReactionScannerService? s_instance;
 
        public static ReactionScannerService Instance => s_instance ??= new ReactionScannerService();
 

	
 
        private int _swatchHeight    = SWATCH_HEIGHT[(int)Constants.DefaultInterfaceSize];
 
        private int _swatchWidth     = SWATCH_WIDTH[(int)Constants.DefaultInterfaceSize];
 
        private int _swatchTestWidth = SWATCH_TEST_WIDTH[(int)Constants.DefaultInterfaceSize];
 
        private int _colorBarWidth   = COLOR_BAR_WIDTH[(int)Constants.DefaultInterfaceSize];
 
        private int _redBarSpacing   = RED_BAR_SPACING[(int)Constants.DefaultInterfaceSize];
 
        private int _greenBarSpacing = GREEN_BAR_SPACING[(int)Constants.DefaultInterfaceSize];
 
        private int _blueBarSpacing  = BLUE_BAR_SPACING[(int)Constants.DefaultInterfaceSize];
 
        private int _swatchHeight    = 0;
 
        private int _minSwatchWidth  = 0;
 
        private int _maxSwatchWidth  = 0;
 
        private int _swatchWidth     = 0;
 
        private int _redBarSpacing   = 0;
 
        private int _greenBarSpacing = 0;
 
        private int _blueBarSpacing  = 0;
 
        private int _swatchTopBorder    = 0;
 
        private int _swatchBottomBorder = 0;
 
        private int _swatchLeftBorder   = 0;
 
        private int _swatchRightBorder  = 0;
 
        private int _swatchTestWidth = 0;
 

	
 
        private bool _lockedSwatchWidth = false;
 

	
 
        // Current Status
 
        public bool IsCaptured { get; private set; }
 

	
 
        private PaintColor _recordedColor = new PaintColor();
 
        private readonly PaintColor _recordedColor = new();
 
        public PaintColor RecordedColor {
 
            get => _recordedColor;
 
            private set => _recordedColor.Set(value);
 
        }
 

	
 
        private int _pixelMultiplier = 1;
 

	
 
        private InterfaceSize _interfaceSize = Constants.DefaultInterfaceSize;
 

	
 
        private bool _firstRun   = true;
 
        private int _lastSwatchX = -1;
 
        private int _lastSwatchY = -1;
 
        private int _screenX     = 0;
 
        private int _screenY     = 0;
 

	
...
 
@@ -148,17 +93,12 @@ namespace DesertPaintCodex.Services
 
            Debug.Assert(_targetBitmap != null);
 
        }
 

	
 
        public void RefreshFromSettings()
 
        {
 
            SettingsService.Get(SettingKey.PixelMultiplier, out _pixelMultiplier, Constants.DefaultPixelMultiplier);
 
            SettingsService.Get(SettingKey.InterfaceSize, out int interfaceSize, (int)Constants.DefaultInterfaceSize);
 
            if (Enum.IsDefined(typeof(InterfaceSize), interfaceSize))
 
            {
 
                _interfaceSize = (InterfaceSize)interfaceSize;
 
            }
 

	
 
            SettingsService.Get(SettingKey.ScreenX, out _screenX, Constants.DefaultScreenX);
 
            SettingsService.Get(SettingKey.ScreenY, out _screenY, Constants.DefaultScreenY);
 
            
 
            SettingsService.Get(SettingKey.ScreenWidth,  out int screenWidth, Constants.DefaultScreenWidth);
 
            SettingsService.Get(SettingKey.ScreenHeight, out int screenHeight, Constants.DefaultScreenHeight);
...
 
@@ -168,19 +108,24 @@ namespace DesertPaintCodex.Services
 
            UpdateSwatchSizes();
 
        }
 
        
 

	
 
        private void UpdateSwatchSizes()
 
        {
 
            _swatchHeight    = SWATCH_HEIGHT[(int)_interfaceSize]     * _pixelMultiplier;
 
            _swatchWidth     = SWATCH_WIDTH[(int)_interfaceSize]      * _pixelMultiplier;
 
            _colorBarWidth   = COLOR_BAR_WIDTH[(int)_interfaceSize]   * _pixelMultiplier;
 
            _redBarSpacing   = RED_BAR_SPACING[(int)_interfaceSize]   * _pixelMultiplier;
 
            _greenBarSpacing = GREEN_BAR_SPACING[(int)_interfaceSize] * _pixelMultiplier;
 
            _blueBarSpacing  = BLUE_BAR_SPACING[(int)_interfaceSize]  * _pixelMultiplier;
 
            _swatchTestWidth = SWATCH_TEST_WIDTH[(int)_interfaceSize] * _pixelMultiplier;
 
            _swatchWidth     = MinSwatchWidth * _pixelMultiplier;
 
            _swatchHeight    = SwatchHeight    * _pixelMultiplier;
 
            _redBarSpacing   = RedBarSpacing   * _pixelMultiplier;
 
            _greenBarSpacing = GreenBarSpacing * _pixelMultiplier;
 
            _blueBarSpacing  = BlueBarSpacing  * _pixelMultiplier;
 
            _swatchTopBorder    = 2 * _pixelMultiplier;
 
            _swatchBottomBorder = 2 * _pixelMultiplier;
 
            _swatchLeftBorder   = 2 * _pixelMultiplier;
 
            _swatchRightBorder  = 2 * _pixelMultiplier;
 
            _minSwatchWidth = MinSwatchWidth * _pixelMultiplier;
 
            _maxSwatchWidth = MaxSwatchWidth * _pixelMultiplier;
 
            _swatchTestWidth = (int)(0.1f * MinSwatchWidth * _pixelMultiplier);
 
        }
 

	
 
        public void CancelScan()
 
        {
 
            _canceler?.Cancel();
 
        }
...
 
@@ -198,32 +143,32 @@ namespace DesertPaintCodex.Services
 
        private bool IsPossibleSwatchSlice(Pixels pixels, int x, int y)
 
        {
 
            // 1.) Check if the top pixel is a dark pixel.
 
            if (!PixelColor.IsDark(pixels.ColorAt(x, y))) return false;
 
            
 
            // 2.) grab the swatch color 2 down from top border
 
            ColorMatcher.Color = pixels.ColorAt(x, y + 2);
 
            ColorMatcher.Color = pixels.ColorAt(x, y + _swatchTopBorder);
 

	
 
            // Scan the column from 2 below the top to 3 above the bottom to ensure the color matches
 
            for (int i = 2; i < (_swatchHeight-3); ++i)
 
            for (int i = 2; i < (_swatchHeight - 3); ++i)
 
            {
 
                if (!pixels.DoesPixelMatch(x, y + i, ColorMatcher.IsMatch)) return false;
 
            }
 

	
 
            return true;
 
        }
 

	
 
        private bool IsPossibleSwatchUpperLeft(Pixels pixels, int x, int y)
 
        {
 
            bool result = true;
 

	
 
            int swatchSolidWidth = _swatchWidth - 4;
 
            int swatchSolidHeight = _swatchHeight - 5; // 2 top and 3 bottom pixels are slightly different colors
 
            int swatchSolidLeftX = x + 2;
 
            int swatchSolidTopY = y + 2;
 
            int swatchSolidRightX = swatchSolidLeftX + swatchSolidWidth - 1;
 
            int swatchSolidWidth   = _swatchWidth  - _swatchLeftBorder - _swatchRightBorder;
 
            int swatchSolidHeight  = _swatchHeight - _swatchTopBorder - _swatchBottomBorder;
 
            int swatchSolidLeftX   = x + _swatchLeftBorder;
 
            int swatchSolidTopY    = y + _swatchTopBorder;
 
            int swatchSolidRightX  = swatchSolidLeftX + swatchSolidWidth - 1;
 
            int swatchSolidBottomY = swatchSolidTopY + swatchSolidHeight - 1;
 
            
 
            Color swatchColor = pixels.ColorAt(swatchSolidLeftX, swatchSolidTopY);
 
            ColorMatcher.Color = swatchColor;
 

	
 
            // Check the other 3 corners of the swatch size for color match
...
 
@@ -253,15 +198,35 @@ namespace DesertPaintCodex.Services
 
            if (!result)
 
            {
 
                // Box corners test failed
 
                WriteLog(LogVerbosity.High, "Failed to find left edge for potential swatch of color {2}, {3}, {4} at {0}, {1}", x, y, swatchColor.R, swatchColor.G, swatchColor.B);
 
                return false;
 
            }
 
            
 
            // Find the right side of the swatch, if necessary.
 
            if (!_lockedSwatchWidth)
 
            {
 
                int xCutoff = Math.Min(x + _maxSwatchWidth - _swatchRightBorder, _targetBitmap.Width);
 
                int newRight = swatchSolidRightX;
 
                for (int px = swatchSolidRightX; px < xCutoff; px++)
 
                {
 
                    if (pixels.DoesPixelMatch(px, swatchSolidTopY, ColorMatcher.IsMatch) &&
 
                        pixels.DoesPixelMatch(px, swatchSolidBottomY, ColorMatcher.IsMatch))
 
                    {
 
                        newRight = px;
 
                    }
 
                    else break;
 
                }
 

	
 
                swatchSolidRightX = newRight;
 
                _swatchWidth = swatchSolidRightX + _swatchRightBorder - x + _pixelMultiplier;
 
                WriteLog(LogVerbosity.High, "Setting new swatch width of {0}.", _swatchWidth);
 
            }
 

	
 
            // scan down the right and left sides
 
            for (int yOff = 1; yOff < (swatchSolidHeight - 1); ++yOff)
 
            for (int yOff = _pixelMultiplier; yOff < (swatchSolidHeight - _pixelMultiplier); ++yOff)
 
            {
 
                result &= pixels.DoesPixelMatch(swatchSolidLeftX, swatchSolidTopY + yOff, ColorMatcher.IsMatch);
 
                if (!result)
 
                {
 
                    testColor = pixels.ColorAt(swatchSolidLeftX, swatchSolidTopY + yOff);
 
                    break;
...
 
@@ -272,16 +237,16 @@ namespace DesertPaintCodex.Services
 
                    testColor = pixels.ColorAt(swatchSolidRightX, swatchSolidTopY + yOff);
 
                    break;
 
                }
 
            }
 
            if (!result)
 
            {
 
                WriteLog(LogVerbosity.Normal, "Failed to find left/right edges for potential swatch of color {2}, {3}, {4} at {0}, {1} [failed color = {5},{6},{7}]", x, y, swatchColor.R, swatchColor.G, swatchColor.B, testColor.R, testColor.G, testColor.B);
 
                WriteLog(LogVerbosity.Normal, "Failed to find left/right edges for potential swatch of color {2}, {3}, {4} at ({0}, {1}) [failed color = {5},{6},{7}]", x, y, swatchColor.R, swatchColor.G, swatchColor.B, testColor.R, testColor.G, testColor.B);
 
                return false;
 
            }
 
            for (int xOff = 1; xOff < (swatchSolidWidth - 1); ++xOff)
 
            for (int xOff = _pixelMultiplier; xOff < (swatchSolidWidth - _pixelMultiplier); ++xOff)
 
            {
 
                result &= pixels.DoesPixelMatch(swatchSolidLeftX + xOff, swatchSolidTopY, ColorMatcher.IsMatch);
 
                if (!result)
 
                {
 
                    testColor = pixels.ColorAt(swatchSolidLeftX + xOff, swatchSolidTopY);
 
                    break;
...
 
@@ -292,89 +257,90 @@ namespace DesertPaintCodex.Services
 
                    testColor = pixels.ColorAt(swatchSolidLeftX + xOff, swatchSolidBottomY);
 
                    break;
 
                }
 
            }
 
            if (!result)
 
            {
 
                WriteLog(LogVerbosity.Normal, "Failed to match upper/lower edges for potential swatch of color {2}, {3}, {4} at {0}, {1} [failed color = {5},{6},{7}]", x, y, swatchColor.R, swatchColor.G, swatchColor.B, testColor.R, testColor.G, testColor.B);
 
                WriteLog(LogVerbosity.Normal, "Failed to match upper/lower edges for potential swatch of color {2}, {3}, {4} at ({0}, {1}) [failed color = {5},{6},{7}]", x, y, swatchColor.R, swatchColor.G, swatchColor.B, testColor.R, testColor.G, testColor.B);
 
                return false;
 
            }
 

	
 

	
 
            // test the left edge for dark pixels -- the bottom-most pixel is bright now
 
            int i = 0;
 
            for (i = 1; result && i < _swatchHeight - 1; ++i)
 
            for (i = _pixelMultiplier; result && i < _swatchHeight - _pixelMultiplier; ++i)
 
            {
 
                result &= pixels.DoesPixelMatch(x, y + i, PixelColor.IsDark);
 
                if (!result) break;
 
            }
 
            if (!result)
 
            {
 
                // No dark border on the left side
 
                WriteLog(LogVerbosity.Normal, "Failed to find left border for potential swatch of color {2}, {3}, {4} at {0}, {1}", x, y, swatchColor.R, swatchColor.G, swatchColor.B);
 
                return false;
 
            }
 

	
 
            // test the dark top border and for papyrus above and below the swatch
 
            // test the dark top border and for background above and below the swatch
 
            bool borderError = false;
 
            int papyErrorCount = 0;
 
            for (i = 0; result && (i < _swatchWidth - 1); ++i)
 
            int bgErrorCount = 0;
 
            for (i = 0; result && (i < _swatchWidth - _pixelMultiplier); ++i)
 
            {
 
                bool isBorder = pixels.DoesPixelMatch(x + i, y, PixelColor.IsDark);
 
                result &= isBorder;
 
                if (!isBorder)
 
                {
 
                    WriteLog(LogVerbosity.Normal, "Probable swatch at {0},{1} failed upper border test at {2},{3}", x, y, x + i, y);
 
                    borderError = true;
 
                }
 

	
 
                // Checking along the top of the swatch for papyrus
 
                // Checking along the top of the swatch for background
 
                // The row just above is shaded, so check 2 above
 
                if (y > 1)
 
                {
 
                    bool isPapyrus = pixels.DoesPixelMatch(x + i, y - 2, PixelColor.IsPapyrus);
 
                    papyErrorCount += isPapyrus ? 0 : 1;
 
                    bool isUiBackground = pixels.DoesPixelMatch(x + i, y - _swatchTopBorder, PixelColor.IsUiBackground);
 
                    bgErrorCount += isUiBackground ? 0 : 1;
 

	
 
                }
 
                else
 
                {
 
                    ++papyErrorCount;
 
                    ++bgErrorCount;
 
                }
 

	
 
                // Checking along the bottom of the swatch for papyrus
 
                // Checking along the bottom of the swatch for background
 
                if (y < pixels.Height)
 
                {
 
                    bool isPapyrus = pixels.DoesPixelMatch(x + i, y + _swatchHeight, PixelColor.IsPapyrus);
 
                    papyErrorCount += isPapyrus ? 0 : 1;
 
                    bool isUiBackground = pixels.DoesPixelMatch(x + i, y + _swatchHeight, PixelColor.IsUiBackground);
 
                    bgErrorCount += isUiBackground ? 0 : 1;
 
                }
 
                else
 
                {
 
                    ++papyErrorCount;
 
                    ++bgErrorCount;
 
                }
 
            }
 

	
 
            result &= (papyErrorCount < (_swatchWidth / 20)); // allow up to 5% error rate checking for papy texture, because this seems to be inconsistent
 
            if (!result && ((i > (_swatchWidth*0.8)) || (papyErrorCount >= (_swatchWidth/20))))
 
            result &= (bgErrorCount < (_swatchWidth / 20)); // allow up to 5% error rate checking for background texture, because this seems to be inconsistent
 
            if (!result && ((i > (_swatchWidth * 0.8)) || (bgErrorCount >= (_swatchWidth / 20))))
 
            {
 
                if (!borderError && (papyErrorCount < _swatchWidth))
 
                if (!borderError && (bgErrorCount < _swatchWidth))
 
                {
 
                    WriteLog(LogVerbosity.Normal, "Found a potential swatch candidate of width {0} at {1},{2} that had {3} failures matching papyrus texture", i, x, y, papyErrorCount);
 
                    WriteLog(LogVerbosity.Normal, "Found a potential swatch candidate of width {0} at {1},{2} that had {3} failures matching background color", i, x, y, bgErrorCount);
 
                }
 
            }
 

	
 
            return result;
 
        }
 

	
 
        private bool TestPosition(int x, int y, Pixels pixels, ref PaintColor reactedColor)
 
        {
 
            // Check 4 corners of solid area and left/right solid bar areas
 
            bool foundSwatch = IsPossibleSwatchUpperLeft(pixels, x, y); // ((pixel_r < 0x46) && (pixel_g < 0x46) && (pixel_b < 0x46));
 
            bool foundSwatch = IsPossibleSwatchUpperLeft(pixels, x, y);
 
            if (foundSwatch)
 
            {
 
                WriteLog(LogVerbosity.Normal, "Found probable swatch at {0},{1} - checking border slices", x, y);
 
                int borderXOffset = 0;
 
                for (borderXOffset = 2; foundSwatch && (borderXOffset < _swatchTestWidth); ++borderXOffset)
 
                for (borderXOffset = _swatchLeftBorder; foundSwatch && (borderXOffset < _swatchTestWidth); ++borderXOffset)
 
                {
 
                    foundSwatch &= IsPossibleSwatchSlice(pixels, x + borderXOffset, y);
 
                    if (!foundSwatch)
 
                    {
 
                        WriteLog(LogVerbosity.Normal, "Failed slice test at {0},{1}", x + borderXOffset, y);
 
                        break;
...
 
@@ -385,23 +351,33 @@ namespace DesertPaintCodex.Services
 
                        WriteLog(LogVerbosity.Normal, "Failed slice test at {0},{1}", x + _swatchWidth - borderXOffset, y);
 
                        break;
 
                    }
 
                }
 
            }
 

	
 
            if (!foundSwatch) return false;
 
            if (!foundSwatch)
 
            {
 
                if (!_lockedSwatchWidth)
 
                {
 
                    // Reset our guess at swatch width.
 
                    _swatchWidth = MinSwatchWidth;
 
                }
 
                return false;
 
            }
 
            
 
            // WE FOUND THE SWATCH!
 
            // Now we know where the color bars are.
 
            _lockedSwatchWidth = true;
 
            int redPixelCount = pixels.LengthOfColorAt(x, y + _redBarSpacing, PixelColor.IsRed);
 
            reactedColor.Red = (byte)Math.Round((float)redPixelCount * 255f / (float)_colorBarWidth);
 
            reactedColor.Red = (byte)Math.Round(redPixelCount * 255f / _swatchWidth);
 

	
 
            int greenPixelCount = pixels.LengthOfColorAt(x, y + _greenBarSpacing, PixelColor.IsGreen);
 
            reactedColor.Green = (byte)Math.Round((float)greenPixelCount * 255f / (float)_colorBarWidth);
 
            reactedColor.Green = (byte)Math.Round(greenPixelCount * 255f / _swatchWidth);
 

	
 
            int bluePixelCount = pixels.LengthOfColorAt(x, y + _blueBarSpacing, PixelColor.IsBlue);
 
            reactedColor.Blue = (byte)Math.Round((float)bluePixelCount * 255f / (float)_colorBarWidth);
 
            reactedColor.Blue = (byte)Math.Round(bluePixelCount * 255f / _swatchWidth);
 
            WriteLog(LogVerbosity.Low, "Found the color swatch at {0}, {1}. Color={2} Red={3}px Green={4}px Blue={5}px", x, y, reactedColor, redPixelCount, greenPixelCount, bluePixelCount);
 
            return true;
 
        }
 

	
 
        public async Task<bool> CaptureReactionAsync(IProgress<float> progress)
 
        {
...
 
@@ -418,13 +394,13 @@ namespace DesertPaintCodex.Services
 
            using (var g = Graphics.FromImage(_targetBitmap))
 
            {
 
                Debug.WriteLine("Scan starting at [" + _screenX + ", " + _screenY + "]");
 
                g.CopyFromScreen(_screenX, _screenY, 0, 0, _targetBitmap.Size, CopyPixelOperation.SourceCopy);
 
            }
 
            
 
            _targetBitmap.Save(Path.Combine(ProfileManager.CurrentProfile?.Directory ?? "", "screenshot.png"), ImageFormat.Png);
 
            // _targetBitmap.Save(Path.Combine(ProfileManager.CurrentProfile?.Directory ?? "", "screenshot.png"), ImageFormat.Png);
 

	
 
            Pixels pixels = new(_targetBitmap, _pixelMultiplier);
 

	
 
            IsCaptured = false;
 
            if (!_firstRun)
 
            {
...
 
@@ -439,25 +415,25 @@ namespace DesertPaintCodex.Services
 
                _firstRun = true;
 
            }
 

	
 
            SettingsService.Get("Log.Verbosity", out var verbosityIdx, 1);
 
            _logVerbosity = (LogVerbosity)verbosityIdx;
 
            
 
            int patchTestSize = ((_swatchHeight - 5) / 2) - 1;
 
            int patchTestSize = ((_swatchHeight - _swatchTopBorder - _swatchBottomBorder) / 2) - 1;
 

	
 
            SettingsService.Get("ScanArea.Min.X", out var startX, 0);
 
            SettingsService.Get("ScanArea.Min.Y", out var startY, 0);
 
            SettingsService.Get("ScanArea.Max.X", out var endX,   _targetBitmap.Width);
 
            SettingsService.Get("ScanArea.Max.Y", out var endY,   _targetBitmap.Height);
 
            
 
            startX = Math.Max(2, Math.Min(startX, _targetBitmap.Width - 2));
 
            startY = Math.Max(2, Math.Min(startY, _targetBitmap.Height - 2));
 
            endX = Math.Min(_targetBitmap.Width - 2, Math.Max(2, endX)) - _colorBarWidth; //  + patchTestSize;
 
            endX = Math.Min(_targetBitmap.Width  - 2, Math.Max(2, endX)) - _minSwatchWidth; //  + patchTestSize;
 
            endY = Math.Min(_targetBitmap.Height - 2, Math.Max(2, endY)) - (_blueBarSpacing + 10); // + patchTestSize;
 

	
 
            Debug.WriteLine("startX: " + startX + " endX: " + endX + " startY: " + startY + " endY: " + endY);
 
            Debug.WriteLine("startX: " + startX + " endX: " + endX + " startY: " + startY + " endY: " + endY + " with patch test size of " + patchTestSize);
 
            
 
            int xSpan = endX - startX;
 
            int ySpan = endY - startY;
 
            int total = xSpan * ySpan;
 

	
 
            for (int roughX = startX; roughX < endX ; roughX += patchTestSize)
...
 
@@ -471,15 +447,15 @@ namespace DesertPaintCodex.Services
 
                    cancellToken.ThrowIfCancellationRequested();
 

	
 
                    if (!pixels.IsSolidPatchAt(roughX, roughY, patchTestSize, patchTestSize)) continue;
 
                    Color patchColor = pixels.ColorAt(roughX, roughY);
 

	
 
                    WriteLog(LogVerbosity.Excessive, "Found a solid patch of {2},{3},{4} at {0}, {1}", roughX, roughY, patchColor.R, patchColor.G, patchColor.B);
 
                    for (int x = Math.Max(0, roughX - patchTestSize); x < roughX; ++x)
 
                    for (int x = Math.Max(0, roughX - patchTestSize - _swatchLeftBorder + _pixelMultiplier); x < roughX; ++x)
 
                    {
 
                        for (int y = Math.Max(0, roughY - patchTestSize); y < roughY; ++y)
 
                        for (int y = Math.Max(0, roughY - patchTestSize - _swatchTopBorder + _pixelMultiplier); y < roughY; ++y)
 
                        {
 
                            WriteLog(LogVerbosity.Excessive, "Searching for potential swatch at {0},{1} after found square at {2},{3}", x, y, roughX, roughY);
 
                            
 
                            if (!TestPosition(x, y, pixels, ref reactedColor)) continue;
 
                            
 
                            RecordedColor = reactedColor;
...
 
@@ -509,14 +485,13 @@ namespace DesertPaintCodex.Services
 
            int r = reactedColor.Red   - expectedColor.Red   - reaction1.Red   - reaction2.Red;
 
            int g = reactedColor.Green - expectedColor.Green - reaction1.Green - reaction2.Green;
 
            int b = reactedColor.Blue  - expectedColor.Blue  - reaction1.Blue  - reaction2.Blue;
 
            
 
            return new Reaction(r, g, b);
 
        }
 

	
 

	
 
        
 
        public static Reaction CalculateReaction(PaintColor expectedColor, PaintColor reactedColor)
 
        {
 
            // A 2-reagent reaction.
 
            int r = reactedColor.Red   - expectedColor.Red;
 
            int g = reactedColor.Green - expectedColor.Green;
 
            int b = reactedColor.Blue  - expectedColor.Blue;
Services/SettingKey.cs
Show inline comments
 
namespace DesertPaintCodex.Services
 
{
 
    public static class SettingKey
 
    {
 
        public const string InterfaceSize   = "interfacesize";
 
        public const string PixelMultiplier = "pixelmultiplier";
 
        public const string ScreenIndex     = "screenindex";
 
        public const string ScreenX         = "screenx";
 
        public const string ScreenY         = "screeny";
 
        public const string ScreenWidth     = "screenwidth";
 
        public const string ScreenHeight    = "screenheight";
Util/Constants.cs
Show inline comments
 
using DesertPaintCodex.Models;
 

	
 
namespace DesertPaintCodex.Util
 
{
 
    public static class Constants
 
    {
 
        public const InterfaceSize DefaultInterfaceSize = InterfaceSize.Small;
 
        public static int DefaultPixelMultiplier = 1;
 
        public static int DefaultScreenWidth = 1920;
 
        public static int DefaultScreenHeight = 1080;
 
        public static int DefaultScreenX = 0;
 
        public static int DefaultScreenY = 0;
 

	
...
 
@@ -26,12 +25,8 @@ namespace DesertPaintCodex.Util
 
                }
 

	
 
                return _stubReactionTest;
 
            }
 
            
 
        }
 
            
 
            
 
            
 

	
 
    }
 
}
...
 
\ No newline at end of file
Util/PixelColor.cs
Show inline comments
...
 
@@ -3,13 +3,13 @@ using System.Drawing;
 
using DesertPaintCodex.Services;
 

	
 
namespace DesertPaintCodex.Util
 
{
 
    public class PixelColor
 
    {
 
        private const int ColorTolerance = 3;
 
        private const int ColorTolerance = 2;
 
        
 
        public static bool IsMatch(Color colorA, Color colorB)
 
        {
 
            return ((Math.Abs(colorA.R - colorB.R) <= ColorTolerance) &&
 
                (Math.Abs(colorA.G - colorB.G) <= ColorTolerance) &&
 
                (Math.Abs(colorA.B - colorB.B) <= ColorTolerance));
...
 
@@ -17,34 +17,26 @@ namespace DesertPaintCodex.Util
 

	
 
        public static bool IsDark(Color color)
 
        {
 
            return (color.R < 0x47) && (color.G < 0x47) && (color.B < 0x47);
 
        }
 

	
 
        public static bool IsPapyrus(Color color)
 
        public static bool IsUiBackground(Color color)
 
        {
 
            // red between 208 and 244
 
            // 240 and 255
 
            // green between 192 and 237
 
            // 223 and 250
 
            // blue between 145 and 205
 
            // 178 and 232
 
            //return ((r > 0xD0) && (g >= 0xC0) && (b >= 0x91)) &&
 
            //       ((r < 0xF4) && (g <= 0xED) && (b <= 0xCD));
 
            return ((color.R >= 0xD0) && (color.R <= 0xFF) && (color.G >= 0xC0) && (color.G <= 0xFA) && (color.B >= 0x91) && (color.B <= 0xE8));
 
            return (color.R is >= 0x27 and <= 0x2F) && (color.G is >= 0x3B and <= 0x42) && (color.B is >= 0x41 and <= 0x48);
 
        }
 

	
 
        public static bool IsRed(Color color)
 
        {
 
            return (color.R > 0x9F) && (color.G < 0x70) && (color.B < 0x70);
 
            return (color.R > 0x9D) && (color.G < 0x60) && (color.B < 0x60);
 
        }
 
        public static bool IsGreen(Color color)
 
        {
 
            return (color.R < 0x70) && (color.G > 0x9F) && (color.B < 0x70);
 
            return (color.R < 0x60) && (color.G > 0x9D) && (color.B < 0x60);
 
        }
 
        public static bool IsBlue(Color color)
 
        {
 
            return (color.R < 0x70) && (color.G < 0x70) && (color.B > 0x9F);
 
            return (color.R < 0x60) && (color.G < 0x60) && (color.B > 0x9D);
 
        }
 

	
 
    }
 
}
...
 
\ No newline at end of file
ViewModels/ExperimentLogViewModel.cs
Show inline comments
...
 
@@ -57,21 +57,24 @@ namespace DesertPaintCodex.ViewModels
 
                .Subscribe(_ => _testView.ReactionTest = GetSelectedReactionTest());
 

	
 
            _testView.ReactionTest = GetSelectedReactionTest();
 

	
 
            TestView.SaveReaction.Subscribe(_ => OnSaveReaction());
 
            TestView.ClearReaction.Subscribe(_ => OnClearReaction());
 
            TestView.FinalizeTestResults.Subscribe(_ => OnReactionResults());
 
        }
 

	
 
        private ReactionTest GetSelectedReactionTest()
 
        {
 
            int itemIndex = (SelectedList == 0) ? SelectedRemainingTest : SelectedCompletedTest;
 
            if (itemIndex < 0) return Constants.StubReactionTest;
 
            var list = _testLists[SelectedList];
 
            return (itemIndex >= list.Count) ? Constants.StubReactionTest : list[itemIndex];
 
        }
 
        
 
        #region Command Handlers
 

	
 
        private void OnSaveReaction()
 
        {
 
            ReactionTest test = TestView.ReactionTest;
 
            int oldPos = RemainingTests.IndexOf(test);
 
            
...
 
@@ -109,12 +112,30 @@ namespace DesertPaintCodex.ViewModels
 
            if (RemainingTests.Count == 1)
 
            {
 
                SelectedRemainingTest = 0;
 
            }
 
        }
 

	
 
        private void OnReactionResults()
 
        {
 
            ReactionTest? test = null;
 
            int selectedTest = SelectedRemainingTest;
 
            if (selectedTest >= 0)
 
            {
 
                test = RemainingTests[selectedTest];
 
            }
 
            ReactionTestService.PopulateRemainingTests(RemainingTests);
 
            if (test != null)
 
            {
 
                int newIndex = RemainingTests.IndexOf(test);
 
                SelectedRemainingTest = newIndex;
 
            }
 
        }
 
        
 
        #endregion
 

	
 
        private static void InsertTestIntoList(ReactionTest test, IList<ReactionTest> list)
 
        {
 
            int i;
 
            for (i = 0; i < list.Count; i++)
 
            {
 
                if (test.CompareTo(list[i]) < 0) break;
ViewModels/ReactionTestViewModel.cs
Show inline comments
...
 
@@ -39,12 +39,13 @@ namespace DesertPaintCodex.ViewModels
 
            this.WhenAnyValue(x => x.ReactionTest)
 
                .Subscribe(_ => UpdateDerivedState());
 
            
 
            ShowScreenSettingsDialog = new Interaction<ScreenSettingsViewModel, Unit>();
 
            SaveReaction = ReactiveCommand.Create(() => ReactionTest.SaveReaction());
 
            ClearReaction = ReactiveCommand.Create(() => ReactionTest.ClearReaction());
 
            FinalizeTestResults = ReactiveCommand.Create(Test);
 
        }
 

	
 
        private void UpdateDerivedState()
 
        {
 
            // There are more "reactive" ways to pull this off, but I don't have time to figure out all those recipes
 
            // right now.
...
 
@@ -72,12 +73,13 @@ namespace DesertPaintCodex.ViewModels
 
        public async void Analyze()
 
        {
 
            Debug.WriteLine("Analyze");
 
            try
 
            {
 
                await ReactionTest.StartScan();
 
                await FinalizeTestResults.Execute();
 
            }
 
            catch (OperationCanceledException)
 
            {
 
                Debug.WriteLine("Scan canceled.");
 
            }
 
        }
...
 
@@ -98,13 +100,19 @@ namespace DesertPaintCodex.ViewModels
 
        }
 

	
 
        public async Task ShowClipInfo()
 
        {
 
            await ShowInfoBox("Clipped Reactions", "The Pigment Lab is only capable of displaying color channel values in the 0-255 range. However, sometimes, your reactions will push one or more channels outside of that range. When that happens, the value will be clamped either to 0 or 255. In this case, we say that the reaction was \"Clipped.\"\n\nThis is nothing to panic about, though. We solve this issue by using a third (buffer) reagent to offset the extreme value, so that it falls within measurable range, similar to how we test Catalyst+Catalyst reactions. Desert Paint Codex will automatically do the math for this, but it does move clipped reactions towards the end of your test list, because you'll want any reactions between the buffer reagent and your two test reagents already known, prior to running your buffered test.");
 
        }
 

	
 
        private void Test()
 
        {
 
            Debug.WriteLine("Test complete");
 
        }
 
        
 
        public Interaction<ScreenSettingsViewModel, Unit> ShowScreenSettingsDialog { get; }
 

	
 
        public ReactiveCommand<Unit, Unit> ClearReaction { get; }
 
        public ReactiveCommand<Unit, Unit> SaveReaction { get; }
 
        public ReactiveCommand<Unit, Unit> FinalizeTestResults { get; }
 
    }
 
}
...
 
\ No newline at end of file
ViewModels/ScreenSettingsViewModel.cs
Show inline comments
 

 
using Avalonia.Controls;
 
using Avalonia.Platform;
 
using DesertPaintCodex.Models;
 
using DesertPaintCodex.Services;
 
using ReactiveUI;
 
using System.Collections.Generic;
 
using System.Collections.ObjectModel;
 
using System.Diagnostics;
 
using System.Reactive;
 
using Avalonia;
 
using Avalonia.Controls.ApplicationLifetimes;
 
using DesertPaintCodex.Util;
 

	
 
namespace DesertPaintCodex.ViewModels
 
{
 
    public class ScreenSettingsViewModel : ViewModelBase
 
    {
 
        public ObservableCollection<string> InterfaceSizes { get; } = new();
 
        public ObservableCollection<ScreenMetrics> Screens { get; } = new();
 

	
 
        private int _selectedInterfaceSize;
 
        public int SelectedInterfaceSize { get => _selectedInterfaceSize; private set => this.RaiseAndSetIfChanged(ref _selectedInterfaceSize, value); }
 

	
 
        
 
        private int _pixeMultiplier;
 
        public int PixelMultiplier { get => _pixeMultiplier; private set => this.RaiseAndSetIfChanged(ref _pixeMultiplier, value); }
 

	
 
        private int _screenIndex;
 
        public int ScreenIndex { get => _screenIndex; private set => this.RaiseAndSetIfChanged(ref _screenIndex, value); }
 
        
 
        public ScreenSettingsViewModel()
 
        {
 
            if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
 
            {
 
                IScreenImpl screen = desktop.MainWindow.PlatformImpl.Screen;
 
                for (int i = 0; i < (int)InterfaceSize.Count; i++)
 
                {
 
                    InterfaceSizes.Add(((InterfaceSize)i).ToString());
 
                }
 

	
 
                IReadOnlyList<Screen> screens = screen.AllScreens;
 

	
 
                for (int i = 0; i < screens.Count; i++)
 
                {
 
                    Screens.Add(new ScreenMetrics(screens[i].Bounds, screens[i].Primary));
 
                }
 
            
 
                SettingsService.Get(SettingKey.InterfaceSize,   out _selectedInterfaceSize, (int)Constants.DefaultInterfaceSize);
 
                
 
                SettingsService.Get(SettingKey.PixelMultiplier, out _pixeMultiplier, Constants.DefaultPixelMultiplier);
 
                SettingsService.Get(SettingKey.ScreenIndex,     out _screenIndex, 0);
 

	
 
                _screenIndex = System.Math.Min(_screenIndex, screens.Count - 1); // In case the user has fewer screens than last time.
 

	
 
                Save = ReactiveCommand.Create(() => {
 
                    SettingsService.Set(SettingKey.InterfaceSize,   _selectedInterfaceSize);
 
                    SettingsService.Set(SettingKey.PixelMultiplier, _pixeMultiplier);
 
                    SettingsService.Set(SettingKey.ScreenIndex,     _screenIndex);
 
                    SettingsService.Set(SettingKey.ScreenX,         screens[_screenIndex].Bounds.X);
 
                    SettingsService.Set(SettingKey.ScreenY,         screens[_screenIndex].Bounds.Y);
 
                    SettingsService.Set(SettingKey.ScreenWidth,     screens[_screenIndex].Bounds.Width);
 
                    SettingsService.Set(SettingKey.ScreenHeight,    screens[_screenIndex].Bounds.Height);
...
 
@@ -65,22 +52,15 @@ namespace DesertPaintCodex.ViewModels
 
                    ReactionScannerService.Instance.RefreshFromSettings();
 
                });
 
            }
 
            else
 
            {
 
                // Placeholder stuff.
 
                
 
                for (int i = 0; i < (int)InterfaceSize.Count; i++)
 
                {
 
                    InterfaceSizes.Add(((InterfaceSize)i).ToString());
 
                }
 
            
 
                Screens.Add(new ScreenMetrics(new Avalonia.PixelRect(0, 0, 1920, 1080), true));
 
                Screens.Add(new ScreenMetrics(new Avalonia.PixelRect(1920, 0, 1920, 1080), false));
 
            
 
                SelectedInterfaceSize = 0;
 
                
 
                PixelMultiplier       = 1;
 
                ScreenIndex           = 0;
 
                
 
                Save = ReactiveCommand.Create(() => { });
 
            }
 
        }
ViewModels/SelectProfileViewModel.cs
Show inline comments
 
using System.Reactive;
 
using ReactiveUI;
 
using System.Collections.Generic;
 
using System.Collections.ObjectModel;
 
using System.Diagnostics;
 
using DesertPaintCodex.Models;
 
using DesertPaintCodex.Services;
 
using DynamicData;
 

	
 
namespace DesertPaintCodex.ViewModels
 
{
 
    public class SelectProfileViewModel : ViewModelBase
 
    {
 
        public List<string> Profiles { get; private set; }
 
        public ObservableCollection<string> Profiles { get; } = new();
 

	
 
        private string? _selectedItem;
 
        public string? SelectedItem { get => _selectedItem; set => this.RaiseAndSetIfChanged(ref _selectedItem, value); }
 
        
 
        public SelectProfileViewModel()
 
        {
 
            Profiles = ProfileManager.GetProfileList();
 
            Profiles.AddRange(ProfileManager.GetProfileList());
 

	
 
            var okEnabled = this.WhenAnyValue(
 
                x => x.SelectedItem,
 
                x => !string.IsNullOrWhiteSpace(x));
 
            
 
            NewProfile = ReactiveCommand.Create(() => { });
ViewModels/WelcomeViewModel.cs
Show inline comments
...
 
@@ -27,31 +27,26 @@ namespace DesertPaintCodex.ViewModels
 
                ShowCreateProfile();
 
            }
 
        }
 

	
 
        private void ShowSelectProfile()
 
        {
 
            if (_selectProfileVM == null)
 
            {
 
                _selectProfileVM = new SelectProfileViewModel();
 
                _selectProfileVM.NewProfile.Subscribe(_ => ShowCreateProfile());
 
                Observable.Merge(_selectProfileVM.Ok, _selectProfileVM.Cancel)
 
                    .Take(1)
 
                    .InvokeCommand(FinishWelcome);
 
            }
 

	
 
            _selectProfileVM = new SelectProfileViewModel();
 
            _selectProfileVM.NewProfile.Subscribe(_ => ShowCreateProfile());
 
            Observable.Merge(_selectProfileVM.Ok, _selectProfileVM.Cancel)
 
                .Take(1)
 
                .InvokeCommand(FinishWelcome);
 
            
 
            ProfileActivity = _selectProfileVM;
 
        }
 

	
 
        private void ShowCreateProfile()
 
        {
 
            if (_createProfileVM == null)
 
            {
 
                _createProfileVM = new CreateProfileViewModel();
 
                Observable.Merge(_createProfileVM.Ok, _createProfileVM.Cancel).Subscribe(_ => ShowSelectProfile());
 
            }
 
            _createProfileVM = new CreateProfileViewModel();
 
            Observable.Merge(_createProfileVM.Ok, _createProfileVM.Cancel).Subscribe(_ => ShowSelectProfile());
 

	
 
            ProfileActivity = _createProfileVM;
 
        }
 

	
 
        public ReactiveCommand<Unit, Unit> FinishWelcome { get; }
 
    }
Views/ExperimentLogView.axaml
Show inline comments
...
 
@@ -60,16 +60,20 @@
 
        </DataTemplate>
 
    </UserControl.DataTemplates>
 

	
 
    <DockPanel Classes="Activity">
 
        <TabControl DockPanel.Dock="Left" Width="318" Classes="ListFilter" SelectedIndex="{Binding SelectedList}">
 
            <TabItem Header="Pending Tests">
 
                <ListBox Items="{Binding RemainingTests}" SelectedIndex="{Binding SelectedRemainingTest}" />
 
                <ListBox Name="RemainingList"
 
                         Items="{Binding RemainingTests}"
 
                         SelectedIndex="{Binding SelectedRemainingTest}"
 
                         SelectionChanged="RemainingSelectionChanged"/>
 
            </TabItem>
 
            <TabItem Header="Completed Tests">
 
                <ListBox Items="{Binding CompletedTests}" SelectedIndex="{Binding SelectedCompletedTest}" />
 
                <ListBox Items="{Binding CompletedTests}"
 
                         SelectedIndex="{Binding SelectedCompletedTest}" />
 
            </TabItem>
 
        </TabControl>
 
        
 
        <views:ReactionTestView DataContext="{Binding TestView}"/>
 
    </DockPanel>
 
    
Views/ExperimentLogView.axaml.cs
Show inline comments
 
using Avalonia.Markup.Xaml;
 
using Avalonia.Controls;
 
using Avalonia.Markup.Xaml;
 
using Avalonia.ReactiveUI;
 
using DesertPaintCodex.ViewModels;
 

	
 

	
 
namespace DesertPaintCodex.Views
 
{
 
    public class ExperimentLogView : ReactiveUserControl<ExperimentLogViewModel>
 
    {
 
        private readonly ListBox? _remainingList;
 
        
 
        public ExperimentLogView()
 
        {
 
            InitializeComponent();
 

	
 
            _remainingList = this.FindControl<ListBox>("RemainingList");
 
        }
 

	
 
        private void InitializeComponent()
 
        {
 
            AvaloniaXamlLoader.Load(this);
 
        }
 
        
 
        private void RemainingSelectionChanged(object? sender, SelectionChangedEventArgs e)
 
        {
 
            if (_remainingList == null) return;
 
            
 
            foreach (var item in e.AddedItems)
 
            {
 
                _remainingList.ScrollIntoView(item);
 
                return;
 
            }
 
        }
 
    }
 
}
Views/ScreenSettingsView.axaml
Show inline comments
...
 
@@ -21,37 +21,26 @@
 
      <TextBlock Text="Size" Grid.Row="0" Grid.Column="2"/>
 
    </Grid>
 
    
 
    <Button DockPanel.Dock="Bottom" Command="{Binding Save}">Ok</Button>
 
    
 
    <Grid DockPanel.Dock="Bottom" ColumnDefinitions="*,12,80,12,24" RowDefinitions="Auto,30,Auto" Margin="0, 0, 0, 30">
 
      <TextBlock VerticalAlignment="Center" TextWrapping="Wrap" TextAlignment="Right" Grid.Row="0" Grid.Column="0">Game UI Size</TextBlock>
 
      <ComboBox Items="{Binding InterfaceSizes}" SelectedIndex="{Binding SelectedInterfaceSize, Mode=TwoWay}" Grid.Row="0" Grid.Column="2" /> 
 
      <Border Classes="Help" Grid.Row="0" Grid.Column="4">
 
        <TextBlock>?</TextBlock>
 
        <ToolTip.Tip>
 
          <StackPanel Margin="5" Spacing="20" Orientation="Vertical">
 
            <TextBlock Classes="BlockHeader">Game UI Size</TextBlock>
 
            <TextBlock TextWrapping="Wrap">[Coming Soon]</TextBlock>
 
          </StackPanel>
 
        </ToolTip.Tip>
 
      </Border>
 
      
 
      <TextBlock VerticalAlignment="Center" TextWrapping="Wrap" TextAlignment="Right" Grid.Row="2" Grid.Column="0">
 
        Screen pixels to game pixels:
 
      </TextBlock>
 
      <NumericUpDown Minimum="1" Maximum="4" Increment="1" Value="{Binding PixelMultiplier}" Grid.Row="2" Grid.Column="2"/>
 
      <Border Classes="Help" Grid.Row="2" Grid.Column="4">
 
        <TextBlock>?</TextBlock>
 
        <ToolTip.Tip>
 
          <StackPanel Margin="5" Spacing="20" Orientation="Vertical">
 
            <TextBlock Classes="BlockHeader">Screen Pixels to Game Pixels</TextBlock>
 
            <TextBlock TextWrapping="Wrap">[Coming Soon]</TextBlock>
 
            <TextBlock TextWrapping="Wrap">If your screen is scaled at the system-level, you may need to adjust this number to measure UI elements correctly.</TextBlock>
 
          </StackPanel>
 
        </ToolTip.Tip>
 
      </Border></Grid>
 
      </Border>
 
    </Grid>
 

	
 
    <ListBox Items="{Binding Screens}" SelectedIndex="{Binding ScreenIndex, Mode=TwoWay}" Margin="0,4,0,20">
 
      <ListBox.ItemTemplate>
 
        <DataTemplate>
 
          <Grid ColumnDefinitions="80,10,80,10,60" RowDefinitions="Auto">
 
            <TextBlock Text="{Binding PositionToStr}" Grid.Row="0" Grid.Column="0" />
0 comments (0 inline, 0 general)