Skip to content

Commit 1211b5d

Browse files
committed
improvements to animation pointer
1 parent 8ef6ca1 commit 1211b5d

File tree

4 files changed

+80
-20
lines changed

4 files changed

+80
-20
lines changed

src/Shared/_Extensions.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,42 @@ internal static string Replace(this string text, string oldText, string newText,
204204
if (comparison == StringComparison.Ordinal) return text.Replace(oldText, newText);
205205
throw new NotImplementedException();
206206
}
207+
208+
internal static int IndexOf(this string text, string value, StringComparison comparison)
209+
{
210+
switch(comparison)
211+
{
212+
case StringComparison.OrdinalIgnoreCase:
213+
case StringComparison.InvariantCultureIgnoreCase:
214+
text = text.ToUpperInvariant();
215+
value = value.ToUpperInvariant();
216+
break;
217+
case StringComparison.CurrentCultureIgnoreCase:
218+
text = text.ToUpper();
219+
value = value.ToUpper();
220+
break;
221+
}
222+
223+
return text.IndexOf(value);
224+
}
225+
226+
internal static int IndexOf(this string text, char value, StringComparison comparison)
227+
{
228+
switch (comparison)
229+
{
230+
case StringComparison.OrdinalIgnoreCase:
231+
case StringComparison.InvariantCultureIgnoreCase:
232+
text = text.ToUpperInvariant();
233+
value = char.ToUpperInvariant(value);
234+
break;
235+
case StringComparison.CurrentCultureIgnoreCase:
236+
text = text.ToUpper();
237+
value = char.ToUpper(value);
238+
break;
239+
}
240+
241+
return text.IndexOf(value);
242+
}
207243
#endif
208244

209245
#endregion

src/SharpGLTF.Core/Schema2/gltf.AnimationChannel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ void IChildOfList<Animation>.SetLogicalParent(Animation parent, int index)
8080
public string TargetPointerPath => this._target?.GetPointerPath() ?? null;
8181

