| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using DCL.ECS7; |
| | 4 | | using DCL.ECS7.InternalComponents; |
| | 5 | | using DCL.ECSComponents; |
| | 6 | | using DCL.ECSRuntime; |
| | 7 | |
|
| | 8 | | namespace ECSSystems.PointerInputSystem |
| | 9 | | { |
| | 10 | | public static class ECSPointerInputSystem |
| | 11 | | { |
| | 12 | | private class State |
| | 13 | | { |
| | 14 | | public IECSComponentWriter componentWriter; |
| | 15 | | public IECSReadOnlyComponentsGroup<InternalColliders, PBOnPointerDown> pointerDownGroup; |
| | 16 | | public IECSReadOnlyComponentsGroup<InternalColliders, PBOnPointerUp> pointerUpGroup; |
| | 17 | | public IInternalECSComponent<InternalColliders> pointerColliderComponent; |
| | 18 | | public ECSComponent<PBOnPointerDown> pointerDownComponent; |
| | 19 | | public ECSComponent<PBOnPointerUp> pointerUpComponent; |
| | 20 | | public DataStore_ECS7 dataStoreEcs7; |
| | 21 | | public DataStore_Cursor dataStoreCursor; |
| | 22 | | public bool isPointerDown; |
| | 23 | | public PointerInputResult lastPointerDownResult; |
| | 24 | | public PointerHoverResult lastPointerHoverResult; |
| | 25 | | } |
| | 26 | |
|
| | 27 | | public static Action CreateSystem( |
| | 28 | | IECSComponentWriter componentWriter, |
| | 29 | | IECSReadOnlyComponentsGroup<InternalColliders, PBOnPointerDown> pointerDownGroup, |
| | 30 | | IECSReadOnlyComponentsGroup<InternalColliders, PBOnPointerUp> pointerUpGroup, |
| | 31 | | IInternalECSComponent<InternalColliders> pointerColliderComponent, |
| | 32 | | ECSComponent<PBOnPointerDown> pointerDownComponent, |
| | 33 | | ECSComponent<PBOnPointerUp> pointerUpComponent, |
| | 34 | | DataStore_ECS7 dataStoreEcs, |
| | 35 | | DataStore_Cursor dataStoreCursor) |
| | 36 | | { |
| 2 | 37 | | var state = new State() |
| | 38 | | { |
| | 39 | | componentWriter = componentWriter, |
| | 40 | | pointerDownGroup = pointerDownGroup, |
| | 41 | | pointerUpGroup = pointerUpGroup, |
| | 42 | | pointerColliderComponent = pointerColliderComponent, |
| | 43 | | pointerDownComponent = pointerDownComponent, |
| | 44 | | pointerUpComponent = pointerUpComponent, |
| | 45 | | dataStoreEcs7 = dataStoreEcs, |
| | 46 | | dataStoreCursor = dataStoreCursor, |
| | 47 | | isPointerDown = false, |
| | 48 | | lastPointerDownResult = PointerInputResult.empty, |
| | 49 | | lastPointerHoverResult = PointerHoverResult.empty |
| | 50 | | }; |
| 7 | 51 | | return () => Update(state); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | private static void Update(State state) |
| | 55 | | { |
| 5 | 56 | | bool isPointerDown = state.isPointerDown; |
| | 57 | |
|
| | 58 | | // process pointer down/up input |
| 5 | 59 | | if (state.dataStoreEcs7.lastPointerInputEvent.HasValue) |
| | 60 | | { |
| 3 | 61 | | isPointerDown = state.dataStoreEcs7.lastPointerInputEvent.Value.isButtonDown; |
| 3 | 62 | | DataStore_ECS7.PointerEvent pointerEvent = state.dataStoreEcs7.lastPointerInputEvent.Value; |
| | 63 | |
|
| 3 | 64 | | PointerInputResult result = PointerDownUpProcessor.ProcessPointerDownUp(pointerEvent, |
| | 65 | | state.pointerColliderComponent, state.pointerDownComponent, state.pointerUpComponent, |
| | 66 | | state.lastPointerDownResult); |
| | 67 | |
|
| 3 | 68 | | state.lastPointerDownResult = result; |
| 3 | 69 | | if (result.hasValue) |
| | 70 | | { |
| 3 | 71 | | if (pointerEvent.isButtonDown) |
| | 72 | | { |
| 2 | 73 | | if (result.shouldTriggerEvent) |
| | 74 | | { |
| 1 | 75 | | state.componentWriter.PutComponent(result.sceneId, result.entityId, |
| | 76 | | ComponentID.ON_POINTER_DOWN_RESULT, |
| | 77 | | ProtoConvertUtils.GetPointerDownResultModel((ActionButton)pointerEvent.buttonId, |
| | 78 | | null, pointerEvent.rayResult.ray, pointerEvent.rayResult.hitInfo.hit), |
| | 79 | | ECSComponentWriteType.SEND_TO_SCENE); |
| | 80 | | } |
| 1 | 81 | | } |
| | 82 | | else |
| | 83 | | { |
| 1 | 84 | | if (result.shouldTriggerEvent) |
| | 85 | | { |
| 1 | 86 | | state.componentWriter.PutComponent(result.sceneId, result.entityId, |
| | 87 | | ComponentID.ON_POINTER_UP_RESULT, |
| | 88 | | ProtoConvertUtils.GetPointerUpResultModel((ActionButton)pointerEvent.buttonId, |
| | 89 | | null, pointerEvent.rayResult.ray, pointerEvent.rayResult.hitInfo.hit), |
| | 90 | | ECSComponentWriteType.SEND_TO_SCENE); |
| | 91 | | } |
| 1 | 92 | | state.lastPointerDownResult = PointerInputResult.empty; |
| | 93 | | } |
| | 94 | | } |
| | 95 | | } |
| | 96 | |
|
| | 97 | | // process pointer hover |
| 5 | 98 | | if (state.dataStoreEcs7.lastPointerRayHit.HasValue) |
| | 99 | | { |
| 0 | 100 | | PointerHoverResult result = PointerHoverProcessor.ProcessPointerHover(isPointerDown, |
| | 101 | | state.dataStoreEcs7.lastPointerRayHit.Value, state.pointerDownGroup, state.pointerUpGroup); |
| | 102 | |
|
| 0 | 103 | | if (isPointerDown) |
| | 104 | | { |
| 0 | 105 | | PointerInputResult lastPointerDown = state.lastPointerDownResult; |
| 0 | 106 | | if (result.hasValue && !result.Equals(state.lastPointerHoverResult) && result.IsSameEntity(lastPoint |
| | 107 | | { |
| 0 | 108 | | state.dataStoreCursor.ShowHoverFeedback(result); |
| | 109 | | } |
| 0 | 110 | | } |
| | 111 | | else |
| | 112 | | { |
| 0 | 113 | | if (result.hasValue && !result.Equals(state.lastPointerHoverResult)) |
| | 114 | | { |
| 0 | 115 | | state.dataStoreCursor.ShowHoverFeedback(result); |
| | 116 | | } |
| | 117 | | } |
| 0 | 118 | | state.lastPointerHoverResult = result; |
| 0 | 119 | | } |
| 5 | 120 | | else if (state.lastPointerHoverResult.hasValue) |
| | 121 | | { |
| 0 | 122 | | state.dataStoreCursor.HideHoverFeedback(); |
| 0 | 123 | | state.lastPointerHoverResult = PointerHoverResult.empty; |
| | 124 | | } |
| | 125 | |
|
| 5 | 126 | | state.dataStoreEcs7.lastPointerInputEvent = null; |
| 5 | 127 | | state.dataStoreEcs7.lastPointerRayHit = null; |
| 5 | 128 | | state.isPointerDown = isPointerDown; |
| 5 | 129 | | } |
| | 130 | |
|
| | 131 | | private static bool IsSameEntity(this PointerHoverResult self, PointerInputResult other) |
| | 132 | | { |
| 0 | 133 | | if (!self.hasValue || !other.hasValue) |
| 0 | 134 | | return false; |
| | 135 | |
|
| 0 | 136 | | return self.sceneId == other.sceneId && self.entityId == other.entityId; |
| | 137 | | } |
| | 138 | |
|
| | 139 | | private static void ShowHoverFeedback(this DataStore_Cursor cursor, PointerHoverResult hoverResult) |
| | 140 | | { |
| 0 | 141 | | if (!hoverResult.hasValue) |
| 0 | 142 | | return; |
| | 143 | |
|
| 0 | 144 | | cursor.hoverFeedbackEnabled.Set(hoverResult.hasFeedback); |
| | 145 | |
|
| 0 | 146 | | if (hoverResult.hasFeedback) |
| | 147 | | { |
| 0 | 148 | | cursor.cursorType.Set(DataStore_Cursor.CursorType.HOVER); |
| 0 | 149 | | cursor.hoverFeedbackButton.Set(hoverResult.buttonId.ToString()); |
| 0 | 150 | | cursor.hoverFeedbackText.Set(hoverResult.text); |
| 0 | 151 | | cursor.hoverFeedbackHoverState.Set(true); |
| | 152 | | } |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | private static void HideHoverFeedback(this DataStore_Cursor cursor) |
| | 156 | | { |
| 0 | 157 | | cursor.cursorType.Set(DataStore_Cursor.CursorType.NORMAL); |
| 0 | 158 | | cursor.hoverFeedbackHoverState.Set(false); |
| 0 | 159 | | } |
| | 160 | | } |
| | 161 | | } |