Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tracker battery node #14

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}