TMPCharacterBindings.cs
Runtime/Bindings/UGUI/TMPCharacterBindings.cs
1 #if MEATMOTION_TMP
2 using System;
3 using TMPro;
4 using UnityEngine;
5
6 namespace MeatMotion.Extensions
7 {
8 public delegate void TMPCharacterMotionUpdateAction<T>(T value, int index, ref TMPMotionCharacter character);
9
10 public struct TMPMotionCharacter
11 {
12 public Vector3 Position;
13 public Vector3 Scale;
14 public Quaternion Rotation;
15 public Color Color;
16 }
17
18 public static partial class TextMeshProMotionExtensions
19 {
20 public static MotionHandle BindToTMPChar(this FloatMotionBuilder builder,
21 TMP_Text text, int charIndex, TMPCharacterMotionUpdateAction<float> action)
22 => builder.AsMotionBuilder().BindToTMPChar(text, charIndex, action);
23
24 public static MotionHandle BindToTMPChar<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder,
25 TMP_Text text, int charIndex, TMPCharacterMotionUpdateAction<TValue> action)
26 where TValue : unmanaged
27 where TOptions : unmanaged, IMotionOptions
28 where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
29 => builder.BindCharacter(text, charIndex, false, action);
30
31 static MotionHandle BindCharacter(this FloatMotionBuilder builder, TMP_Text text, int charIndex,
32 bool positionOnly, TMPCharacterMotionUpdateAction<float> action)
33 => builder.AsMotionBuilder().BindCharacter(text, charIndex, positionOnly, action);
34
35 static MotionHandle BindCharacter<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder,
36 TMP_Text text, int charIndex, bool positionOnly, TMPCharacterMotionUpdateAction<TValue> action)
37 where TValue : unmanaged
38 where TOptions : unmanaged, IMotionOptions
39 where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
40 {
41 UIBinding.Require(text, nameof(text));
42 if (charIndex < 0) throw new ArgumentOutOfRangeException(nameof(charIndex));
43 if (action == null) throw new ArgumentNullException(nameof(action));
44 var binding = new CharacterBinding<TValue>(CharacterMesh.Acquire(text), charIndex, action, positionOnly);
45 try
46 {
47 var handle = builder.Bind(binding, static (value, state) => state.Write(value));
48 binding.Observe(handle);
49 return handle;
50 }
51 catch
52 {
53 binding.Release();
54 throw;
55 }
56 }
57
58 public static MotionHandle BindToTMPCharColor<TOptions, TAdapter>(this MotionBuilder<Color, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
59 where TOptions : unmanaged, IMotionOptions
60 where TAdapter : unmanaged, IMotionAdapter<Color, TOptions>
61 => builder.BindToTMPChar(text, charIndex, static (Color v, int _, ref TMPMotionCharacter c) => c.Color = v);
62
63 public static MotionHandle BindToTMPCharColorR(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
64 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Color.r = v);
65
66 public static MotionHandle BindToTMPCharColorR<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
67 where TOptions : unmanaged, IMotionOptions
68 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
69 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Color.r = v);
70
71 public static MotionHandle BindToTMPCharColorG(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
72 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Color.g = v);
73
74 public static MotionHandle BindToTMPCharColorG<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
75 where TOptions : unmanaged, IMotionOptions
76 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
77 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Color.g = v);
78
79 public static MotionHandle BindToTMPCharColorB(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
80 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Color.b = v);
81
82 public static MotionHandle BindToTMPCharColorB<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
83 where TOptions : unmanaged, IMotionOptions
84 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
85 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Color.b = v);
86
87 public static MotionHandle BindToTMPCharColorA(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
88 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Color.a = v);
89
90 public static MotionHandle BindToTMPCharColorA<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
91 where TOptions : unmanaged, IMotionOptions
92 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
93 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Color.a = v);
94
95 public static MotionHandle BindToTMPCharPosition<TOptions, TAdapter>(this MotionBuilder<Vector3, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
96 where TOptions : unmanaged, IMotionOptions
97 where TAdapter : unmanaged, IMotionAdapter<Vector3, TOptions>
98 => builder.BindCharacter(text, charIndex, true, static (Vector3 v, int _, ref TMPMotionCharacter c) => c.Position = v);
99
100 public static MotionHandle BindToTMPCharPositionX(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
101 => builder.BindCharacter(text, charIndex, true, static (float v, int _, ref TMPMotionCharacter c) => c.Position.x = v);
102
103 public static MotionHandle BindToTMPCharPositionX<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
104 where TOptions : unmanaged, IMotionOptions
105 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
106 => builder.BindCharacter(text, charIndex, true, static (float v, int _, ref TMPMotionCharacter c) => c.Position.x = v);
107
108 public static MotionHandle BindToTMPCharPositionY(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
109 => builder.BindCharacter(text, charIndex, true, static (float v, int _, ref TMPMotionCharacter c) => c.Position.y = v);
110
111 public static MotionHandle BindToTMPCharPositionY<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
112 where TOptions : unmanaged, IMotionOptions
113 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
114 => builder.BindCharacter(text, charIndex, true, static (float v, int _, ref TMPMotionCharacter c) => c.Position.y = v);
115
116 public static MotionHandle BindToTMPCharPositionZ(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
117 => builder.BindCharacter(text, charIndex, true, static (float v, int _, ref TMPMotionCharacter c) => c.Position.z = v);
118
119 public static MotionHandle BindToTMPCharPositionZ<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
120 where TOptions : unmanaged, IMotionOptions
121 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
122 => builder.BindCharacter(text, charIndex, true, static (float v, int _, ref TMPMotionCharacter c) => c.Position.z = v);
123
124 public static MotionHandle BindToTMPCharPositionXY<TOptions, TAdapter>(this MotionBuilder<Vector2, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
125 where TOptions : unmanaged, IMotionOptions
126 where TAdapter : unmanaged, IMotionAdapter<Vector2, TOptions>
127 => builder.BindCharacter(text, charIndex, true, static (Vector2 v, int _, ref TMPMotionCharacter c) =>
128 {
129 c.Position.x = v.x;
130 c.Position.y = v.y;
131 });
132
133 public static MotionHandle BindToTMPCharPositionYZ<TOptions, TAdapter>(this MotionBuilder<Vector2, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
134 where TOptions : unmanaged, IMotionOptions
135 where TAdapter : unmanaged, IMotionAdapter<Vector2, TOptions>
136 => builder.BindCharacter(text, charIndex, true, static (Vector2 v, int _, ref TMPMotionCharacter c) =>
137 {
138 c.Position.y = v.x;
139 c.Position.z = v.y;
140 });
141
142 public static MotionHandle BindToTMPCharPositionXZ<TOptions, TAdapter>(this MotionBuilder<Vector2, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
143 where TOptions : unmanaged, IMotionOptions
144 where TAdapter : unmanaged, IMotionAdapter<Vector2, TOptions>
145 => builder.BindCharacter(text, charIndex, true, static (Vector2 v, int _, ref TMPMotionCharacter c) =>
146 {
147 c.Position.x = v.x;
148 c.Position.z = v.y;
149 });
150
151 public static MotionHandle BindToTMPCharRotation<TOptions, TAdapter>(this MotionBuilder<Quaternion, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
152 where TOptions : unmanaged, IMotionOptions
153 where TAdapter : unmanaged, IMotionAdapter<Quaternion, TOptions>
154 => builder.BindToTMPChar(text, charIndex, static (Quaternion v, int _, ref TMPMotionCharacter c) => c.Rotation = v);
155
156 public static MotionHandle BindToTMPCharEulerAngles<TOptions, TAdapter>(this MotionBuilder<Vector3, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
157 where TOptions : unmanaged, IMotionOptions
158 where TAdapter : unmanaged, IMotionAdapter<Vector3, TOptions>
159 => builder.BindToTMPChar(text, charIndex, static (Vector3 v, int _, ref TMPMotionCharacter c) => c.Rotation = Quaternion.Euler(v));
160
161 public static MotionHandle BindToTMPCharEulerAnglesX(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
162 => builder.AsMotionBuilder().BindToTMPCharEulerAnglesX(text, charIndex);
163
164 public static MotionHandle BindToTMPCharEulerAnglesX<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
165 where TOptions : unmanaged, IMotionOptions
166 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
167 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) =>
168 {
169 var euler = c.Rotation.eulerAngles;
170 euler.x = v;
171 c.Rotation = Quaternion.Euler(euler);
172 });
173
174 public static MotionHandle BindToTMPCharEulerAnglesY(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
175 => builder.AsMotionBuilder().BindToTMPCharEulerAnglesY(text, charIndex);
176
177 public static MotionHandle BindToTMPCharEulerAnglesY<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
178 where TOptions : unmanaged, IMotionOptions
179 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
180 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) =>
181 {
182 var euler = c.Rotation.eulerAngles;
183 euler.y = v;
184 c.Rotation = Quaternion.Euler(euler);
185 });
186
187 public static MotionHandle BindToTMPCharEulerAnglesZ(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
188 => builder.AsMotionBuilder().BindToTMPCharEulerAnglesZ(text, charIndex);
189
190 public static MotionHandle BindToTMPCharEulerAnglesZ<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
191 where TOptions : unmanaged, IMotionOptions
192 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
193 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) =>
194 {
195 var euler = c.Rotation.eulerAngles;
196 euler.z = v;
197 c.Rotation = Quaternion.Euler(euler);
198 });
199
200 public static MotionHandle BindToTMPCharEulerAnglesXY<TOptions, TAdapter>(this MotionBuilder<Vector2, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
201 where TOptions : unmanaged, IMotionOptions
202 where TAdapter : unmanaged, IMotionAdapter<Vector2, TOptions>
203 => builder.BindToTMPChar(text, charIndex, static (Vector2 v, int _, ref TMPMotionCharacter c) =>
204 {
205 var euler = c.Rotation.eulerAngles;
206 euler.x = v.x;
207 euler.y = v.y;
208 c.Rotation = Quaternion.Euler(euler);
209 });
210
211 public static MotionHandle BindToTMPCharEulerAnglesYZ<TOptions, TAdapter>(this MotionBuilder<Vector2, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
212 where TOptions : unmanaged, IMotionOptions
213 where TAdapter : unmanaged, IMotionAdapter<Vector2, TOptions>
214 => builder.BindToTMPChar(text, charIndex, static (Vector2 v, int _, ref TMPMotionCharacter c) =>
215 {
216 var euler = c.Rotation.eulerAngles;
217 euler.y = v.x;
218 euler.z = v.y;
219 c.Rotation = Quaternion.Euler(euler);
220 });
221
222 public static MotionHandle BindToTMPCharEulerAnglesXZ<TOptions, TAdapter>(this MotionBuilder<Vector2, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
223 where TOptions : unmanaged, IMotionOptions
224 where TAdapter : unmanaged, IMotionAdapter<Vector2, TOptions>
225 => builder.BindToTMPChar(text, charIndex, static (Vector2 v, int _, ref TMPMotionCharacter c) =>
226 {
227 var euler = c.Rotation.eulerAngles;
228 euler.x = v.x;
229 euler.z = v.y;
230 c.Rotation = Quaternion.Euler(euler);
231 });
232
233 public static MotionHandle BindToTMPCharScale<TOptions, TAdapter>(this MotionBuilder<Vector3, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
234 where TOptions : unmanaged, IMotionOptions
235 where TAdapter : unmanaged, IMotionAdapter<Vector3, TOptions>
236 => builder.BindToTMPChar(text, charIndex, static (Vector3 v, int _, ref TMPMotionCharacter c) => c.Scale = v);
237
238 public static MotionHandle BindToTMPCharScaleX(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
239 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Scale.x = v);
240
241 public static MotionHandle BindToTMPCharScaleX<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
242 where TOptions : unmanaged, IMotionOptions
243 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
244 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Scale.x = v);
245
246 public static MotionHandle BindToTMPCharScaleY(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
247 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Scale.y = v);
248
249 public static MotionHandle BindToTMPCharScaleY<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
250 where TOptions : unmanaged, IMotionOptions
251 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
252 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Scale.y = v);
253
254 public static MotionHandle BindToTMPCharScaleZ(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
255 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Scale.z = v);
256
257 public static MotionHandle BindToTMPCharScaleZ<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
258 where TOptions : unmanaged, IMotionOptions
259 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
260 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Scale.z = v);
261
262 public static MotionHandle BindToTMPCharScaleXY<TOptions, TAdapter>(this MotionBuilder<Vector2, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
263 where TOptions : unmanaged, IMotionOptions
264 where TAdapter : unmanaged, IMotionAdapter<Vector2, TOptions>
265 => builder.BindToTMPChar(text, charIndex, static (Vector2 v, int _, ref TMPMotionCharacter c) =>
266 {
267 c.Scale.x = v.x;
268 c.Scale.y = v.y;
269 });
270
271 public static MotionHandle BindToTMPCharScaleYZ<TOptions, TAdapter>(this MotionBuilder<Vector2, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
272 where TOptions : unmanaged, IMotionOptions
273 where TAdapter : unmanaged, IMotionAdapter<Vector2, TOptions>
274 => builder.BindToTMPChar(text, charIndex, static (Vector2 v, int _, ref TMPMotionCharacter c) =>
275 {
276 c.Scale.y = v.x;
277 c.Scale.z = v.y;
278 });
279
280 public static MotionHandle BindToTMPCharScaleXZ<TOptions, TAdapter>(this MotionBuilder<Vector2, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
281 where TOptions : unmanaged, IMotionOptions
282 where TAdapter : unmanaged, IMotionAdapter<Vector2, TOptions>
283 => builder.BindToTMPChar(text, charIndex, static (Vector2 v, int _, ref TMPMotionCharacter c) =>
284 {
285 c.Scale.x = v.x;
286 c.Scale.z = v.y;
287 });
288
289 public static MotionHandle BindToTMPCharScaleXYZ(this FloatMotionBuilder builder, TMP_Text text, int charIndex)
290 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Scale = new Vector3(v, v, v));
291
292 public static MotionHandle BindToTMPCharScaleXYZ<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
293 where TOptions : unmanaged, IMotionOptions
294 where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
295 => builder.BindToTMPChar(text, charIndex, static (float v, int _, ref TMPMotionCharacter c) => c.Scale = new Vector3(v, v, v));
296 }
297 }
298 #endif