| | 1 | | using UnityEngine; |
| | 2 | |
|
| | 3 | | namespace DCL |
| | 4 | | { |
| | 5 | | public class IdleChecker : IIdleChecker |
| | 6 | | { |
| 667 | 7 | | Vector3 lastMouseCoordinate = Vector3.zero; |
| 667 | 8 | | private int maxTime = 60; |
| | 9 | | private float lastActivityTime = 0.0f; |
| | 10 | | private bool idle = false; |
| | 11 | | public event IIdleChecker.ChangeStatus OnChangeStatus; |
| | 12 | |
|
| 0 | 13 | | public void Subscribe(IIdleChecker.ChangeStatus callback) { OnChangeStatus += callback; } |
| | 14 | |
|
| 0 | 15 | | public void Unsubscribe(IIdleChecker.ChangeStatus callback) { OnChangeStatus -= callback; } |
| | 16 | |
|
| 2 | 17 | | public void Initialize() { lastActivityTime = Time.time; } |
| | 18 | |
|
| 2 | 19 | | public void SetMaxTime(int time) { maxTime = time; } |
| | 20 | |
|
| 0 | 21 | | public int GetMaxTime() { return maxTime; } |
| | 22 | |
|
| | 23 | | public void Update() |
| | 24 | | { |
| 19167 | 25 | | Vector3 mouseDelta = Input.mousePosition - lastMouseCoordinate; |
| | 26 | |
|
| 19167 | 27 | | bool mouseMoved = mouseDelta.sqrMagnitude > 0.0001f; |
| | 28 | |
|
| 19167 | 29 | | if (Input.anyKey || mouseMoved) |
| | 30 | | { |
| 0 | 31 | | lastActivityTime = Time.time; |
| | 32 | | } |
| | 33 | |
|
| 19167 | 34 | | lastMouseCoordinate = Input.mousePosition; |
| | 35 | |
|
| 19167 | 36 | | if (idle) |
| | 37 | | { |
| 8630 | 38 | | if (!IdleCheck()) |
| 0 | 39 | | SetIdleState(false); |
| 0 | 40 | | } |
| | 41 | | else |
| | 42 | | { |
| 10537 | 43 | | if (IdleCheck()) |
| 443 | 44 | | SetIdleState(true); |
| | 45 | | } |
| 19167 | 46 | | } |
| | 47 | |
|
| | 48 | | private void SetIdleState(bool status) |
| | 49 | | { |
| 443 | 50 | | idle = status; |
| | 51 | |
|
| 443 | 52 | | OnChangeStatus?.Invoke(idle); |
| | 53 | |
|
| 443 | 54 | | DCL.Interface.WebInterface.ReportIdleStateChanged(idle); |
| 443 | 55 | | } |
| | 56 | |
|
| 19167 | 57 | | private bool IdleCheck() { return Time.time - lastActivityTime > maxTime; } |
| | 58 | |
|
| 214 | 59 | | public bool isIdle() { return idle; } |
| | 60 | | } |
| | 61 | | } |