Skip to content

Commit cb2b851

Browse files
committed
Caching layer and input count in Touchmanager.
1 parent fca94be commit cb2b851

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

Source/Assets/TouchScript/Scripts/TouchManagerInstance.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System;
66
using System.Collections;
77
using System.Collections.Generic;
8-
using System.Linq;
98
using TouchScript.Devices.Display;
109
using TouchScript.Hit;
1110
using TouchScript.InputSources;
@@ -192,7 +191,9 @@ public IList<TouchPoint> ActiveTouches
192191
private float dotsPerCentimeter = TouchManager.CM_TO_INCH * 96;
193192

194193
private List<TouchLayer> layers = new List<TouchLayer>(10);
194+
private int layerCount = 0;
195195
private List<IInputSource> inputs = new List<IInputSource>(3);
196+
private int inputCount = 0;
196197

197198
private List<TouchPoint> touches = new List<TouchPoint>(30);
198199
private Dictionary<int, TouchPoint> idToTouch = new Dictionary<int, TouchPoint>(30);
@@ -235,24 +236,29 @@ public bool AddLayer(TouchLayer layer, int index, bool addIfExists = true)
235236
{
236237
if (!addIfExists) return false;
237238
layers.RemoveAt(i);
239+
layerCount--;
238240
}
239241
if (index <= 0)
240242
{
241243
layers.Insert(0, layer);
244+
layerCount++;
242245
return i == -1;
243246
}
244-
if (index >= layers.Count)
247+
if (index >= layerCount)
245248
{
246249
layers.Add(layer);
250+
layerCount++;
247251
return i == -1;
248252
}
249253
if (i != -1)
250254
{
251255
if (index < i) layers.Insert(index, layer);
252256
else layers.Insert(index - 1, layer);
257+
layerCount++;
253258
return false;
254259
}
255260
layers.Insert(index, layer);
261+
layerCount++;
256262
return true;
257263
}
258264

@@ -261,14 +267,15 @@ public bool RemoveLayer(TouchLayer layer)
261267
{
262268
if (layer == null) return false;
263269
var result = layers.Remove(layer);
270+
if (result) layerCount--;
264271
return result;
265272
}
266273

267274
/// <inheritdoc />
268275
public void ChangeLayerIndex(int at, int to)
269276
{
270-
if (at < 0 || at >= layers.Count) return;
271-
if (to < 0 || to >= layers.Count) return;
277+
if (at < 0 || at >= layerCount) return;
278+
if (to < 0 || to >= layerCount) return;
272279
var data = layers[at];
273280
layers.RemoveAt(at);
274281
layers.Insert(to, data);
@@ -280,6 +287,7 @@ public bool AddInput(IInputSource input)
280287
if (input == null) return false;
281288
if (inputs.Contains(input)) return true;
282289
inputs.Add(input);
290+
inputCount++;
283291
return true;
284292
}
285293

@@ -288,6 +296,7 @@ public bool RemoveInput(IInputSource input)
288296
{
289297
if (input == null) return false;
290298
var result = inputs.Remove(input);
299+
if (result) inputCount--;
291300
return result;
292301
}
293302

@@ -313,8 +322,7 @@ public bool GetHitTarget(Vector2 position, out TouchHit hit, out TouchLayer laye
313322
hit = default(TouchHit);
314323
layer = null;
315324

316-
var count = layers.Count;
317-
for (var i = 0; i < count; i++)
325+
for (var i = 0; i < layerCount; i++)
318326
{
319327
var touchLayer = layers[i];
320328
if (touchLayer == null) continue;
@@ -545,11 +553,12 @@ private void updateLayers()
545553
{
546554
// filter empty layers
547555
layers = layers.FindAll(l => l != null);
556+
layerCount = layers.Count;
548557
}
549558

550559
private void createCameraLayer()
551560
{
552-
if (layers.Count == 0 && shouldCreateCameraLayer)
561+
if (layerCount == 0 && shouldCreateCameraLayer)
553562
{
554563
if (Camera.main != null)
555564
{
@@ -564,7 +573,7 @@ private void createCameraLayer()
564573

565574
private void createTouchInput()
566575
{
567-
if (inputs.Count == 0 && shouldCreateStandardInput)
576+
if (inputCount == 0 && shouldCreateStandardInput)
568577
{
569578
if (Application.isEditor)
570579
Debug.Log("[TouchScript] No input source found, adding StandardInput. (this message is harmless)");
@@ -585,14 +594,12 @@ private void createTouchInput()
585594

586595
private void updateInputs()
587596
{
588-
var count = inputs.Count;
589-
for (var i = 0; i < count; i++) inputs[i].UpdateInput();
597+
for (var i = 0; i < inputCount; i++) inputs[i].UpdateInput();
590598
}
591599

592600
private void updateBegan(List<TouchPoint> points)
593601
{
594602
var count = points.Count;
595-
var layerCount = layers.Count;
596603
for (var i = 0; i < count; i++)
597604
{
598605
var touch = points[i];

0 commit comments

Comments
 (0)