8282
/// <summary>
83-
/// Gets the <see cref="Node"/> which property is to be bound with this animation.
84-
/// </summary>
85-
[Obsolete("Use TargetPointerPath whenever possible")]
83+
/// Gets the <see cref="Node"/> which property is to be bound with this animation,
84+
/// or NULL if the target is not a <see cref="Node"/>
85+
/// </summary>
8686
public Node TargetNode
8787
{
8888
get

src/SharpGLTF.Core/Schema2/gltf.AnimationChannelTarget.cs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,29 @@ void IChildOf<AnimationChannel>.SetLogicalParent(AnimationChannel parent)
6666

6767
#region API
6868

69+
/// <summary>
70+
/// Gets the index of the <see cref="Node"/> pointed by this animation,
71+
/// or -1 if it points to a target that is not a <see cref="Node"/>.
72+
/// </summary>
73+
/// <returns>A node index, or -1</returns>
6974
public int GetNodeIndex()
7075
{
7176
if (this._node != null) return this._node.Value;
7277

7378
if (_NodePath == PropertyPath.pointer)
7479
{
7580
var aptr = this.GetExtension<AnimationPointer>();
76-
if (aptr != null && AnimationPointer.TryParseNodeTransform(aptr.Pointer, out var nidx, out _)) return nidx;
81+
if (aptr != null && AnimationPointer.TryParseNodeIndex(aptr.Pointer, out var nidx)) return nidx;
7782
}
7883

7984
return -1;
8085
}
8186

87+
/// <summary>
88+
/// If the target is a node, it returns a <see cref="PropertyPath"/> resolved to <see cref="PropertyPath.scale"/>, <see cref="PropertyPath.rotation"/> or <see cref="PropertyPath.translation"/>
89+
/// otherwise it will return <see cref="PropertyPath.pointer"/>
90+
/// </summary>
91+
/// <returns>A <see cref="PropertyPath"/> value.</returns>
8292
public PropertyPath GetNodePath()
8393
{
8494
if (_NodePath == PropertyPath.pointer)
@@ -90,6 +100,10 @@ public PropertyPath GetNodePath()
90100
return _NodePath;
91101
}
92102

103+
/// <summary>
104+
/// Returns a pointer path regardless of whether it's defined by default <see cref="PropertyPath"/> or by the animation pointer extension.
105+
/// </summary>
106+
/// <returns>An animation pointer path.</returns>
93107
public string GetPointerPath()
94108
{
95109
var aptr = this.GetExtension<AnimationPointer>();
@@ -153,35 +167,45 @@ public string Pointer
153167
/// <summary>
154168
/// Parses the pointer path to see if it can be converted to a standard nodeIndex-PropertyPath path.
155169
/// </summary>
156-
/// <param name="pointer">The path to try parse.</param>
170+
/// <param name="pointerPath">The path to try parse.</param>
157171
/// <param name="nodeIndex">the logical index of the node.</param>
158172
/// <param name="property">the transformation property.</param>
159173
/// <returns>true if the parsing succeeded.</returns>
160-
public static bool TryParseNodeTransform(string pointer, out int nodeIndex, out PropertyPath property)
174+
public static bool TryParseNodeTransform(string pointerPath, out int nodeIndex, out PropertyPath property)
161175
{
162-
nodeIndex = -1;
163176
property = PropertyPath.pointer;
164177

165-
if (pointer == null || !pointer.StartsWith("/nodes/")) return false;
166-
178+
if (!TryParseNodeIndex(pointerPath, out nodeIndex)) return false;
167179

168-
pointer = pointer.Substring(7);
169-
var next = pointer.IndexOf('/');
180+
pointerPath = pointerPath.Substring(7);
181+
var next = pointerPath.IndexOf('/', StringComparison.Ordinal);
182+
if (next < 0) return false;
170183

171-
if (!int.TryParse(pointer.Substring(0, next), System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out var idx)) return false;
172-
173-
var tail = pointer.Substring(next + 1);
184+
var tail = pointerPath.Substring(next + 1);
174185
switch (tail)
175186
{
176-
case "scale": nodeIndex = idx; property = PropertyPath.scale; return true;
177-
case "rotation": nodeIndex = idx; property = PropertyPath.rotation; return true;
178-
case "translation": nodeIndex = idx; property = PropertyPath.translation; return true;
179-
case "weights": nodeIndex = idx; property = PropertyPath.weights; return true;
187+
case "scale": property = PropertyPath.scale; return true;
188+
case "rotation": property = PropertyPath.rotation; return true;
189+
case "translation": property = PropertyPath.translation; return true;
190+
case "weights": property = PropertyPath.weights; return true;
180191
}
181192

182193
return false;
183194
}
184195

196+
public static bool TryParseNodeIndex(string pointerPath, out int nodeIndex)
197+
{
198+
nodeIndex = -1;
199+
200+
if (pointerPath == null || !pointerPath.StartsWith("/nodes/")) return false;
201+
202+
pointerPath = pointerPath.Substring(7);
203+
var next = pointerPath.IndexOf('/', StringComparison.Ordinal);
204+
if (next < 0) next = pointerPath.Length;
205+
206+
return int.TryParse(pointerPath.Substring(0, next), System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out nodeIndex);
207+
}
208+
185209
#endregion
186210
}
187211
}

src/SharpGLTF.Core/Schema2/gltf.AnimationSampler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public float Duration
128128

129129
#region API
130130

131-
private Accessor _CreateInputAccessor(IReadOnlyList<Single> input)
131+
private Accessor _CreateInputAccessor(ROLIST input)
132132
{
133133
Guard.NotNull(input, nameof(input));
134134
Guard.MustBeGreaterThan(input.Count, 0, nameof(input.Count));
@@ -147,7 +147,7 @@ private Accessor _CreateInputAccessor(IReadOnlyList<Single> input)
147147
return accessor;
148148
}
149149

150-
private Accessor _CreateOutputAccessor(IReadOnlyList<Single> output)
150+
private Accessor _CreateOutputAccessor(ROLIST output)
151151
{
152152
Guard.NotNull(output, nameof(output));
153153
Guard.MustBeGreaterThan(output.Count, 0, nameof(output.Count));

0 commit comments

Comments
 (0)