| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Camera |
| | 5 | | { |
| | 6 | | public class InputSpikeFixer |
| | 7 | | { |
| | 8 | | private const float INPUT_SPIKE_TOLERANCE = 10f; |
| | 9 | | private readonly Func<CursorLockMode> getLockMode; |
| | 10 | |
|
| | 11 | | private CursorLockMode lastLockState; |
| | 12 | | private bool isLockStateDirty; |
| | 13 | |
|
| 799 | 14 | | public InputSpikeFixer(Func<CursorLockMode> getLockMode) |
| | 15 | | { |
| 799 | 16 | | this.getLockMode = getLockMode; |
| 799 | 17 | | lastLockState = getLockMode(); |
| 799 | 18 | | isLockStateDirty = false; |
| 799 | 19 | | } |
| | 20 | | public float GetValue(float currentValue) |
| | 21 | | { |
| 11023 | 22 | | CheckLockState(); |
| | 23 | |
|
| 11023 | 24 | | float absoluteValue = Mathf.Abs(currentValue); |
| | 25 | |
|
| 11023 | 26 | | if (ShouldIgnoreInputValue(absoluteValue)) |
| | 27 | | { |
| 2 | 28 | | if (IsInputValueTolerable(absoluteValue)) |
| 1 | 29 | | isLockStateDirty = false; |
| 2 | 30 | | return 0; |
| | 31 | | } |
| | 32 | |
|
| 11021 | 33 | | return currentValue; |
| | 34 | | } |
| 0 | 35 | | private static bool IsInputValueTolerable(float value) { return value < INPUT_SPIKE_TOLERANCE; } |
| 0 | 36 | | private bool ShouldIgnoreInputValue(float value) { return value > 0 && isLockStateDirty; } |
| | 37 | |
|
| | 38 | | private void CheckLockState() |
| | 39 | | { |
| 11023 | 40 | | var currentLockState = getLockMode(); |
| | 41 | |
|
| 11023 | 42 | | if (currentLockState != lastLockState) |
| | 43 | | { |
| 2 | 44 | | isLockStateDirty = true; |
| | 45 | | } |
| | 46 | |
|
| 11023 | 47 | | lastLockState = currentLockState; |
| 11023 | 48 | | } |
| | 49 | | } |
| | 50 | | } |