refactor to start taking action on messages

This commit is contained in:
Cassandra de la Cruz-Munoz 2023-09-24 12:26:52 -04:00
parent 93b6b512ca
commit 3a1c0c7d91
5 changed files with 91 additions and 33 deletions

19
CameraReceiver.cs Normal file
View File

@ -0,0 +1,19 @@
using Godot;
namespace godotVmcSharp
{
class CameraReceiver : Node3D
{
readonly Camera3D camera;
public CameraReceiver(Camera3D camera)
{
this.camera = camera;
}
public void ProcessMessage(VmcExtCam message) {
this.camera.Fov = message.Fov;
this.camera.Transform = message.Transform;
}
}
}

22
DeviceReceiver.cs Normal file
View File

@ -0,0 +1,22 @@
using System.Collections.Generic;
using Godot;
namespace godotVmcSharp
{
class DeviceReceiver: Node3D
{
readonly Dictionary<string, Node3D> devices;
public DeviceReceiver()
{
this.devices = new Dictionary<string, Node3D>{};
}
public void ProcessMessage(VmcExtDevicePos message)
{
if (!this.devices.ContainsKey(message.Serial))
{
this.devices.Add(message.Serial, new Node3D());
}
this.devices[message.Serial].Transform = message.Transform;
}
}
}

View File

@ -0,0 +1,23 @@
using System.Collections.Generic;
using Godot;
namespace godotVmcSharp
{
class DirectionalLightReceiver
{
readonly Dictionary<string, DirectionalLight3D> lights;
public DirectionalLightReceiver()
{
this.lights = new Dictionary<string, DirectionalLight3D>{};
}
public void ProcessMessage(VmcExtLight message)
{
if (!this.lights.ContainsKey(message.Name))
{
this.lights.Add(message.Name, new DirectionalLight3D());
}
this.lights[message.Name].Transform = message.Transform;
this.lights[message.Name].LightColor = message.Color;
}
}
}

View File

