Skip to content

Commit

Permalink
add tracker battery node
Browse files Browse the repository at this point in the history
add tracker battery node
  • Loading branch information
Xlinka authored Jan 24, 2024
2 parents 4937a06 + 3ab330d commit dc23727
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 46 deletions.
95 changes: 95 additions & 0 deletions Bindings/Devices/ViveTrackerBattery.cs
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;
}
}
}
54 changes: 54 additions & 0 deletions ProtoFlux/Devices/ViveTrackerBattery.cs
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);
}
}
46 changes: 0 additions & 46 deletions ProtoFlux/Hardware/OpenVR/Tracker/TrackerBatteryBase.cs

This file was deleted.

39 changes: 39 additions & 0 deletions UserComponents/ViveTrackerProxy.cs
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;
}
}

0 comments on commit dc23727

Please sign in to comment.