diff --git a/Editor/Scripts/Inspector/SignalBinary.cs b/Editor/Scripts/Inspector/SignalBinary.cs new file mode 100644 index 0000000..31fee3d --- /dev/null +++ b/Editor/Scripts/Inspector/SignalBinary.cs @@ -0,0 +1,41 @@ +using OC.VisualElements; +using UnityEditor; +using UnityEditor.UIElements; +using UnityEngine; +using UnityEngine.UIElements; + +namespace OC.Editor.Inspector +{ + [CustomEditor(typeof(Components.SignalBinary), true), CanEditMultipleObjects] + public class SignalBinary : UnityEditor.Editor + { + public override VisualElement CreateInspectorGUI() + { + var component = target as Components.SignalBinary; + if (component == null) return null; + + var container = new VisualElement(); + + var groupControl = new PropertyGroup("Control"); + groupControl.AddOverrideOption(component); + groupControl.Add(new ToggleButton("Signal").BindProperty(component.Signal).AlignedField()); + + var groupStatus = new PropertyGroup("Status"); + groupStatus.Add(new LampField("Value", Color.green).BindProperty(component.Value).AlignedField()); + + var groupSettings = new PropertyGroup("Settings"); + groupSettings.Add(new PropertyField{bindingPath = "_device"}); + + var groupEvents = new PropertyGroup("Events"); + groupEvents.Add(new PropertyField{bindingPath = "OnValueChangedEvent"}); + + container.Add(groupControl); + container.Add(groupStatus); + container.Add(groupSettings); + container.Add(new PropertyField{bindingPath = "_link"}); + container.Add(groupEvents); + + return container; + } + } +} \ No newline at end of file diff --git a/Editor/Scripts/Inspector/SignalBinary.cs.meta b/Editor/Scripts/Inspector/SignalBinary.cs.meta new file mode 100644 index 0000000..bcdd4af --- /dev/null +++ b/Editor/Scripts/Inspector/SignalBinary.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cb73191a76cf4ad68ee79a7d6f8f6d69 +timeCreated: 1736264584 \ No newline at end of file diff --git a/Runtime/Scripts/Components/SignalBinary.cs b/Runtime/Scripts/Components/SignalBinary.cs new file mode 100644 index 0000000..9408087 --- /dev/null +++ b/Runtime/Scripts/Components/SignalBinary.cs @@ -0,0 +1,92 @@ +using OC.Communication; +using UnityEngine; +using UnityEngine.Events; + +namespace OC.Components +{ + [AddComponentMenu("Open Commissioning/Signal Binary")] + [SelectionBase] + [DisallowMultipleComponent] + public class SignalBinary : MonoComponent, IDevice, IMeasurement, IControlOverridable, ICustomInspector, IInteractable + { + public Link Link => _link; + public IProperty Override => _override; + public IPropertyReadOnly Value => _value; + public IProperty Signal => _signal; + + public UnityEvent OnValueChangedEvent; + + [SerializeField] + protected Property _override = new (false); + [SerializeField] + protected Property _value = new (false); + [SerializeField] + protected Property _signal = new (false); + [SerializeField] + private Component _device; + [SerializeField] + private Link _link; + + private Connector _connector; + private bool _isMeasurementDeviceValid; + private IMeasurement _measurementDevice; + + private void OnEnable() + { + _signal.ValueChanged += OnSignalChanged; + _value.ValueChanged += OnValueChanged; + } + + private void OnDisable() + { + _signal.ValueChanged -= OnSignalChanged; + _value.ValueChanged -= OnValueChanged; + } + + private void Start() + { + _link.Initialize(this); + _connector = new Connector(_link); + GetMeasurementDevice(); + OnValueChanged(_value.Value); + } + + private void Reset() + { + _link = new Link(this, "FB_SensorBinary"); + } + + private void OnSignalChanged(bool value) + { + _value.Value = value; + } + + private void OnValueChanged(bool value) + { + _connector.Status.SetBit(0, value); + OnValueChangedEvent?.Invoke(value); + } + + private void GetMeasurementDevice() + { + _isMeasurementDeviceValid = false; + + if (_device == null) return; + if (_device is IMeasurement device) + { + _isMeasurementDeviceValid = true; + _measurementDevice = device; + _measurementDevice.Value.ValueChanged += OnDeviceValueChanged; + OnDeviceValueChanged(_measurementDevice.Value.Value); + } + + if (!_isMeasurementDeviceValid) Logging.Logger.Log(LogType.Error, "Device reference isn't valid! IMeasurementDevice is required!", this); + } + + private void OnDeviceValueChanged(bool value) + { + if (_override) return; + _signal.Value = value; + } + } +} \ No newline at end of file diff --git a/Runtime/Scripts/Components/SignalBinary.cs.meta b/Runtime/Scripts/Components/SignalBinary.cs.meta new file mode 100644 index 0000000..9020017 --- /dev/null +++ b/Runtime/Scripts/Components/SignalBinary.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3fc0357b1aa948f4ba0f4951982ed7d4 +timeCreated: 1736264117 \ No newline at end of file