| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL |
| | 5 | | { |
| | 6 | | public class IdleChecker : IIdleChecker |
| | 7 | | { |
| 630 | 8 | | Vector3 lastMouseCoordinate = Vector3.zero; |
| 630 | 9 | | private int maxTime = 60; |
| | 10 | | private float lastActivityTime = 0.0f; |
| | 11 | | private bool idle = false; |
| | 12 | | private IUpdateEventHandler updateEventHandler; |
| | 13 | | public event IIdleChecker.ChangeStatus OnChangeStatus; |
| | 14 | |
|
| 0 | 15 | | public void Subscribe(IIdleChecker.ChangeStatus callback) { OnChangeStatus += callback; } |
| | 16 | |
|
| 0 | 17 | | public void Unsubscribe(IIdleChecker.ChangeStatus callback) { OnChangeStatus -= callback; } |
| | 18 | |
|
| 630 | 19 | | public IdleChecker (IUpdateEventHandler eventHandler = null) |
| | 20 | | { |
| 630 | 21 | | this.updateEventHandler = eventHandler; |
| 630 | 22 | | } |
| | 23 | |
|
| | 24 | | public void Initialize() |
| | 25 | | { |
| 630 | 26 | | lastActivityTime = Time.time; |
| | 27 | |
|
| 630 | 28 | | if (this.updateEventHandler == null) |
| 630 | 29 | | this.updateEventHandler = DCL.Environment.i.platform.updateEventHandler; |
| | 30 | |
|
| 630 | 31 | | updateEventHandler?.AddListener(IUpdateEventHandler.EventType.Update, Update); |
| 629 | 32 | | } |
| | 33 | |
|
| 0 | 34 | | public void SetMaxTime(int time) { maxTime = time; } |
| | 35 | |
|
| 0 | 36 | | public int GetMaxTime() { return maxTime; } |
| | 37 | |
|
| | 38 | | public void Update() |
| | 39 | | { |
| 6994 | 40 | | Vector3 mouseDelta = Input.mousePosition - lastMouseCoordinate; |
| | 41 | |
|
| 6994 | 42 | | bool mouseMoved = mouseDelta.sqrMagnitude > 0.0001f; |
| | 43 | |
|
| 6994 | 44 | | if (Input.anyKey || mouseMoved) |
| | 45 | | { |
| 0 | 46 | | lastActivityTime = Time.time; |
| | 47 | | } |
| | 48 | |
|
| 6994 | 49 | | lastMouseCoordinate = Input.mousePosition; |
| | 50 | |
|
| 6994 | 51 | | if (idle) |
| | 52 | | { |
| 0 | 53 | | if (!IdleCheck()) |
| 0 | 54 | | SetIdleState(false); |
| 0 | 55 | | } |
| | 56 | | else |
| | 57 | | { |
| 6994 | 58 | | if (IdleCheck()) |
| 1 | 59 | | SetIdleState(true); |
| | 60 | | } |
| 6994 | 61 | | } |
| | 62 | |
|
| | 63 | | private void SetIdleState(bool status) |
| | 64 | | { |
| 1 | 65 | | idle = status; |
| | 66 | |
|
| 1 | 67 | | OnChangeStatus?.Invoke(idle); |
| | 68 | |
|
| 1 | 69 | | DCL.Interface.WebInterface.ReportIdleStateChanged(idle); |
| 1 | 70 | | } |
| | 71 | |
|
| 6994 | 72 | | private bool IdleCheck() { return Time.time - lastActivityTime > maxTime; } |
| | 73 | |
|
| 0 | 74 | | public bool isIdle() { return idle; } |
| | 75 | |
|
| | 76 | | public void Dispose() |
| | 77 | | { |
| 629 | 78 | | updateEventHandler?.RemoveListener(IUpdateEventHandler.EventType.Update, Update); |
| 629 | 79 | | } |
| | 80 | | } |
| | 81 | | } |