// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Collections; namespace Microsoft.MixedReality.Toolkit.Input { /// /// Base class for input sources that don't inherit from MonoBehaviour. /// /// /// This base class does not support adding or removing pointers, because many will never /// pass pointers in their constructors and will fall back to either the Gaze or Mouse Pointer. /// public class BaseGenericInputSource : IMixedRealityInputSource, IDisposable { /// /// Constructor. /// public BaseGenericInputSource(string name, IMixedRealityPointer[] pointers = null, InputSourceType sourceType = InputSourceType.Other) { SourceId = CoreServices.InputSystem?.GenerateNewSourceId() ?? 0; SourceName = name; if (pointers != null) { Pointers = pointers; } else if (!CoreServices.InputSystem.IsNull() && !CoreServices.InputSystem.GazeProvider.IsNull() && CoreServices.InputSystem.GazeProvider.GazePointer is IMixedRealityPointer gazePointer) { Pointers = new[] { gazePointer }; } else { Pointers = new IMixedRealityPointer[] { }; } SourceType = sourceType; } /// public uint SourceId { get; } /// public string SourceName { get; } /// public virtual IMixedRealityPointer[] Pointers { get; } /// public InputSourceType SourceType { get; set; } #region IEquality Implementation public static bool Equals(IMixedRealityInputSource left, IMixedRealityInputSource right) { return left.Equals(right); } /// bool IEqualityComparer.Equals(object left, object right) { return left.Equals(right); } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) { return false; } if (ReferenceEquals(this, obj)) { return true; } if (obj.GetType() != GetType()) { return false; } return Equals((IMixedRealityInputSource)obj); } private bool Equals(IMixedRealityInputSource other) { return other != null && SourceId == other.SourceId && string.Equals(SourceName, other.SourceName); } /// int IEqualityComparer.GetHashCode(object obj) { return obj.GetHashCode(); } public override int GetHashCode() { unchecked { int hashCode = 0; hashCode = (hashCode * 397) ^ (int)SourceId; hashCode = (hashCode * 397) ^ (SourceName != null ? SourceName.GetHashCode() : 0); return hashCode; } } /// /// Dispose. /// public virtual void Dispose() { } #endregion IEquality Implementation } }