ReportingMotionExtensions.cs
Runtime/Bindings/ReportingMotionExtensions.cs
1 using System;
2 using UnityEngine;
3
4 namespace MeatMotion.Extensions
5 {
6 internal static class MotionTextFormat
7 {
8 internal static string Format<T>(string pattern, T value)
9 {
10 #if MEATMOTION_ZSTRING
11 return Cysharp.Text.ZString.Format(pattern, value);
12 #else
13 return string.Format(pattern, value);
14 #endif
15 }
16 }
17
18 public static class ProgressMotionExtensions
19 {
20 public static MotionHandle BindToProgress(this FloatMotionBuilder builder, IProgress<float> progress)
21 => builder.AsMotionBuilder().BindToProgress(progress);
22
23 public static MotionHandle BindToProgress<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder, IProgress<TValue> progress)
24 where TValue : unmanaged
25 where TOptions : unmanaged, IMotionOptions
26 where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
27 => builder.Bind(progress ?? throw new ArgumentNullException(nameof(progress)), static (v, target) => target.Report(v));
28 }
29
30 public static class LoggerMotionExtensions
31 {
32 public static MotionHandle BindToUnityLogger(this FloatMotionBuilder builder)
33 => builder.AsMotionBuilder().BindToUnityLogger();
34
35 public static MotionHandle BindToUnityLogger<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder)
36 where TValue : unmanaged
37 where TOptions : unmanaged, IMotionOptions
38 where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
39 => builder.Bind(static v => Debug.unityLogger.Log(v));
40
41 public static MotionHandle BindToUnityLogger(this FloatMotionBuilder builder, string format)
42 => builder.AsMotionBuilder().BindToUnityLogger(format);
43
44 public static MotionHandle BindToUnityLogger<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder, string format)
45 where TValue : unmanaged
46 where TOptions : unmanaged, IMotionOptions
47 where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
48 => builder.Bind(format, static (v, pattern) => Debug.unityLogger.Log(MotionTextFormat.Format(pattern, v)));
49
50 public static MotionHandle BindToUnityLogger(this FloatMotionBuilder builder, ILogger logger)
51 => builder.AsMotionBuilder().BindToUnityLogger(logger);
52
53 public static MotionHandle BindToUnityLogger<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder, ILogger logger)
54 where TValue : unmanaged
55 where TOptions : unmanaged, IMotionOptions
56 where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
57 => builder.Bind(logger ?? throw new ArgumentNullException(nameof(logger)), static (v, target) => target.Log(v));
58
59 public static MotionHandle BindToUnityLogger(this FloatMotionBuilder builder, ILogger logger, string format)
60 => builder.AsMotionBuilder().BindToUnityLogger(logger, format);
61
62 public static MotionHandle BindToUnityLogger<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder, ILogger logger, string format)
63 where TValue : unmanaged
64 where TOptions : unmanaged, IMotionOptions
65 where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
66 => builder.Bind(logger ?? throw new ArgumentNullException(nameof(logger)), format,
67 static (v, target, pattern) => target.Log(MotionTextFormat.Format(pattern, v)));
68 }
69 }