@ -16,24 +16,26 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
using System;
using System.Net; using System.Net;
using System.Collections.Generic; using godotOscSharp;
using Godot; using Godot;
namespace godotVmcSharp namespace godotVmcSharp
{ {
public class Marionette public class Marionette
{ {
private godotOscSharp.OscReceiver receiver; private OscReceiver receiver;
private godotOscSharp.OscSender sender; private OscSender sender;
private CameraReceiver cam;
private DeviceReceiver devices;
private DirectionalLightReceiver lights;
public Marionette(int port) public Marionette(int port)
{ {
receiver = new godotOscSharp.OscReceiver(port); receiver = new OscReceiver(port);
receiver.MessageReceived += (sender, e) => receiver.MessageReceived += (sender, e) =>
{ {
if (sender == null) { if (sender == null) {
sender = new godotOscSharp.OscSender(IPAddress.Parse(e.IPAddress), port); sender = new OscSender(IPAddress.Parse(e.IPAddress), port);
} }
GD.Print($"Received a message from {e.IPAddress}:{e.Port}"); GD.Print($"Received a message from {e.IPAddress}:{e.Port}");
ProcessMessage(e.Message); ProcessMessage(e.Message);
@ -42,8 +44,10 @@ namespace godotVmcSharp
{ {
GD.Print($"Error: {e.ErrorMessage}"); GD.Print($"Error: {e.ErrorMessage}");
}; };
devices = new DeviceReceiver();
lights = new DirectionalLightReceiver();
} }
private void ProcessMessage(godotOscSharp.OscMessage m) private void ProcessMessage(OscMessage m)
{ {
switch (m.Address.ToString()) switch (m.Address.ToString())
{ {
@ -66,7 +70,7 @@ namespace godotVmcSharp
new VmcMessage(m.Address); new VmcMessage(m.Address);
break; break;
case "/VMC/Ext/Cam": case "/VMC/Ext/Cam":
new VmcExtCam(m); this.cam.ProcessMessage(new VmcExtCam(m));
break; break;
case "/VMC/Ext/Con": case "/VMC/Ext/Con":
new VmcExtCon(m); new VmcExtCon(m);
@ -84,28 +88,18 @@ namespace godotVmcSharp
new VmcExtMidiCcBit(m); new VmcExtMidiCcBit(m);
break; break;
case "/VMC/Ext/Hmd/Pos": case "/VMC/Ext/Hmd/Pos":
new VmcExtDevicePos(m);
break;
case "/VMC/Ext/Con/Pos": case "/VMC/Ext/Con/Pos":
new VmcExtDevicePos(m);
break;
case "/VMC/Ext/Tra/Pos": case "/VMC/Ext/Tra/Pos":
new VmcExtDevicePos(m);
break;
case "/VMC/Ext/Hmd/Pos/Local": case "/VMC/Ext/Hmd/Pos/Local":
new VmcExtDevicePos(m);
break;
case "/VMC/Ext/Con/Pos/Local": case "/VMC/Ext/Con/Pos/Local":
new VmcExtDevicePos(m);
break;
case "/VMC/Ext/Tra/Pos/Local": case "/VMC/Ext/Tra/Pos/Local":
new VmcExtDevicePos(m); this.devices.ProcessMessage(new VmcExtDevicePos(m));
break; break;
case "/VMC/Ext/Rcv": case "/VMC/Ext/Rcv":
new VmcExtRcv(m); new VmcExtRcv(m);
break; break;
case "/VMC/Ext/Light": case "/VMC/Ext/Light":
new VmcExtLight(m); lights.ProcessMessage(new VmcExtLight(m));
break; break;
case "/VMC/Ext/VRM": case "/VMC/Ext/VRM":
new VmcExtVrm(m); new VmcExtVrm(m);

View File

@ -16,7 +16,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
using System;
using System.Net; using System.Net;
using Godot; using Godot;
using godotOscSharp; using godotOscSharp;
@ -25,12 +24,15 @@ namespace godotVmcSharp
{ {
public class Performer public class Performer
{ {
private godotOscSharp.OscReceiver receiver; private OscReceiver receiver;
private godotOscSharp.OscSender sender; private OscSender sender;
private CameraReceiver cam;
private DeviceReceiver devices;
private DirectionalLightReceiver lights;
public Performer(IPAddress host, int port) public Performer(IPAddress host, int port)
{ {
receiver = new godotOscSharp.OscReceiver(port); receiver = new OscReceiver(port);
sender = new godotOscSharp.OscSender(host, port); sender = new OscSender(host, port);
receiver.MessageReceived += (sender, e) => receiver.MessageReceived += (sender, e) =>
{ {
GD.Print($"Received a message from {e.IPAddress}:{e.Port}"); GD.Print($"Received a message from {e.IPAddress}:{e.Port}");
@ -40,19 +42,17 @@ namespace godotVmcSharp
{ {
GD.Print($"Error: {e.ErrorMessage}"); GD.Print($"Error: {e.ErrorMessage}");
}; };
devices = new DeviceReceiver();
lights = new DirectionalLightReceiver();
} }
private void ProcessMessage(godotOscSharp.OscMessage m) private void ProcessMessage(OscMessage m)
{ {
switch (m.Address.ToString()) switch (m.Address.ToString())
{ {
case "/VMC/Ext/Hmd/Pos": case "/VMC/Ext/Hmd/Pos":
new VmcExtDevicePos(m);
break;
case "/VMC/Ext/Con/Pos": case "/VMC/Ext/Con/Pos":
new VmcExtDevicePos(m);
break;
case "/VMC/Ext/Tra/Pos": case "/VMC/Ext/Tra/Pos":
new VmcExtDevicePos(m); this.devices.ProcessMessage(new VmcExtDevicePos(m));
break; break;
case "/VMC/Ext/Set/Period": case "/VMC/Ext/Set/Period":
new VmcExtSetPeriod(m); new VmcExtSetPeriod(m);
@ -61,7 +61,7 @@ namespace godotVmcSharp
new VmcExtMidiCcVal(m); new VmcExtMidiCcVal(m);
break; break;
case "/VMC/Ext/Cam": case "/VMC/Ext/Cam":
new VmcExtCam(m); this.cam.ProcessMessage(new VmcExtCam(m));
break; break;
case "/VMC/Ext/Blend/Val": case "/VMC/Ext/Blend/Val":
new VmcExtBlendVal(m); new VmcExtBlendVal(m);
@ -88,7 +88,7 @@ namespace godotVmcSharp
new VmcExtSetConfig(m); new VmcExtSetConfig(m);
break; break;
case "/VMC/Ext/Light": case "/VMC/Ext/Light":
new VmcExtLight(m); this.lights.ProcessMessage(new VmcExtLight(m));
break; break;
case "/VMC/Ext/Set/Shortcut": case "/VMC/Ext/Set/Shortcut":
new VmcExtSetShortcut(m); new VmcExtSetShortcut(m);