diff --git a/Source/Assets/TouchScript/Editor/TouchManagerEditor.cs b/Source/Assets/TouchScript/Editor/TouchManagerEditor.cs index e620a4478..b9ab847cf 100644 --- a/Source/Assets/TouchScript/Editor/TouchManagerEditor.cs +++ b/Source/Assets/TouchScript/Editor/TouchManagerEditor.cs @@ -200,13 +200,13 @@ private void refresh() { if (Application.isPlaying) { - var l = TouchManager.Instance.Layers; layers.arraySize = 0; - for (var i = 0; i < l.Count; i++) + LayerManager.Instance.ForEach((l) => { layers.arraySize++; - layers.GetArrayElementAtIndex(layers.arraySize - 1).objectReferenceValue = l[i]; - } + layers.GetArrayElementAtIndex(layers.arraySize - 1).objectReferenceValue = l; + return true; + }); } else { diff --git a/Source/Assets/TouchScript/Examples/_misc/Scripts/Runner.cs b/Source/Assets/TouchScript/Examples/_misc/Scripts/Runner.cs index 304679f36..cae28f1cd 100644 --- a/Source/Assets/TouchScript/Examples/_misc/Scripts/Runner.cs +++ b/Source/Assets/TouchScript/Examples/_misc/Scripts/Runner.cs @@ -5,14 +5,13 @@ using UnityEngine; using TouchScript.Layers; using System.Collections; - #if UNITY_EDITOR using UnityEditor; using System; #endif - #if UNITY_5_3_OR_NEWER using UnityEngine.SceneManagement; + #endif namespace TouchScript.Examples @@ -25,14 +24,14 @@ public class Runner : MonoBehaviour private static Runner instance; private TouchLayer layer; - public void LoadLevel(string name) - { + public void LoadLevel(string name) + { #if UNITY_5_3_OR_NEWER - SceneManager.LoadScene(name); + SceneManager.LoadScene(name); #else Application.LoadLevel(name); #endif - } + } public void LoadNextLevel() { @@ -43,20 +42,20 @@ public void LoadNextLevel() #endif } - public void LoadPreviousLevel() - { + public void LoadPreviousLevel() + { #if UNITY_5_3_OR_NEWER - var newLevel = SceneManager.GetActiveScene().buildIndex - 1; - if (newLevel == 0) newLevel = SceneManager.sceneCountInBuildSettings - 1; - SceneManager.LoadScene(newLevel); + var newLevel = SceneManager.GetActiveScene().buildIndex - 1; + if (newLevel == 0) newLevel = SceneManager.sceneCountInBuildSettings - 1; + SceneManager.LoadScene(newLevel); #else var newLevel = Application.loadedLevel - 1; if (newLevel == 0) newLevel = Application.levelCount - 1; Application.LoadLevel(newLevel); #endif - } + } - private void Start() + private void Start() { if (instance == null) { @@ -64,26 +63,27 @@ private void Start() DontDestroyOnLoad(gameObject); } - layer = GetComponent(); + layer = GetComponent(); #if UNITY_EDITOR - var guids = AssetDatabase.FindAssets("t:Scene", new string[]{"Assets/TouchScript/Examples"}); - if (EditorBuildSettings.scenes.Length != guids.Length) - { - if (EditorUtility.DisplayDialog("Add Example Scenes to Build Settings?", - "You are running Examples scene but example scenes are not added to Build Settings. Do you want to add them now?", "Yes", "No")) - { - var importers = Array.ConvertAll(guids, (string guid) => AssetImporter.GetAtPath(AssetDatabase.GUIDToAssetPath(guid))); - Array.Sort(importers, (AssetImporter a, AssetImporter b) => { - var i1 = string.IsNullOrEmpty(a.userData) ? 42 : Convert.ToInt32(a.userData); - var i2 = string.IsNullOrEmpty(b.userData) ? 42 : Convert.ToInt32(b.userData); - if (i1 == i2) return 0; - return i1 - i2; - }); - EditorBuildSettings.scenes = Array.ConvertAll(importers, (AssetImporter i) => new EditorBuildSettingsScene(i.assetPath, true)); - EditorUtility.DisplayDialog("Success", "Example scenes were added to Build Settings. Please restart Play Mode.", "OK"); - } - } + var guids = AssetDatabase.FindAssets("t:Scene", new string[] {"Assets/TouchScript/Examples"}); + if (EditorBuildSettings.scenes.Length != guids.Length) + { + if (EditorUtility.DisplayDialog("Add Example Scenes to Build Settings?", + "You are running Examples scene but example scenes are not added to Build Settings. Do you want to add them now?", "Yes", "No")) + { + var importers = Array.ConvertAll(guids, (string guid) => AssetImporter.GetAtPath(AssetDatabase.GUIDToAssetPath(guid))); + Array.Sort(importers, (AssetImporter a, AssetImporter b) => + { + var i1 = string.IsNullOrEmpty(a.userData) ? 42 : Convert.ToInt32(a.userData); + var i2 = string.IsNullOrEmpty(b.userData) ? 42 : Convert.ToInt32(b.userData); + if (i1 == i2) return 0; + return i1 - i2; + }); + EditorBuildSettings.scenes = Array.ConvertAll(importers, (AssetImporter i) => new EditorBuildSettingsScene(i.assetPath, true)); + EditorUtility.DisplayDialog("Success", "Example scenes were added to Build Settings. Please restart Play Mode.", "OK"); + } + } #endif #if UNITY_5_4_OR_NEWER @@ -103,14 +103,15 @@ private void Start() private void OnDestroy() { #if UNITY_5_4_OR_NEWER - SceneManager.sceneLoaded -= sceneLoadedHandler; + SceneManager.sceneLoaded -= sceneLoadedHandler; #endif - } + } private void Update() { if (Input.GetKeyDown(KeyCode.Escape)) Application.Quit(); } + #if UNITY_5_4_OR_NEWER private void sceneLoadedHandler(Scene scene, LoadSceneMode mode) { @@ -124,9 +125,9 @@ private void OnLevelWasLoaded(int num) #endif private IEnumerator resetUILayer() - { - yield return new WaitForEndOfFrame(); - TouchManager.Instance.AddLayer(layer, 0); - } + { + yield return new WaitForEndOfFrame(); + LayerManager.Instance.AddLayer(layer, 0); + } } } \ No newline at end of file diff --git a/Source/Assets/TouchScript/Scripts/Devices/Display/GenericDisplayDevice.cs b/Source/Assets/TouchScript/Scripts/Devices/Display/GenericDisplayDevice.cs index 08e8fca0a..6be0ccfe7 100644 --- a/Source/Assets/TouchScript/Scripts/Devices/Display/GenericDisplayDevice.cs +++ b/Source/Assets/TouchScript/Scripts/Devices/Display/GenericDisplayDevice.cs @@ -56,9 +56,6 @@ protected override void OnEnable() updateNativeResulotion(); updateNativeDPI(); UpdateDPI(); - - Debug.LogFormat("{0}x{1} {2}x{3}", Screen.width, Screen.height, Screen.currentResolution.width, Screen.currentResolution.height); - Debug.LogFormat("Device Model: {0}, Device Name: {1}, GPU: {2}", SystemInfo.deviceModel, SystemInfo.deviceName, SystemInfo.graphicsDeviceName); } private void updateNativeResulotion() diff --git a/Source/Assets/TouchScript/Scripts/GestureManager.cs b/Source/Assets/TouchScript/Scripts/GestureManager.cs index eb0b62747..c5f46e8f7 100644 --- a/Source/Assets/TouchScript/Scripts/GestureManager.cs +++ b/Source/Assets/TouchScript/Scripts/GestureManager.cs @@ -12,7 +12,7 @@ namespace TouchScript /// /// Why IList instead of Pointer in pointer events? /// Right now touchesBegan/touchesMoved/touchesEnded methods in Gesture class accept IList as their argument which seems to overcomplicate a lot of stuff and just calling touchBegan(TouchPoint) would be easier. - /// The later approach was tried in 7.0 and reverted in 8.0 since it introduced a really hard to fix gesture priority issue.If with lists a gesture knows all touches changed during current frame, individual touchMoved calls have to be buffered till the end of frame.But there's no way to execute gesture recognition logic at the end of frame in the right hierarchical order. This concern resulted in the following issue: https://github.com/TouchScript/TouchScript/issues/203 + /// The later approach was tried in 7.0 and reverted in 8.0 since it introduced a really hard to fix gesture priority issue. If with lists a gesture knows all touches changed during current frame, individual touchMoved calls have to be buffered till the end of frame. But there's no way to execute gesture recognition logic at the end of frame in the right hierarchical order. This concern resulted in the following issue: https://github.com/TouchScript/TouchScript/issues/203 /// /// public sealed class GestureManager : MonoBehaviour diff --git a/Source/Assets/TouchScript/Scripts/Gestures/Gesture.cs b/Source/Assets/TouchScript/Scripts/Gestures/Gesture.cs index 1f331a9a3..20a830ee0 100644 --- a/Source/Assets/TouchScript/Scripts/Gestures/Gesture.cs +++ b/Source/Assets/TouchScript/Scripts/Gestures/Gesture.cs @@ -647,7 +647,7 @@ public virtual HitData GetScreenPositionHitData() { HitData hit; fakePointer.Position = ScreenPosition; - touchManager.INTERNAL_GetHitTarget(fakePointer, out hit); + LayerManager.Instance.GetHitTarget(fakePointer, out hit); return hit; } diff --git a/Source/Assets/TouchScript/Scripts/ILayerManager.cs b/Source/Assets/TouchScript/Scripts/ILayerManager.cs new file mode 100644 index 000000000..233da8c55 --- /dev/null +++ b/Source/Assets/TouchScript/Scripts/ILayerManager.cs @@ -0,0 +1,53 @@ +/* + * @author Valentin Simonov / http://va.lent.in/ + */ + +using System; +using System.Collections.Generic; +using TouchScript.Hit; +using TouchScript.Layers; +using TouchScript.Pointers; + +namespace TouchScript +{ + public interface ILayerManager + { + /// + /// Gets the list of . + /// + /// A sorted list of currently active layers. + IList Layers { get; } + + int LayerCount { get; } + + /// + /// Adds a layer in a specific position. + /// + /// The layer to add. + /// Layer index to add the layer to or -1 to add to the end of the list. + /// if set to true move the layer to another index if it is already added; don't move otherwise. + /// + /// True if the layer was added. + /// + bool AddLayer(TouchLayer layer, int index = -1, bool addIfExists = true); + + /// + /// Removes a layer. + /// + /// The layer to remove. + /// True if the layer was removed. + bool RemoveLayer(TouchLayer layer); + + /// + /// Swaps layers. + /// + /// Layer index 1. + /// Layer index 2. + void ChangeLayerIndex(int at, int to); + + void ForEach(Func action); + + bool GetHitTarget(IPointer pointer, out HitData hit); + + } +} diff --git a/Source/Assets/TouchScript/Scripts/ILayerManager.cs.meta b/Source/Assets/TouchScript/Scripts/ILayerManager.cs.meta new file mode 100644 index 000000000..c13ed3987 --- /dev/null +++ b/Source/Assets/TouchScript/Scripts/ILayerManager.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 2be5f17d5476d2e4a98d02449616ff1e +timeCreated: 1500755913 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Source/Assets/TouchScript/Scripts/ITouchManager.cs b/Source/Assets/TouchScript/Scripts/ITouchManager.cs index fc116e4d3..1696f1190 100644 --- a/Source/Assets/TouchScript/Scripts/ITouchManager.cs +++ b/Source/Assets/TouchScript/Scripts/ITouchManager.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using TouchScript.Devices.Display; using TouchScript.InputSources; -using TouchScript.Layers; using TouchScript.Pointers; namespace TouchScript @@ -107,12 +106,6 @@ public interface ITouchManager /// This is usually a desired behavior but sometimes you would want to turn this off. bool ShouldCreateStandardInput { get; set; } - /// - /// Gets the list of . - /// - /// A sorted list of currently active layers. - IList Layers { get; } - /// /// Gets the list of /// @@ -146,31 +139,6 @@ public interface ITouchManager /// An unsorted list of all pointers which were pressed but not released yet. IList PressedPointers { get; } - /// - /// Adds a layer in a specific position. - /// - /// The layer to add. - /// Layer index to add the layer to or -1 to add to the end of the list. - /// if set to true move the layer to another index if it is already added; don't move otherwise. - /// - /// True if the layer was added. - /// - bool AddLayer(TouchLayer layer, int index = -1, bool addIfExists = true); - - /// - /// Removes a layer. - /// - /// The layer to remove. - /// True if the layer was removed. - bool RemoveLayer(TouchLayer layer); - - /// - /// Swaps layers. - /// - /// Layer index 1. - /// Layer index 2. - void ChangeLayerIndex(int at, int to); - /// /// Adds an input source. /// diff --git a/Source/Assets/TouchScript/Scripts/LayerManager.cs b/Source/Assets/TouchScript/Scripts/LayerManager.cs new file mode 100644 index 000000000..83c7f2528 --- /dev/null +++ b/Source/Assets/TouchScript/Scripts/LayerManager.cs @@ -0,0 +1,16 @@ +/* + * @author Valentin Simonov / http://va.lent.in/ + */ + +using UnityEngine; + +namespace TouchScript +{ + public sealed class LayerManager : MonoBehaviour + { + public static ILayerManager Instance + { + get { return LayerManagerInstance.Instance; } + } + } +} diff --git a/Source/Assets/TouchScript/Scripts/LayerManager.cs.meta b/Source/Assets/TouchScript/Scripts/LayerManager.cs.meta new file mode 100644 index 000000000..fd683f038 --- /dev/null +++ b/Source/Assets/TouchScript/Scripts/LayerManager.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 3e8fcbff26c96d94ab9865ea8a8607d4 +timeCreated: 1500755913 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Source/Assets/TouchScript/Scripts/LayerManagerInstance.cs b/Source/Assets/TouchScript/Scripts/LayerManagerInstance.cs new file mode 100644 index 000000000..19015d66d --- /dev/null +++ b/Source/Assets/TouchScript/Scripts/LayerManagerInstance.cs @@ -0,0 +1,185 @@ +/* + * @author Valentin Simonov / http://va.lent.in/ + */ + +using System; +using System.Collections.Generic; +using TouchScript.Hit; +using TouchScript.Layers; +using TouchScript.Pointers; +using UnityEngine; + +namespace TouchScript +{ + internal sealed class LayerManagerInstance : MonoBehaviour, ILayerManager + { + #region Public properties + + public static ILayerManager Instance + { + get + { + if (shuttingDown) return null; + if (instance == null) + { + if (!Application.isPlaying) return null; + var objects = FindObjectsOfType(); + if (objects.Length == 0) + { + var go = new GameObject("GestureManager Instance"); + instance = go.AddComponent(); + } + else if (objects.Length >= 1) + { + instance = objects[0]; + } + } + return instance; + } + } + + /// + public IList Layers + { + get { return new List(layers); } + } + + /// + public int LayerCount + { + get { return layerCount; } + } + + #endregion + + #region Private variables + + private static LayerManagerInstance instance; + private static bool shuttingDown = false; + + private List layers = new List(10); + private int layerCount = 0; + + #endregion + + #region Public methods + + /// + public bool AddLayer(TouchLayer layer, int index = -1, bool addIfExists = true) + { + if (layer == null) return false; + + var i = layers.IndexOf(layer); + if (i != -1) + { + if (!addIfExists) return false; + layers.RemoveAt(i); + layerCount--; + } + if (index == 0) + { + layers.Insert(0, layer); + layerCount++; + return i == -1; + } + if (index == -1 || index >= layerCount) + { + layers.Add(layer); + layerCount++; + return i == -1; + } + if (i != -1) + { + if (index < i) layers.Insert(index, layer); + else layers.Insert(index - 1, layer); + layerCount++; + return false; + } + layers.Insert(index, layer); + layerCount++; + return true; + } + + /// + public bool RemoveLayer(TouchLayer layer) + { + if (layer == null) return false; + var result = layers.Remove(layer); + if (result) layerCount--; + return result; + } + + /// + public void ChangeLayerIndex(int at, int to) + { + if (at < 0 || at >= layerCount) return; + if (to < 0 || to >= layerCount) return; + var data = layers[at]; + layers.RemoveAt(at); + layers.Insert(to, data); + } + + /// + public void ForEach(Func action) + { + for (var i = 0; i < layerCount; i++) + { + if (!action(layers[i])) break; + } + } + + /// + public bool GetHitTarget(IPointer pointer, out HitData hit) + { + hit = default(HitData); + + for (var i = 0; i < layerCount; i++) + { + var touchLayer = layers[i]; + if (touchLayer == null) continue; + var result = touchLayer.Hit(pointer, out hit); + switch (result) + { + case HitResult.Hit: + return true; + case HitResult.Discard: + return false; + } + } + + return false; + } + + #endregion + + #region Unity + + private void Awake() + { + if (instance == null) + { + instance = this; + } + else if (instance != this) + { + Destroy(this); + return; + } + + gameObject.hideFlags = HideFlags.HideInHierarchy; + DontDestroyOnLoad(gameObject); + } + + private void OnApplicationQuit() + { + shuttingDown = true; + } + + #endregion + + #region Private functions + + #endregion + + } +} diff --git a/Source/Assets/TouchScript/Scripts/LayerManagerInstance.cs.meta b/Source/Assets/TouchScript/Scripts/LayerManagerInstance.cs.meta new file mode 100644 index 000000000..44da71ae4 --- /dev/null +++ b/Source/Assets/TouchScript/Scripts/LayerManagerInstance.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 61a2ae412ada86b48aa49c2ec1557dcb +timeCreated: 1500755913 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Source/Assets/TouchScript/Scripts/Layers/TouchLayer.cs b/Source/Assets/TouchScript/Scripts/Layers/TouchLayer.cs index 36977e6e6..310a41eb0 100644 --- a/Source/Assets/TouchScript/Scripts/Layers/TouchLayer.cs +++ b/Source/Assets/TouchScript/Scripts/Layers/TouchLayer.cs @@ -129,7 +129,7 @@ private IEnumerator lateAwake() yield return null; // Add ourselves after TouchManager finished adding layers in order - TouchManager.Instance.AddLayer(this, -1, false); + LayerManager.Instance.AddLayer(this, -1, false); } // To be able to turn layers off @@ -143,7 +143,7 @@ protected virtual void OnDestroy() if (!Application.isPlaying || TouchManager.Instance == null) return; StopAllCoroutines(); - TouchManager.Instance.RemoveLayer(this); + LayerManager.Instance.RemoveLayer(this); } #endregion diff --git a/Source/Assets/TouchScript/Scripts/Pointers/FakePointer.cs b/Source/Assets/TouchScript/Scripts/Pointers/FakePointer.cs index c145aed77..e2a9e840b 100644 --- a/Source/Assets/TouchScript/Scripts/Pointers/FakePointer.cs +++ b/Source/Assets/TouchScript/Scripts/Pointers/FakePointer.cs @@ -55,7 +55,7 @@ public FakePointer() public HitData GetOverData(bool forceRecalculate = false) { HitData overData; - TouchManagerInstance.Instance.INTERNAL_GetHitTarget(this, out overData); + LayerManager.Instance.GetHitTarget(this, out overData); return overData; } diff --git a/Source/Assets/TouchScript/Scripts/Pointers/Pointer.cs b/Source/Assets/TouchScript/Scripts/Pointers/Pointer.cs index 81a356da0..3ec3f8a8e 100644 --- a/Source/Assets/TouchScript/Scripts/Pointers/Pointer.cs +++ b/Source/Assets/TouchScript/Scripts/Pointers/Pointer.cs @@ -231,7 +231,7 @@ public ProjectionParams ProjectionParams private static StringBuilder builder; - private TouchManagerInstance manager; + private LayerManagerInstance layerManager; private int refCount = 0; private Vector2 position, newPosition; private HitData pressData, overData; @@ -246,7 +246,7 @@ public HitData GetOverData(bool forceRecalculate = false) { if (overDataIsDirty || forceRecalculate) { - manager.INTERNAL_GetHitTarget(this, out overData); + layerManager.GetHitTarget(this, out overData); overDataIsDirty = false; } return overData; @@ -323,7 +323,7 @@ public override string ToString() /// public Pointer(IInputSource input) { - manager = TouchManager.Instance as TouchManagerInstance; + layerManager = LayerManager.Instance as LayerManagerInstance; Type = PointerType.Unknown; InputSource = input; INTERNAL_Reset(); diff --git a/Source/Assets/TouchScript/Scripts/TouchManager.cs b/Source/Assets/TouchScript/Scripts/TouchManager.cs index b762726d6..c90b8c57a 100644 --- a/Source/Assets/TouchScript/Scripts/TouchManager.cs +++ b/Source/Assets/TouchScript/Scripts/TouchManager.cs @@ -404,7 +404,8 @@ private void Awake() Instance.ShouldCreateStandardInput = ShouldCreateStandardInput; for (var i = 0; i < layers.Count; i++) { - Instance.AddLayer(layers[i], i); + var layer = layers[i]; + if (layer != null) LayerManager.Instance.AddLayer(layer, i); } } diff --git a/Source/Assets/TouchScript/Scripts/TouchManagerInstance.cs b/Source/Assets/TouchScript/Scripts/TouchManagerInstance.cs index e9b6aa55b..81d1c12e4 100644 --- a/Source/Assets/TouchScript/Scripts/TouchManagerInstance.cs +++ b/Source/Assets/TouchScript/Scripts/TouchManagerInstance.cs @@ -19,7 +19,6 @@ #endif #if UNITY_5_4_OR_NEWER using UnityEngine.SceneManagement; - #endif namespace TouchScript @@ -165,12 +164,6 @@ public bool ShouldCreateStandardInput set { shouldCreateStandardInput = value; } } - /// - public IList Layers - { - get { return new List(layers); } - } - /// public IList Inputs { @@ -213,6 +206,7 @@ public IList PressedPointers private static bool shuttingDown = false; private static TouchManagerInstance instance; + private bool shouldCreateCameraLayer = true; private bool shouldCreateStandardInput = true; @@ -220,8 +214,8 @@ public IList PressedPointers private float dpi = 96; private float dotsPerCentimeter = TouchManager.CM_TO_INCH * 96; - private List layers = new List(10); - private int layerCount = 0; + private ILayerManager layerManager; + private List inputs = new List(3); private int inputCount = 0; @@ -248,6 +242,12 @@ public IList PressedPointers #endregion + #region Temporary variables + + private Pointer tmpPointer; + + #endregion + #region Debug #if TOUCHSCRIPT_DEBUG @@ -261,61 +261,6 @@ public IList PressedPointers #region Public methods - /// - public bool AddLayer(TouchLayer layer, int index = -1, bool addIfExists = true) - { - if (layer == null) return false; - - var i = layers.IndexOf(layer); - if (i != -1) - { - if (!addIfExists) return false; - layers.RemoveAt(i); - layerCount--; - } - if (index == 0) - { - layers.Insert(0, layer); - layerCount++; - return i == -1; - } - if (index == -1 || index >= layerCount) - { - layers.Add(layer); - layerCount++; - return i == -1; - } - if (i != -1) - { - if (index < i) layers.Insert(index, layer); - else layers.Insert(index - 1, layer); - layerCount++; - return false; - } - layers.Insert(index, layer); - layerCount++; - return true; - } - - /// - public bool RemoveLayer(TouchLayer layer) - { - if (layer == null) return false; - var result = layers.Remove(layer); - if (result) layerCount--; - return result; - } - - /// - public void ChangeLayerIndex(int at, int to) - { - if (at < 0 || at >= layerCount) return; - if (to < 0 || to >= layerCount) return; - var data = layers[at]; - layers.RemoveAt(at); - layers.Insert(to, data); - } - /// public bool AddInput(IInputSource input) { @@ -374,28 +319,6 @@ public void UpdateResolution() #region Internal methods - /// - internal bool INTERNAL_GetHitTarget(IPointer pointer, out HitData hit) - { - hit = default(HitData); - - for (var i = 0; i < layerCount; i++) - { - var touchLayer = layers[i]; - if (touchLayer == null) continue; - var result = touchLayer.Hit(pointer, out hit); - switch (result) - { - case HitResult.Hit: - return true; - case HitResult.Discard: - return false; - } - } - - return false; - } - internal void INTERNAL_AddPointer(Pointer pointer) { lock (pointerLock) @@ -609,6 +532,8 @@ private void Awake() gameObject.hideFlags = HideFlags.HideInHierarchy; DontDestroyOnLoad(gameObject); + layerManager = LayerManager.Instance; + UpdateResolution(); StopAllCoroutines(); @@ -644,7 +569,6 @@ private IEnumerator lateAwake() yield return null; yield return null; - updateLayers(); createCameraLayer(); createInput(); } @@ -665,16 +589,9 @@ private void OnApplicationQuit() #region Private functions - private void updateLayers() - { - // filter empty layers - layers = layers.FindAll(l => l != null); - layerCount = layers.Count; - } - private void createCameraLayer() { - if (layerCount == 0 && shouldCreateCameraLayer) + if (layerManager.LayerCount == 0 && shouldCreateCameraLayer) { if (Camera.main != null) { @@ -682,7 +599,7 @@ private void createCameraLayer() Debug.Log( "[TouchScript] No touch layers found, adding StandardLayer for the main camera. (this message is harmless)"); var layer = Camera.main.gameObject.AddComponent(); - AddLayer(layer); + layerManager.AddLayer(layer); } } } @@ -730,12 +647,9 @@ private void updateAdded(List pointers) pLogger.Log(pointer, PointerEvent.Added); #endif - for (var j = 0; j < layerCount; j++) - { - var touchLayer = layers[j]; - if (touchLayer == null) continue; - touchLayer.INTERNAL_AddPointer(pointer); - } + tmpPointer = pointer; + layerManager.ForEach(layerAddPointer); + tmpPointer = null; #if TOUCHSCRIPT_DEBUG if (DebugMode) addDebugFigureForPointer(pointer); @@ -747,6 +661,12 @@ private void updateAdded(List pointers) pointerListPool.Release(list); } + private bool layerAddPointer(TouchLayer layer) + { + layer.INTERNAL_AddPointer(tmpPointer); + return true; + } + private void updateUpdated(List pointers) { var updatedCount = pointers.Count; @@ -774,12 +694,9 @@ private void updateUpdated(List pointers) if (layer != null) layer.INTERNAL_UpdatePointer(pointer); else { - for (var j = 0; j < layerCount; j++) - { - var touchLayer = layers[j]; - if (touchLayer == null) continue; - touchLayer.INTERNAL_UpdatePointer(pointer); - } + tmpPointer = pointer; + layerManager.ForEach(layerUpdatePointer); + tmpPointer = null; } #if TOUCHSCRIPT_DEBUG @@ -792,6 +709,12 @@ private void updateUpdated(List pointers) pointerListPool.Release(list); } + private bool layerUpdatePointer(TouchLayer layer) + { + layer.INTERNAL_UpdatePointer(tmpPointer); + return true; + } + private void updatePressed(List pointers) { var pressedCount = pointers.Count; @@ -899,12 +822,9 @@ private void updateRemoved(List pointers) pLogger.Log(pointer, PointerEvent.Removed); #endif - for (var j = 0; j < layerCount; j++) - { - var touchLayer = layers[j]; - if (touchLayer == null) continue; - touchLayer.INTERNAL_RemovePointer(pointer); - } + tmpPointer = pointer; + layerManager.ForEach(layerRemovePointer); + tmpPointer = null; #if TOUCHSCRIPT_DEBUG if (DebugMode) removeDebugFigureForPointer(pointer); @@ -923,6 +843,12 @@ private void updateRemoved(List pointers) pointerListPool.Release(list); } + private bool layerRemovePointer(TouchLayer layer) + { + layer.INTERNAL_RemovePointer(tmpPointer); + return true; + } + private void updateCancelled(List pointers) { var cancelledCount = pointers.Count; @@ -949,12 +875,9 @@ private void updateCancelled(List pointers) pLogger.Log(pointer, PointerEvent.Cancelled); #endif - for (var j = 0; j < layerCount; j++) - { - var touchLayer = layers[j]; - if (touchLayer == null) continue; - touchLayer.INTERNAL_CancelPointer(pointer); - } + tmpPointer = pointer; + layerManager.ForEach(layerCancelPointer); + tmpPointer = null; #if TOUCHSCRIPT_DEBUG if (DebugMode) removeDebugFigureForPointer(pointer); @@ -972,6 +895,12 @@ private void updateCancelled(List pointers) pointerListPool.Release(list); } + private bool layerCancelPointer(TouchLayer layer) + { + layer.INTERNAL_CancelPointer(tmpPointer); + return true; + } + private void sendFrameStartedToPointers() { var count = pointers.Count;