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/>.
*/
using System;
using System.Net;
using System.Collections.Generic;
using godotOscSharp;
using Godot;
namespace godotVmcSharp
{
public class Marionette
{
private godotOscSharp.OscReceiver receiver;
private godotOscSharp.OscSender sender;
private OscReceiver receiver;
private OscSender sender;
private CameraReceiver cam;
private DeviceReceiver devices;
private DirectionalLightReceiver lights;
public Marionette(int port)
{
receiver = new godotOscSharp.OscReceiver(port);
receiver = new OscReceiver(port);
receiver.MessageReceived += (sender, e) =>
{
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}");
ProcessMessage(e.Message);
@ -42,8 +44,10 @@ namespace godotVmcSharp
{
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())
{
@ -66,7 +70,7 @@ namespace godotVmcSharp
new VmcMessage(m.Address);
break;
case "/VMC/Ext/Cam":
new VmcExtCam(m);
this.cam.ProcessMessage(new VmcExtCam(m));
break;
case "/VMC/Ext/Con":
new VmcExtCon(m);
@ -84,28 +88,18 @@ namespace godotVmcSharp
new VmcExtMidiCcBit(m);
break;
case "/VMC/Ext/Hmd/Pos":
new VmcExtDevicePos(m);
break;
case "/VMC/Ext/Con/Pos":
new VmcExtDevicePos(m);
break;
case "/VMC/Ext/Tra/Pos":
new VmcExtDevicePos(m);
break;
case "/VMC/Ext/Hmd/Pos/Local":
new VmcExtDevicePos(m);
break;
case "/VMC/Ext/Con/Pos/Local":
new VmcExtDevicePos(m);
break;
case "/VMC/Ext/Tra/Pos/Local":
new VmcExtDevicePos(m);
this.devices.ProcessMessage(new VmcExtDevicePos(m));
break;
case "/VMC/Ext/Rcv":
new VmcExtRcv(m);
break;
case "/VMC/Ext/Light":
new VmcExtLight(m);
lights.ProcessMessage(new VmcExtLight(m));
break;
case "/VMC/Ext/VRM":
new VmcExtVrm(m);

View File

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