Bläddra i källkod

misc: chore: Pass rainbow color by reference in the event instead of passing around a packed int.

Evan Husted 1 år sedan
förälder
incheckning
71d8cfd232

+ 58 - 0
src/Ryujinx.Common/Helpers/RefEvent.cs

@@ -0,0 +1,58 @@
+using Gommon;
+using System.Collections.Generic;
+using System.Threading;
+
+namespace Ryujinx.Common.Helper
+{
+    public class RefEvent<T>
+    {
+        public delegate void Handler(ref T arg);
+        
+        private readonly Lock _subLock = new();
+        private readonly List<Handler> _subscriptions = [];
+    
+        public bool HasSubscribers
+        {
+            get
+            {
+                lock (_subLock)
+                    return _subscriptions.Count != 0;
+            }
+        }
+
+        public IReadOnlyList<Handler> Subscriptions
+        {
+            get
+            {
+                lock (_subLock)
+                    return _subscriptions;
+            }
+        }
+
+        public void Add(Handler subscriber)
+        {
+            Guard.Require(subscriber, nameof(subscriber));
+            lock (_subLock)
+                _subscriptions.Add(subscriber);
+        }
+
+        public void Remove(Handler subscriber)
+        {
+            Guard.Require(subscriber, nameof(subscriber));
+            lock (_subLock)
+                _subscriptions.Remove(subscriber);
+        }
+
+        public void Clear()
+        {
+            lock (_subLock)
+                _subscriptions.Clear();
+        }
+
+        public void Call(ref T arg)
+        {
+            foreach (Handler subscription in Subscriptions)
+                subscription(ref arg);
+        }
+    }
+}

+ 4 - 3
src/Ryujinx.Common/Utilities/Rainbow.cs

@@ -1,4 +1,5 @@
 using Gommon;
+using Ryujinx.Common.Helper;
 using System;
 using System.Drawing;
 using System.Threading;
@@ -55,7 +56,7 @@ namespace Ryujinx.Common.Utilities
             {
                 _color = HsbToRgb((_color.GetHue() + Speed) / 360);
             
-                _updatedHandler.Call(_color.ToArgb());
+                _updatedHandler.Call(ref _color);
             }
         }
 
@@ -67,13 +68,13 @@ namespace Ryujinx.Common.Utilities
                 _color = Color.Blue;
         }
 
-        public static event Action<int> Updated
+        public static event RefEvent<Color>.Handler Updated
         {
             add => _updatedHandler.Add(value);
             remove => _updatedHandler.Remove(value);
         }
 
-        private static readonly Event<int> _updatedHandler = new();
+        private static readonly RefEvent<Color> _updatedHandler = new();
 
         private static Color HsbToRgb(float hue, float saturation = 1, float brightness = 1)
         {

+ 1 - 1
src/Ryujinx.Input.SDL2/SDL2Gamepad.cs

@@ -111,7 +111,7 @@ namespace Ryujinx.Input.SDL2
             byte blue = packedRgb > 0 ? (byte)(packedRgb % 256) : (byte)0;
             
             if (SDL_GameControllerSetLED(_gamepadHandle, red, green, blue) != 0)
-                Logger.Error?.Print(LogClass.Hid, "LED is not supported on this game controller.");
+                Logger.Error?.Print(LogClass.Hid, "LED setting failed; probably in the middle of disconnecting.");
         }
 
         private GamepadFeaturesFlag GetFeaturesFlag()

+ 2 - 4
src/Ryujinx/UI/ViewModels/Input/ControllerInputViewModel.cs

@@ -1,12 +1,10 @@
 using Avalonia.Svg.Skia;
 using CommunityToolkit.Mvvm.ComponentModel;
-using CommunityToolkit.Mvvm.Input;
-using FluentAvalonia.UI.Controls;
-using Ryujinx.Ava.UI.Helpers;
 using Ryujinx.Ava.UI.Models.Input;
 using Ryujinx.Ava.UI.Views.Input;
 using Ryujinx.Common.Utilities;
 using Ryujinx.UI.Views.Input;
+using System.Drawing;
 
 namespace Ryujinx.Ava.UI.ViewModels.Input
 {
@@ -54,7 +52,7 @@ namespace Ryujinx.Ava.UI.ViewModels.Input
                 if (args.PropertyName is nameof(Config.UseRainbowLed))
                 {
                     if (Config is { UseRainbowLed: true, TurnOffLed: false, EnableLedChanging: true })
-                        Rainbow.Updated += color => ParentModel.SelectedGamepad.SetLed((uint)color);
+                        Rainbow.Updated += (ref Color color) => ParentModel.SelectedGamepad.SetLed((uint)color.ToArgb());
                     else
                     {
                         Rainbow.Reset();

+ 2 - 2
src/Ryujinx/UI/ViewModels/Input/InputViewModel.cs

@@ -20,10 +20,10 @@ using Ryujinx.Common.Configuration.Hid.Keyboard;
 using Ryujinx.Common.Logging;
 using Ryujinx.Common.Utilities;
 using Ryujinx.Input;
-using Ryujinx.Input.SDL2;
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
+using System.Drawing;
 using System.IO;
 using System.Linq;
 using System.Text.Json;
@@ -69,7 +69,7 @@ namespace Ryujinx.Ava.UI.ViewModels.Input
                 _selectedGamepad = value;
 
                 if (ConfigViewModel is ControllerInputViewModel { Config.UseRainbowLed: true })
-                    Rainbow.Updated += color => _selectedGamepad.SetLed((uint)color);
+                    Rainbow.Updated += (ref Color color) => _selectedGamepad.SetLed((uint)color.ToArgb());
                 
                 OnPropertiesChanged(nameof(HasLed), nameof(CanClearLed));
             }