diff --git a/src/Device/OutputTarget/SerialOutputTarget.cs b/src/Device/OutputTarget/SerialOutputTarget.cs index 20bd16a..c202700 100644 --- a/src/Device/OutputTarget/SerialOutputTarget.cs +++ b/src/Device/OutputTarget/SerialOutputTarget.cs @@ -12,6 +12,9 @@ public class SerialOutputTarget : IOutputTarget private JSONStorableStringChooser ComPortChooser; private UIHorizontalGroup ButtonGroup; + private JSONStorableAction StartSerialAction; + private JSONStorableAction StopSerialAction; + private SerialPort _serial; public void CreateUI(IUIBuilder builder) @@ -26,12 +29,18 @@ public void CreateUI(IUIBuilder builder) var stopSerialButton = ButtonGroup.items[1].GetComponent(); stopSerialButton.label = "Stop Serial"; stopSerialButton.button.onClick.AddListener(StopSerial); + + StartSerialAction = UIManager.CreateAction("Start Serial", StartSerial); + StopSerialAction = UIManager.CreateAction("Stop Serial", StopSerial); } public void DestroyUI(IUIBuilder builder) { builder.Destroy(ComPortChooser); builder.Destroy(ButtonGroup); + + UIManager.RemoveAction(StartSerialAction); + UIManager.RemoveAction(StopSerialAction); } public void RestoreConfig(JSONNode config) diff --git a/src/Device/OutputTarget/UdpOutputTarget.cs b/src/Device/OutputTarget/UdpOutputTarget.cs index c856b7b..72e46c1 100644 --- a/src/Device/OutputTarget/UdpOutputTarget.cs +++ b/src/Device/OutputTarget/UdpOutputTarget.cs @@ -17,6 +17,9 @@ public class UdpOutputTarget : IOutputTarget private JSONStorableString PortText; private UIHorizontalGroup ButtonGroup; + private JSONStorableAction StartUdpAction; + private JSONStorableAction StopUdpAction; + private UdpClient _client; public void CreateUI(IUIBuilder builder) @@ -34,6 +37,9 @@ public void CreateUI(IUIBuilder builder) var stopSerialButton = ButtonGroup.items[1].GetComponent(); stopSerialButton.label = "Stop Udp"; stopSerialButton.button.onClick.AddListener(StopUdp); + + StartUdpAction = UIManager.CreateAction("Start Udp", StartUdp); + StopUdpAction = UIManager.CreateAction("Stop Udp", StopUdp); } public void DestroyUI(IUIBuilder builder) @@ -41,6 +47,9 @@ public void DestroyUI(IUIBuilder builder) builder.Destroy(AddressInput); builder.Destroy(PortInput); builder.Destroy(ButtonGroup); + + UIManager.RemoveAction(StartUdpAction); + UIManager.RemoveAction(StopUdpAction); } public void RestoreConfig(JSONNode config) diff --git a/src/MotionSource/AbstractRefreshableMotionSource.cs b/src/MotionSource/AbstractRefreshableMotionSource.cs index f0d8350..448a7ea 100644 --- a/src/MotionSource/AbstractRefreshableMotionSource.cs +++ b/src/MotionSource/AbstractRefreshableMotionSource.cs @@ -10,6 +10,8 @@ public abstract class AbstractRefreshableMotionSource : IMotionSource private UIDynamicButton RefreshButton; private UIDynamic Spacer; + private JSONStorableAction RefreshAction; + public abstract Vector3 ReferencePosition { get; } public abstract Vector3 ReferenceUp { get; } public abstract Vector3 ReferenceRight { get; } @@ -28,7 +30,8 @@ public abstract class AbstractRefreshableMotionSource : IMotionSource public virtual void CreateUI(IUIBuilder builder) { - RefreshButton = builder.CreateButton("Refresh", () => { + RefreshButton = builder.CreateButton("Refresh", () => + { ComponentCache.Clear(); RefreshButtonCallback(); }); @@ -36,12 +39,20 @@ public virtual void CreateUI(IUIBuilder builder) RefreshButton.textColor = Color.white; Spacer = builder.CreateSpacer(200); + + RefreshAction = UIManager.CreateAction("Refresh Motion Source", () => + { + ComponentCache.Clear(); + RefreshButtonCallback(); + }); } public virtual void DestroyUI(IUIBuilder builder) { builder.Destroy(RefreshButton); builder.Destroy(Spacer); + + UIManager.RemoveAction(RefreshAction); } protected abstract void RefreshButtonCallback(); diff --git a/src/UI/UIManager.cs b/src/UI/UIManager.cs index 7383419..befac02 100644 --- a/src/UI/UIManager.cs +++ b/src/UI/UIManager.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using UnityEngine; namespace ToySerialController.UI @@ -30,6 +31,15 @@ public static void Initialize(MVRScript plugin) Instance = new UIManager(plugin); } + public static JSONStorableAction CreateAction(string name, Action callback) + { + var action = new JSONStorableAction(name, new JSONStorableAction.ActionCallback(callback)); + Instance.plugin.RegisterAction(action); + return action; + } + + public static void RemoveAction(JSONStorableAction action) => Instance.plugin.DeregisterAction(action); + public static void RemoveSpacer(UIDynamic o) => Instance.plugin.RemoveSpacer(o); public static void RemoveButton(UIDynamicButton o) => Instance.plugin.RemoveButton(o);