Skip to content

Commit

Permalink
feat(Animated): Adds support for DiffClamp node
Browse files Browse the repository at this point in the history
Add support for DiffClamp node on Windows for `Animated.diffClamp` that was added in facebook/react-native#9419.
  • Loading branch information
rozele committed Oct 28, 2016
1 parent 8969c82 commit 4c66f6e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
46 changes: 46 additions & 0 deletions ReactWindows/ReactNative/Animated/DiffClampAnimatedNode.cs
@@ -0,0 +1,46 @@
using Newtonsoft.Json.Linq;
using System;

namespace ReactNative.Animated
{
class DiffClampAnimatedNode : ValueAnimatedNode
{
private readonly NativeAnimatedNodesManager _nativeAnimatedNodesManager;
private readonly int _inputNodeTag;
private readonly double _min;
private readonly double _max;

private double _lastValue;

public DiffClampAnimatedNode(int tag, JObject config, NativeAnimatedNodesManager nativeAnimatedNodesManager)
: base(tag, config)
{
_nativeAnimatedNodesManager = nativeAnimatedNodesManager;
_inputNodeTag = config.Value<int>("input");
_min = config.Value<double>("min");
_max = config.Value<double>("max");

Value = _lastValue = GetInputNodeValue();
}

public override void Update()
{
var value = GetInputNodeValue();

var diff = value - _lastValue;
_lastValue = value;
Value = Math.Min(Math.Max(Value + diff, _min), _max);
}

private double GetInputNodeValue()
{
var valueAnimatedNode = _nativeAnimatedNodesManager.GetNodeById(_inputNodeTag) as ValueAnimatedNode;
if (valueAnimatedNode == null) {
throw new InvalidOperationException(
"Illegal node ID set as an input for Animated.DiffClamp node.");
}

return valueAnimatedNode.Value;
}
}
}
Expand Up @@ -77,6 +77,9 @@ public void CreateAnimatedNode(int tag, JObject config)
case "multiplication":
node = new MultiplicationAnimatedNode(tag, config, this);
break;
case "diffclamp":
node = new DiffClampAnimatedNode(tag, config, this);
break;
case "transform":
node = new TransformAnimatedNode(tag, config, this);
break;
Expand Down
1 change: 1 addition & 0 deletions ReactWindows/ReactNative/ReactNative.csproj
Expand Up @@ -95,6 +95,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Animated\AdditionAnimatedNode.cs" />
<Compile Include="Animated\DiffClampAnimatedNode.cs" />
<Compile Include="Animated\MultiplicationAnimatedNode.cs" />
<Compile Include="Animated\NativeAnimatedModule.cs" />
<Compile Include="Animated\NativeAnimatedNodesManager.cs" />
Expand Down

0 comments on commit 4c66f6e

Please sign in to comment.