From 706a33ef1db606f99713e20e527c347f7382acd8 Mon Sep 17 00:00:00 2001 From: Toys0125 Date: Wed, 15 Apr 2026 17:21:41 -0500 Subject: [PATCH 1/2] Readd InterableShim, Please Remove after load test. --- .../Shims/BasisInteractableShim.cs | 210 ++++++++++++++++++ .../Shims/BasisInteractableShim.cs.meta | 2 + 2 files changed, 212 insertions(+) create mode 100644 Basis/Packages/com.basis.shim/Shims/BasisInteractableShim.cs create mode 100644 Basis/Packages/com.basis.shim/Shims/BasisInteractableShim.cs.meta diff --git a/Basis/Packages/com.basis.shim/Shims/BasisInteractableShim.cs b/Basis/Packages/com.basis.shim/Shims/BasisInteractableShim.cs new file mode 100644 index 0000000000..80cc04dba2 --- /dev/null +++ b/Basis/Packages/com.basis.shim/Shims/BasisInteractableShim.cs @@ -0,0 +1,210 @@ +using System; +using Basis.Scripts.BasisSdk.Interactions; +using Basis.Scripts.Device_Management.Devices; +using UnityEngine; + +// This is temporarily a clone of ExampleButtonInteractable. + +namespace Basis.Shims +{ + public class BasisInteractableShim : BasisInteractableObject // Need to remove at a later point. + { + // public BasisObjectSyncNetworking syncNetworking; + + // events other scripts can subscribe to + public delegate void ClickEvent(); + + public ClickEvent ButtonDown { get; set; } + public ClickEvent ButtonUp { get; set; } + + [ContextMenu("Trigger Down")] public void TriggerButtonDown() { ButtonDown(); } + [ContextMenu("Trigger Up")] public void TriggerButtonUp() { ButtonUp(); } + + //public void AddDelegateForButtonDown( Action d ) { ButtonDown += d; }\ + //public void AddDelegateForButtonUp( Action d ) { ButtonUp += d; } + + [Header("Button Settings")] + public bool isEnabled = true; + [Space(10)] + public string PropertyName = "_Color"; + public Color Color = Color.white; + public Color HoverColor = Color.white; + public Color InteractColor = Color.white; + public Color DisabledColor = Color.white; + + [Header("References")] + public Collider ColliderRef; + public MeshRenderer RendererRef; + + private BasisInputWrapper _inputSource; + // Ignore provided list localy, but keep it updated for other scripts + private BasisInputWrapper _InputSource + { + get => _inputSource; + set + { + if (value.Source != null) + { + Inputs = new(0); + Inputs.SetInputByRole(value.Source, value.GetState()); + } + else if (value.Source == null) + { + Inputs = new(0); + } + _inputSource = value; + + } + } + + void Start() + { + _InputSource = default; + if (ColliderRef == null) + { + TryGetComponent(out ColliderRef); + } + if (RendererRef == null) + { + TryGetComponent(out RendererRef); + } + + SetColor(isEnabled ? Color : DisabledColor); + } + + public override bool CanHover(BasisInput input) + { + return _InputSource.GetState() == BasisInteractInputState.NotAdded && IsWithinRange(input.transform.position, InteractRange) && isEnabled; + } + public override bool CanInteract(BasisInput input) + { + // must be the same input hovering + if (!_InputSource.IsInput(input)) return false; + // dont interact again till after interacting stopped + if (_InputSource.GetState() == BasisInteractInputState.Interacting) return false; + + return IsWithinRange(input.transform.position, InteractRange) && isEnabled; + } + + public override void OnHoverStart(BasisInput input) + { + if (!BasisInputWrapper.TryNewTracking(input, BasisInteractInputState.Hovering, out BasisInputWrapper wrapper)) + { + BasisDebug.LogWarning($"{nameof(BasisInteractableShim)}: Failed to setup input on hover"); + return; + } + _InputSource = wrapper; + SetColor(HoverColor); + // call base method (invokes event) + base.OnHoverStart(input); + } + + public override void OnHoverEnd(BasisInput input, bool willInteract) + { + if (_InputSource.IsInput(input)) + { + // leaving hover and wont interact this frame, + if (!willInteract) + { + bool added = BasisInputWrapper.TryNewTracking(null, BasisInteractInputState.NotAdded, out BasisInputWrapper wrapper); + // setting to null should not add the tracker + Debug.Assert(!added); + _InputSource = wrapper; + SetColor(Color); + } + // Oninteract will update color + + // call base method (invokes event) + base.OnHoverEnd(input, willInteract); + } + } + + public override void OnInteractStart(BasisInput input) + { + if (_InputSource.IsInput(input) && _InputSource.GetState() == BasisInteractInputState.Hovering) + { + // Set ownership to the local player + // syncNetworking.IsOwner = true; + SetColor(InteractColor); + + var newSource = _InputSource; + var didSetState = newSource.TrySetState(BasisInteractInputState.Interacting); + Debug.Assert(didSetState); + _InputSource = newSource; + + ButtonDown?.Invoke(); + // call base method (invokes event) + base.OnInteractStart(input); + } + } + + public override void OnInteractEnd(BasisInput input) + { + if (_InputSource.IsInput(input)) + { + SetColor(Color); + bool added = BasisInputWrapper.TryNewTracking(null, BasisInteractInputState.NotAdded, out BasisInputWrapper wrapper); + // setting to null should not add the tracker + Debug.Assert(!added); + _InputSource = wrapper; + + ButtonUp?.Invoke(); + // call base method (invokes event) + base.OnInteractEnd(input); + } + } + public override bool IsInteractingWith(BasisInput input) + { + return _InputSource.IsInput(input) && + _InputSource.GetState() == BasisInteractInputState.Interacting; + } + + public override bool IsHoveredBy(BasisInput input) + { + return _InputSource.IsInput(input) && + _InputSource.GetState() == BasisInteractInputState.Hovering; + } + + // set material property to a color + private void SetColor(Color color) + { + if (RendererRef != null && RendererRef.material != null) + { + RendererRef.material.SetColor(Shader.PropertyToID(PropertyName), color); + } + } + + + private bool _triggerCleanup; + // per-frame update, after IK transform + public override void InputUpdate() + { + if (!isEnabled) + { + if (_triggerCleanup) + { + _triggerCleanup = false; + // clean up currently hovering/interacting + if (_InputSource.GetState() != BasisInteractInputState.NotAdded) + { + if (IsHoveredBy(_InputSource.Source)) + { + OnHoverEnd(_InputSource.Source, false); + } + if (IsInteractingWith(_InputSource.Source)) + { + OnInteractEnd(_InputSource.Source); + } + } + // setting same color every frame isnt optimal but fine for example + SetColor(DisabledColor); + } + } + else + { + _triggerCleanup = true; + } + } + } +} + diff --git a/Basis/Packages/com.basis.shim/Shims/BasisInteractableShim.cs.meta b/Basis/Packages/com.basis.shim/Shims/BasisInteractableShim.cs.meta new file mode 100644 index 0000000000..7f61e36338 --- /dev/null +++ b/Basis/Packages/com.basis.shim/Shims/BasisInteractableShim.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: f8c388c62246d4cbda53a221c1c5408c \ No newline at end of file From 2866de73d59fdccb8d76e4f23bacd160c4eb3dc0 Mon Sep 17 00:00:00 2001 From: Toys0125 Date: Wed, 15 Apr 2026 17:46:39 -0500 Subject: [PATCH 2/2] Add back BasisInteractableShim with Obsolete notice. --- .../Shims/BasisInteractableShim.cs | 1 + .../Packages/com.basis.shim/Shims/BasisShims.cs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/Basis/Packages/com.basis.shim/Shims/BasisInteractableShim.cs b/Basis/Packages/com.basis.shim/Shims/BasisInteractableShim.cs index 80cc04dba2..ebd30da71a 100644 --- a/Basis/Packages/com.basis.shim/Shims/BasisInteractableShim.cs +++ b/Basis/Packages/com.basis.shim/Shims/BasisInteractableShim.cs @@ -7,6 +7,7 @@ namespace Basis.Shims { + [Obsolete("Use the direct interactable component instead. This is a shim for the old system and should be removed at a later point.")] public class BasisInteractableShim : BasisInteractableObject // Need to remove at a later point. { // public BasisObjectSyncNetworking syncNetworking; diff --git a/Basis/Packages/com.basis.shim/Shims/BasisShims.cs b/Basis/Packages/com.basis.shim/Shims/BasisShims.cs index b3d35b0410..ab8b15b503 100644 --- a/Basis/Packages/com.basis.shim/Shims/BasisShims.cs +++ b/Basis/Packages/com.basis.shim/Shims/BasisShims.cs @@ -37,6 +37,22 @@ public static BasisNetworkShim MakeNetworkable( MonoBehaviour mb ) return mb.gameObject.AddComponent(); } + + [Obsolete("Use the direct interactable component instead. This is a shim for the old system and should be removed at a later point.")] + public static BasisInteractableShim MakeInteractable( object o ) + { + // Actually needs to be CilboxProxies. + GameObject go = null; + if( o is CilboxProxy ) + go = ((CilboxProxy)o).gameObject; + else + go = ((MonoBehaviour)o).gameObject; + + BasisInteractableShim bi; + if( go.TryGetComponent( out bi ) ) return bi; + + return go.AddComponent(); + } }