diff --git a/ADD_ME.cslist b/ADD_ME.cslist index b175989..ab8fd89 100644 --- a/ADD_ME.cslist +++ b/ADD_ME.cslist @@ -35,4 +35,3 @@ src\UI\UIGroup.cs src\Utils\ComponentCache.cs src\Utils\Extensions.cs src\Utils\MathUtils.cs -src\Utils\PInvoke.cs diff --git a/ToySerialController.csproj b/ToySerialController.csproj index da8c1c9..b1dc32d 100644 --- a/ToySerialController.csproj +++ b/ToySerialController.csproj @@ -104,8 +104,8 @@ - - + + @@ -118,7 +118,6 @@ - @@ -126,7 +125,6 @@ - diff --git a/meta.json b/meta.json index 28ef68d..ba6e254 100644 --- a/meta.json +++ b/meta.json @@ -25,10 +25,9 @@ "Custom\\Scripts\\Yoooi\\ToySerialController\\lib\\vam-debug-utils\\LineDrawer.cs", "Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Config\\ConfigManager.cs", "Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Config\\IConfigProvider.cs", - "Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Device\\AbstractGenericDevice.cs", - "Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Device\\AbstractGenericDevice.UI.cs", "Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Device\\IDevice.cs", "Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Device\\TCodeDevice.cs", + "Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Device\\TCodeDevice.UI.cs", "Custom\\Scripts\\Yoooi\\ToySerialController\\src\\MotionSource\\AbstractFemaleMotionSource.cs", "Custom\\Scripts\\Yoooi\\ToySerialController\\src\\MotionSource\\AbstractRefreshableMotionSource.cs", "Custom\\Scripts\\Yoooi\\ToySerialController\\src\\MotionSource\\AnimationMotionSource.cs", diff --git a/src/Config/ConfigManager.cs b/src/Config/ConfigManager.cs index 4a92338..05a00b7 100644 --- a/src/Config/ConfigManager.cs +++ b/src/Config/ConfigManager.cs @@ -31,11 +31,14 @@ public static void SaveConfig(string path, IConfigProvider provider) if (!CheckPath(ref path)) return; + var directory = FileManagerSecure.GetDirectoryName(path); + if (!FileManagerSecure.DirectoryExists(directory)) + FileManagerSecure.CreateDirectory(directory); + var config = new JSONClass(); provider.StoreConfig(config); - Controller.SaveJSON(config, path); + Controller.SaveJSON(config, path, () => SuperController.LogMessage($"Saved config! \"{path}\""), null, null); - SuperController.LogMessage($"Saved config! \"{path}\""); } public static void LoadConfig(string path, IConfigProvider provider) @@ -43,6 +46,10 @@ public static void LoadConfig(string path, IConfigProvider provider) if (!CheckPath(ref path)) return; + var directory = FileManagerSecure.GetDirectoryName(path); + if (!FileManagerSecure.DirectoryExists(directory)) + return; + var config = Controller.LoadJSON(path); provider.RestoreConfig(config); @@ -51,8 +58,8 @@ public static void LoadConfig(string path, IConfigProvider provider) private static void OpenDialog(string defaultPath, string filter, bool textEntry, uFileBrowser.FileBrowserCallback callback) { - if(!FileManagerSecure.DirectoryExists(defaultPath)) - PInvoke.MakeSureDirectoryPathExists(defaultPath); + if (!FileManagerSecure.DirectoryExists(defaultPath)) + FileManagerSecure.CreateDirectory(defaultPath); Controller.mediaFileBrowserUI.fileRemovePrefix = null; Controller.mediaFileBrowserUI.hideExtension = false; diff --git a/src/Device/TCodeDevice.UI.cs b/src/Device/TCodeDevice.UI.cs index 2c9c5d6..161d3a4 100644 --- a/src/Device/TCodeDevice.UI.cs +++ b/src/Device/TCodeDevice.UI.cs @@ -1,7 +1,6 @@ using CurveEditor; using CurveEditor.UI; using SimpleJSON; -using System; using System.Collections.Generic; using ToySerialController.Config; using ToySerialController.UI; @@ -203,7 +202,7 @@ private void CreateR1AxisUI(IUIBuilder builder) { var group = new UIGroup(builder); var visible = false; - R1AxisTitle = builder.CreateButton("Pitch | R1", () => group.SetVisible(visible = !visible), Color.magenta * 0.8f, Color.white, true); + R1AxisTitle = builder.CreateButton("Roll | R1", () => group.SetVisible(visible = !visible), Color.magenta * 0.8f, Color.white, true); RangeMaxR1Slider = group.CreateSlider("Device:RangeMaxR1", "Range Max (+/- \u00b0)", 30, 1, 89, true, true, true, "F0"); OutputMaxR1Slider = group.CreateSlider("Device:OutputMaxR1", "Output Max (+/- %)", 0.5f, 0f, 0.5f, true, true, true, "P0"); OffsetR1Slider = group.CreateSlider("Device:OffsetR1", "Offset (%)", 0f, -0.25f, 0.25f, true, true, true, "P0"); @@ -218,7 +217,7 @@ private void CreateR2AxisUI(IUIBuilder builder) { var group = new UIGroup(builder); var visible = false; - R2AxisTitle = builder.CreateButton("Roll | R2", () => group.SetVisible(visible = !visible), Color.yellow * 0.8f, Color.white, true); + R2AxisTitle = builder.CreateButton("Pitch | R2", () => group.SetVisible(visible = !visible), Color.yellow * 0.8f, Color.white, true); RangeMaxR2Slider = group.CreateSlider("Device:RangeMaxR2", "Range Max (+/- \u00b0)", 30, 1, 89, true, true, true, "F0"); OutputMaxR2Slider = group.CreateSlider("Device:OutputMaxR2", "Output Max (+/- %)", 0.5f, 0.01f, 0.5f, true, true, true, "P0"); OffsetR2Slider = group.CreateSlider("Device:OffsetR2", "Offset (%)", 0f, -0.25f, 0.25f, true, true, true, "P0"); diff --git a/src/Device/TCodeDevice.cs b/src/Device/TCodeDevice.cs index 4840553..27bc4c0 100644 --- a/src/Device/TCodeDevice.cs +++ b/src/Device/TCodeDevice.cs @@ -1,5 +1,4 @@ using DebugUtils; -using System; using System.IO.Ports; using System.Text; using ToySerialController.MotionSource; @@ -12,9 +11,9 @@ public partial class TCodeDevice : IDevice protected readonly float[] XTarget, RTarget, ETarget; protected readonly float[] XCmd, RCmd, ECmd; - private float? _lastCollisionTime; - private bool _lastCollisionSmoothingEnabled; - private float _lastCollisionSmoothingStartTime, _lastCollisionSmoothingDuration; + private float? _lastNoCollisionTime; + private bool _lastNoCollisionSmoothingEnabled; + private float _lastNoCollisionSmoothingStartTime, _lastNoCollisionSmoothingDuration; protected string DeviceReport { get; set; } public string GetDeviceReport() => DeviceReport; @@ -28,6 +27,8 @@ public TCodeDevice() XCmd = new float[] { 0.5f, 0.5f, 0.5f }; RCmd = new float[] { 0.5f, 0.5f, 0.5f }; ECmd = new float[9]; + + _lastNoCollisionTime = Time.time; } public void Write(SerialPort serial) @@ -51,14 +52,14 @@ public void Write(SerialPort serial) var sb = new StringBuilder(); sb.Append(" Target Cmd Serial\n"); - sb.Append("L0\t").AppendFormat("{0,5:0.00}", XTarget[0]).Append(",\t").AppendFormat("{0,5:0.00}", XCmd[0]).Append(",\t").Append(l0).AppendLine(); - sb.Append("L1\t").AppendFormat("{0,5:0.00}", XTarget[1]).Append(",\t").AppendFormat("{0,5:0.00}", XCmd[1]).Append(",\t").Append(l1).AppendLine(); - sb.Append("L2\t").AppendFormat("{0,5:0.00}", XTarget[2]).Append(",\t").AppendFormat("{0,5:0.00}", XCmd[2]).Append(",\t").Append(l2).AppendLine(); - sb.Append("R0\t").AppendFormat("{0,5:0.00}", RTarget[0]).Append(",\t").AppendFormat("{0,5:0.00}", RCmd[0]).Append(",\t").Append(r0).AppendLine(); - sb.Append("R1\t").AppendFormat("{0,5:0.00}", RTarget[1]).Append(",\t").AppendFormat("{0,5:0.00}", RCmd[1]).Append(",\t").Append(r1).AppendLine(); - sb.Append("R2\t").AppendFormat("{0,5:0.00}", RTarget[2]).Append(",\t").AppendFormat("{0,5:0.00}", RCmd[2]).Append(",\t").Append(r2).AppendLine(); - sb.Append("V0\t").AppendFormat("{0,5:0.00}", ETarget[0]).Append(",\t").AppendFormat("{0,5:0.00}", ECmd[0]).Append(",\t").Append(v0).AppendLine(); - sb.Append("V1\t").AppendFormat("{0,5:0.00}", ETarget[1]).Append(",\t").AppendFormat("{0,5:0.00}", ECmd[1]).Append(",\t").Append(v1).AppendLine(); + sb.Append("L0\t").AppendFormat("{0,5:0.00}", XTarget[0]).Append(",\t").AppendFormat("{0,5:0.00}", XCmd[0]).Append(",\t").AppendLine(l0); + sb.Append("L1\t").AppendFormat("{0,5:0.00}", XTarget[1]).Append(",\t").AppendFormat("{0,5:0.00}", XCmd[1]).Append(",\t").AppendLine(l1); + sb.Append("L2\t").AppendFormat("{0,5:0.00}", XTarget[2]).Append(",\t").AppendFormat("{0,5:0.00}", XCmd[2]).Append(",\t").AppendLine(l2); + sb.Append("R0\t").AppendFormat("{0,5:0.00}", RTarget[0]).Append(",\t").AppendFormat("{0,5:0.00}", RCmd[0]).Append(",\t").AppendLine(r0); + sb.Append("R1\t").AppendFormat("{0,5:0.00}", RTarget[1]).Append(",\t").AppendFormat("{0,5:0.00}", RCmd[1]).Append(",\t").AppendLine(r1); + sb.Append("R2\t").AppendFormat("{0,5:0.00}", RTarget[2]).Append(",\t").AppendFormat("{0,5:0.00}", RCmd[2]).Append(",\t").AppendLine(r2); + sb.Append("V0\t").AppendFormat("{0,5:0.00}", ETarget[0]).Append(",\t").AppendFormat("{0,5:0.00}", ECmd[0]).Append(",\t").AppendLine(v0); + sb.Append("V1\t").AppendFormat("{0,5:0.00}", ETarget[1]).Append(",\t").AppendFormat("{0,5:0.00}", ECmd[1]).Append(",\t").AppendLine(v1); sb.Append("L3\t").AppendFormat("{0,5:0.00}", ETarget[2]).Append(",\t").AppendFormat("{0,5:0.00}", ECmd[2]).Append(",\t").Append(l3); DeviceReport = sb.ToString(); } @@ -85,47 +86,57 @@ public bool Update(IMotionSource motionSource) for (var i = 0; i < 5; i++) DebugDraw.DrawCircle(Vector3.Lerp(motionSource.ReferencePosition, referenceEnding, i / 4.0f), motionSource.ReferenceUp, Color.grey, radius); - if (diffPosition.magnitude > 0.00001f) - { - var t = Mathf.Clamp(Vector3.Dot(motionSource.TargetPosition - motionSource.ReferencePosition, motionSource.ReferenceUp), 0f, length); - var closestPoint = motionSource.ReferencePosition + motionSource.ReferenceUp * t; + var t = Mathf.Clamp(Vector3.Dot(motionSource.TargetPosition - motionSource.ReferencePosition, motionSource.ReferenceUp), 0f, length); + var closestPoint = motionSource.ReferencePosition + motionSource.ReferenceUp * t; - if (Vector3.Magnitude(closestPoint - motionSource.TargetPosition) > radius) + if (Vector3.Magnitude(closestPoint - motionSource.TargetPosition) <= radius) + { + if (diffPosition.magnitude > 0.0001f) { - if (_lastCollisionTime == null) - _lastCollisionTime = Time.time; - return false; + XTarget[0] = 1 - Mathf.Clamp01((closestPoint - motionSource.ReferencePosition).magnitude / length); + if (aboveTarget) + XTarget[0] = XTarget[0] > 0 ? 1 : 0; + + var diffOnPlane = Vector3.ProjectOnPlane(diffPosition, motionSource.ReferencePlaneNormal); + var yOffset = Vector3.Project(diffOnPlane, motionSource.ReferenceRight); + var zOffset = Vector3.Project(diffOnPlane, motionSource.ReferenceForward); + XTarget[1] = yOffset.magnitude * Mathf.Sign(Vector3.Dot(yOffset, motionSource.ReferenceRight)); + XTarget[2] = zOffset.magnitude * Mathf.Sign(Vector3.Dot(zOffset, motionSource.ReferenceForward)); } + else + { + XTarget[0] = 1; + XTarget[1] = 0; + XTarget[2] = 0; + } + + var correctedRight = Vector3.ProjectOnPlane(motionSource.TargetRight, motionSource.ReferenceUp); + if (Vector3.Dot(correctedRight, motionSource.ReferenceRight) < 0) + correctedRight -= 2 * Vector3.Project(correctedRight, motionSource.ReferenceRight); - XTarget[0] = 1 - Mathf.Clamp01((closestPoint - motionSource.ReferencePosition).magnitude / length); - if (aboveTarget) - XTarget[0] = XTarget[0] > 0 ? 1 : 0; + RTarget[0] = Vector3.SignedAngle(motionSource.ReferenceRight, correctedRight, motionSource.ReferenceUp) / 180; + RTarget[1] = -Vector3.SignedAngle(motionSource.ReferenceUp, Vector3.ProjectOnPlane(motionSource.TargetUp, motionSource.ReferenceForward), motionSource.ReferenceForward) / 90; + RTarget[2] = Vector3.SignedAngle(motionSource.ReferenceUp, Vector3.ProjectOnPlane(motionSource.TargetUp, motionSource.ReferenceRight), motionSource.ReferenceRight) / 90; - var diffOnPlane = Vector3.ProjectOnPlane(diffPosition, motionSource.ReferencePlaneNormal); - var yOffset = Vector3.Project(diffOnPlane, motionSource.ReferenceRight); - var zOffset = Vector3.Project(diffOnPlane, motionSource.ReferenceForward); - XTarget[1] = yOffset.magnitude * Mathf.Sign(Vector3.Dot(yOffset, motionSource.ReferenceRight)); - XTarget[2] = zOffset.magnitude * Mathf.Sign(Vector3.Dot(zOffset, motionSource.ReferenceForward)); + ETarget[0] = OutputV0CurveEditorSettings.Evaluate(XTarget, RTarget); + ETarget[1] = OutputV1CurveEditorSettings.Evaluate(XTarget, RTarget); + ETarget[2] = OutputL3CurveEditorSettings.Evaluate(XTarget, RTarget); + + if (_lastNoCollisionTime != null) + { + _lastNoCollisionSmoothingEnabled = true; + _lastNoCollisionSmoothingStartTime = Time.time; + _lastNoCollisionSmoothingDuration = Mathf.Clamp(Time.time - _lastNoCollisionTime.Value, 0.5f, 2); + _lastNoCollisionTime = null; + } } else { - SuperController.singleton.Message("diff"); - XTarget[0] = 1; - XTarget[1] = 0; - XTarget[2] = 0; - } - - var correctedRight = Vector3.ProjectOnPlane(motionSource.TargetRight, motionSource.ReferenceUp); - if (Vector3.Dot(correctedRight, motionSource.ReferenceRight) < 0) - correctedRight -= 2 * Vector3.Project(correctedRight, motionSource.ReferenceRight); - - RTarget[0] = Vector3.SignedAngle(motionSource.ReferenceRight, correctedRight, motionSource.ReferenceUp) / 180; - RTarget[1] = Vector3.SignedAngle(motionSource.ReferenceUp, Vector3.ProjectOnPlane(motionSource.TargetUp, motionSource.ReferenceForward), motionSource.ReferenceForward) / 90; - RTarget[2] = Vector3.SignedAngle(motionSource.ReferenceUp, Vector3.ProjectOnPlane(motionSource.TargetUp, motionSource.ReferenceRight), motionSource.ReferenceRight) / 90; + if (_lastNoCollisionTime == null) + _lastNoCollisionTime = Time.time; - ETarget[0] = OutputV0CurveEditorSettings.Evaluate(XTarget, RTarget); - ETarget[1] = OutputV1CurveEditorSettings.Evaluate(XTarget, RTarget); - ETarget[2] = OutputL3CurveEditorSettings.Evaluate(XTarget, RTarget); + return false; + } var l0t = (XTarget[0] - RangeMinL0Slider.val) / (RangeMaxL0Slider.val - RangeMinL0Slider.val); var l1t = (XTarget[1] + RangeMaxL1Slider.val) / (2 * RangeMaxL1Slider.val); @@ -184,21 +195,12 @@ public bool Update(IMotionSource motionSource) if (EnableOverrideV1Toggle.val) v1CmdRaw = OverrideV1Slider.val; if (EnableOverrideL3Toggle.val) l3CmdRaw = OverrideL3Slider.val; - if (_lastCollisionTime != null) - { - var noCollisionDuration = Time.time - _lastCollisionTime.Value; - _lastCollisionSmoothingDuration = Mathf.Clamp(noCollisionDuration, 0.5f, 2); - _lastCollisionSmoothingStartTime = Time.time; - _lastCollisionSmoothingEnabled = true; - _lastCollisionTime = null; - } - - if (_lastCollisionSmoothingEnabled) + if (_lastNoCollisionSmoothingEnabled) { - var lastCollisionSmoothingT = Mathf.Pow(2, 10 * ((Time.time - _lastCollisionSmoothingStartTime) / _lastCollisionSmoothingDuration - 1)); + var lastCollisionSmoothingT = Mathf.Pow(2, 10 * ((Time.time - _lastNoCollisionSmoothingStartTime) / _lastNoCollisionSmoothingDuration - 1)); if (lastCollisionSmoothingT >= 1.0f) { - _lastCollisionSmoothingEnabled = false; + _lastNoCollisionSmoothingEnabled = false; } else { diff --git a/src/MotionSource/AssetFemaleMotionSource.cs b/src/MotionSource/AssetFemaleMotionSource.cs index 1ae9289..f70779d 100644 --- a/src/MotionSource/AssetFemaleMotionSource.cs +++ b/src/MotionSource/AssetFemaleMotionSource.cs @@ -5,7 +5,6 @@ using ToySerialController.Utils; using UnityEngine; using DebugUtils; -using Leap.Unity; namespace ToySerialController.MotionSource { diff --git a/src/MotionSource/DildoFemaleMotionSource.cs b/src/MotionSource/DildoFemaleMotionSource.cs index 06519d1..1ff5d78 100644 --- a/src/MotionSource/DildoFemaleMotionSource.cs +++ b/src/MotionSource/DildoFemaleMotionSource.cs @@ -1,5 +1,4 @@ using SimpleJSON; -using System.Collections.Generic; using System.Linq; using ToySerialController.UI; using ToySerialController.Utils; diff --git a/src/MotionSource/RangeTestMotionSource.cs b/src/MotionSource/RangeTestMotionSource.cs index fa4f030..31d4793 100644 --- a/src/MotionSource/RangeTestMotionSource.cs +++ b/src/MotionSource/RangeTestMotionSource.cs @@ -1,6 +1,5 @@ using DebugUtils; using SimpleJSON; -using System; using System.Collections.Generic; using ToySerialController.UI; using UnityEngine; diff --git a/src/Plugin.cs b/src/Plugin.cs index d45e282..225a536 100644 --- a/src/Plugin.cs +++ b/src/Plugin.cs @@ -5,9 +5,6 @@ using ToySerialController.MotionSource; using DebugUtils; using ToySerialController.Utils; -using MVR.FileManagementSecure; -using Microsoft.Win32.SafeHandles; -using UnityEngine; namespace ToySerialController { diff --git a/src/Utils/PInvoke.cs b/src/Utils/PInvoke.cs deleted file mode 100644 index 826897a..0000000 --- a/src/Utils/PInvoke.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.Win32.SafeHandles; -using System; -using System.Runtime.InteropServices; -using System.Threading; - -namespace ToySerialController.Utils -{ - public static class PInvoke - { - [DllImport("dbgHelp", SetLastError = true)] - public static extern bool MakeSureDirectoryPathExists(string dirPath); - - [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] - public static extern SafeFileHandle CreateFile(string fileName, uint fileAccess, uint fileShare, - IntPtr securityAttributes, uint creationDisposition, uint flags, IntPtr template); - - [DllImport("kernel32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer,uint nNumberOfBytesToWrite, - out uint lpNumberOfBytesWritten, IntPtr lpOverlapped); - } -}