-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tracker battery node
- Loading branch information
Showing
4 changed files
with
188 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using FrooxEngine; | ||
using FrooxEngine.ProtoFlux; | ||
using FrooxEngine.ProtoFlux.Runtimes.Execution; | ||
using ProtoFlux.Core; | ||
|
||
|
||
[Category(new string[] { "ProtoFlux/Runtimes/Execution/Nodes/Devices" })] | ||
public class ViveTrackerBattery : VoidNode<FrooxEngineContext> | ||
{ | ||
public readonly SyncRef<INodeObjectOutput<User>> User; | ||
|
||
public readonly SyncRef<INodeValueOutput<BodyNode>> BodyNode; | ||
|
||
public readonly NodeValueOutput<bool> IsActive; | ||
|
||
public readonly NodeValueOutput<float> BatteryLevel; | ||
|
||
public readonly NodeValueOutput<bool> IsBatteryCharging; | ||
|
||
public override Type NodeType => typeof(ProtoFlux.Runtimes.Execution.Nodes.FrooxEngine.Input.ViveTrackerBattery); | ||
|
||
public ProtoFlux.Runtimes.Execution.Nodes.FrooxEngine.Input.ViveTrackerBattery TypedNodeInstance { get; private set; } | ||
|
||
public override INode NodeInstance => TypedNodeInstance; | ||
|
||
public override int NodeInputCount => base.NodeInputCount + 2; | ||
|
||
public override int NodeOutputCount => base.NodeOutputCount + 3; | ||
|
||
public override N Instantiate<N>() | ||
{ | ||
if (TypedNodeInstance != null) | ||
{ | ||
throw new InvalidOperationException("Node has already been instantiated"); | ||
} | ||
ProtoFlux.Runtimes.Execution.Nodes.FrooxEngine.Input.ViveTrackerBattery viveTrackerBattery = (TypedNodeInstance = new ProtoFlux.Runtimes.Execution.Nodes.FrooxEngine.Input.ViveTrackerBattery()); | ||
return viveTrackerBattery as N; | ||
} | ||
|
||
protected override void AssociateInstanceInternal(INode node) | ||
{ | ||
if (node is ProtoFlux.Runtimes.Execution.Nodes.FrooxEngine.Input.ViveTrackerBattery typedNodeInstance) | ||
{ | ||
TypedNodeInstance = typedNodeInstance; | ||
return; | ||
} | ||
throw new ArgumentException("Node instance is not of type " + typeof(ProtoFlux.Runtimes.Execution.Nodes.FrooxEngine.Input.ViveTrackerBattery)); | ||
} | ||
|
||
public override void ClearInstance() | ||
{ | ||
TypedNodeInstance = null; | ||
} | ||
|
||
protected override ISyncRef GetInputInternal(ref int index) | ||
{ | ||
ISyncRef inputInternal = base.GetInputInternal(ref index); | ||
if (inputInternal != null) | ||
{ | ||
return inputInternal; | ||
} | ||
switch (index) | ||
{ | ||
case 0: | ||
return User; | ||
case 1: | ||
return BodyNode; | ||
default: | ||
index -= 2; | ||
return null; | ||
} | ||
} | ||
|
||
protected override INodeOutput GetOutputInternal(ref int index) | ||
{ | ||
INodeOutput outputInternal = base.GetOutputInternal(ref index); | ||
if (outputInternal != null) | ||
{ | ||
return outputInternal; | ||
} | ||
switch (index) | ||
{ | ||
case 0: | ||
return IsActive; | ||
case 1: | ||
return BatteryLevel; | ||
case 2: | ||
return IsBatteryCharging; | ||
default: | ||
index -= 3; | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FrooxEngine.ProtoFlux; | ||
using FrooxEngine; | ||
using ProtoFlux.Core; | ||
using ProtoFlux.Runtimes.Execution; | ||
using Obsidian; | ||
|
||
|
||
namespace ProtoFlux.Runtimes.Execution.Nodes.FrooxEngine.Input; | ||
[NodeCategory("Devices")] | ||
public class ViveTrackerBattery : VoidNode<FrooxEngineContext> | ||
{ | ||
public ObjectArgument<User> User; | ||
public ValueArgument<BodyNode> BodyNode; | ||
|
||
[ContinuouslyChanging] | ||
public readonly ValueOutput<bool> IsActive; | ||
|
||
[ContinuouslyChanging] | ||
public readonly ValueOutput<float> BatteryLevel; | ||
|
||
[ContinuouslyChanging] | ||
public readonly ValueOutput<bool> IsBatteryCharging; | ||
|
||
protected override void ComputeOutputs(FrooxEngineContext context) | ||
{ | ||
User user = 0.ReadObject<User>(context); | ||
if (user != null && user.IsRemoved) | ||
{ | ||
user = null; | ||
} | ||
var node = 1.ReadValue<BodyNode>(context); | ||
var trackerDevice = user?.GetComponent<ViveTrackerProxy>(p => p.TrackerBodyNode == node); | ||
if (trackerDevice == null && user != null) | ||
{ | ||
trackerDevice = user.AttachComponent<ViveTrackerProxy>(); | ||
trackerDevice.TrackerBodyNode.Value = node; | ||
} | ||
IsActive.Write(trackerDevice?.IsTrackerActive.Value ?? false, context); | ||
BatteryLevel.Write(trackerDevice?.BatteryLevel.Target?.Value ?? (-1f), context); | ||
IsBatteryCharging.Write((trackerDevice?.BatteryCharging.Target?.Value).GetValueOrDefault(), context); | ||
} | ||
|
||
public ViveTrackerBattery() | ||
{ | ||
IsActive = new ValueOutput<bool>(this); | ||
BatteryLevel = new ValueOutput<float>(this); | ||
IsBatteryCharging = new ValueOutput<bool>(this); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Linq; | ||
using FrooxEngine; | ||
|
||
namespace Obsidian; | ||
|
||
public class ViveTrackerProxy : UserComponent | ||
{ | ||
public readonly Sync<BodyNode> TrackerBodyNode; | ||
|
||
public readonly Sync<bool> IsTrackerActive; | ||
|
||
public readonly SyncRef<ValueStream<float>> BatteryLevel; | ||
|
||
public readonly SyncRef<ValueStream<bool>> BatteryCharging; | ||
|
||
private ViveTracker _currentTracker; | ||
|
||
protected override void OnCommonUpdate() | ||
{ | ||
if (base.User.IsLocalUser) | ||
{ | ||
ViveTracker device = InputInterface.GetDevices<ViveTracker>().FirstOrDefault(t => t.CorrespondingBodyNode == TrackerBodyNode.Value); | ||
if (device != _currentTracker) | ||
{ | ||
TrackerBodyNode.Value = device?.CorrespondingBodyNode ?? BodyNode.NONE; | ||
BatteryLevel.Target = device?.BatteryLevel.GetStream(base.World); | ||
BatteryCharging.Target = device?.BatteryCharging.GetStream(base.World); | ||
IsTrackerActive.Value = device != null; | ||
_currentTracker = device; | ||
} | ||
} | ||
} | ||
|
||
protected override void OnDispose() | ||
{ | ||
base.OnDispose(); | ||
_currentTracker = null; | ||
} | ||
} |