Explorar el Código

Some other types

flabbet hace 1 año
padre
commit
ddca8503ce

+ 8 - 0
samples/Sample3_Preferences/PreferencesSampleExtension.cs

@@ -18,11 +18,19 @@ public class PreferencesSampleExtension : WasmExtension
     public override void OnInitialized()
     {
         Api.Preferences.AddCallback<int>("HelloCount", (name, value) => Api.Logger.Log($"Hello count changed to {value}!"));
+        Api.Preferences.AddCallback<double>("TestDouble", (name, value) => Api.Logger.Log($"Test double changed to {value}!"));
+        Api.Preferences.AddCallback<string>("TestString", (name, value) => Api.Logger.Log($"Test string changed to {value}!"));
+        Api.Preferences.AddCallback<bool>("TestBool", (name, value) => Api.Logger.Log($"Test bool changed to {value}!"));
+        Api.Preferences.AddCallback<byte[]>("TestByteArray", (name, value) => Api.Logger.Log($"Test byte array changed to {value}!"));
         
         // Internally this preference will have name "yourCompany.Samples.Preferences:HelloCount".
         int helloCount = Api.Preferences.GetPreference<int>("HelloCount");
 
         Api.Preferences.UpdatePreference("HelloCount", helloCount + 1);
+        
+        Api.Preferences.UpdatePreference("TestDouble", 3.14);
+        Api.Preferences.UpdatePreference("TestString", "Hello, World!");
+        Api.Preferences.UpdatePreference("TestBool", true);
 
         // This will overwrite built-in PixiEditor preference. Extension must have WriteNonOwnedPreferences permission.
         // Prepending "PixiEditor:" to preference name will access built-in PixiEditor preferences. If you set it to other extension unique name,

+ 16 - 12
src/PixiEditor.Extensions.WasmRuntime/Api/Modules/PreferencesModule.cs

@@ -70,22 +70,26 @@ internal class PreferencesModule : ApiModule
         }
         
         string typeName = value.GetType().Name.ToLower();
-        var callbackAction = Extension.Instance.GetAction<int, int>($"{typeName}_preference_updated");
         int namePtr = Extension.WasmMemoryUtility.WriteString(preferenceName);
-        int valuePtr = WriteValue(value);
-        
-        callbackAction.Invoke(namePtr, valuePtr);
+        InvokePrimitiveCallbackAction(typeName, value, namePtr);
     }
 
-    private int WriteValue(object value)
+    private void InvokePrimitiveCallbackAction(string typeName, object value, int namePtr)
     {
-        return value switch
+        switch (value)
         {
-            int intValue => intValue,
-            bool boolValue => Extension.WasmMemoryUtility.WriteBoolean(boolValue),
-            float floatValue => Extension.WasmMemoryUtility.WriteSingle(floatValue),
-            double doubleValue => Extension.WasmMemoryUtility.WriteDouble(doubleValue),
-            _ => throw new ArgumentException("Unsupported preference value type.")
-        };
+            case int intValue:
+                Extension.Instance.GetAction<int, int>("int32_preference_updated").Invoke(namePtr, intValue);
+                break;
+            case bool boolValue:
+                Extension.Instance.GetAction<int, int>("bool_preference_updated").Invoke(namePtr, Convert.ToInt32(boolValue));
+                break;
+            case float floatValue:
+                Extension.Instance.GetAction<int, float>("float_preference_updated").Invoke(namePtr, floatValue);
+                break;
+            case double doubleValue:
+                Extension.Instance.GetAction<int, double>("double_preference_updated").Invoke(namePtr, doubleValue);
+                break;
+        }
     }
 }