Skip to content

Commit

Permalink
Register button callbacks as actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoooi0 committed May 24, 2022
1 parent 313602a commit b0116fc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/Device/OutputTarget/SerialOutputTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -26,12 +29,18 @@ public void CreateUI(IUIBuilder builder)
var stopSerialButton = ButtonGroup.items[1].GetComponent<UIDynamicButton>();
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)
Expand Down
9 changes: 9 additions & 0 deletions src/Device/OutputTarget/UdpOutputTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -34,13 +37,19 @@ public void CreateUI(IUIBuilder builder)
var stopSerialButton = ButtonGroup.items[1].GetComponent<UIDynamicButton>();
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)
{
builder.Destroy(AddressInput);
builder.Destroy(PortInput);
builder.Destroy(ButtonGroup);

UIManager.RemoveAction(StartUdpAction);
UIManager.RemoveAction(StopUdpAction);
}

public void RestoreConfig(JSONNode config)
Expand Down
13 changes: 12 additions & 1 deletion src/MotionSource/AbstractRefreshableMotionSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -28,20 +30,29 @@ public abstract class AbstractRefreshableMotionSource : IMotionSource

public virtual void CreateUI(IUIBuilder builder)
{
RefreshButton = builder.CreateButton("Refresh", () => {
RefreshButton = builder.CreateButton("Refresh", () =>
{
ComponentCache.Clear();
RefreshButtonCallback();
});
RefreshButton.buttonColor = new Color(0, 0.75f, 1f) * 0.8f;
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();
Expand Down
12 changes: 11 additions & 1 deletion src/UI/UIManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace ToySerialController.UI
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit b0116fc

Please sign in to comment.