| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Components; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.ECS7; |
| | 6 | | using DCL.ECSComponents; |
| | 7 | | using DCL.ECSRuntime; |
| | 8 | | using DCL.Helpers; |
| | 9 | | using DCL.Interface; |
| | 10 | | using DCL.Models; |
| | 11 | | using DCLPlugins.UUIDEventComponentsPlugin.UUIDComponent.Interfaces; |
| | 12 | | using UnityEngine; |
| | 13 | | using Ray = UnityEngine.Ray; |
| | 14 | |
|
| | 15 | | namespace DCLPlugins.ECSComponents |
| | 16 | | { |
| | 17 | | public class PointerInputRepresentantion : IPointerInputEvent |
| | 18 | | { |
| | 19 | | private static int lamportTimestamp = 0; |
| | 20 | | private IDCLEntity eventEntity; |
| | 21 | | private IParcelScene scene; |
| | 22 | | private readonly OnPointerEventHandler pointerEventHandler; |
| | 23 | | private readonly PointerInputEventType type; |
| | 24 | | private readonly IECSComponentWriter componentWriter; |
| | 25 | | private readonly DataStore_ECS7 dataStore; |
| | 26 | |
|
| | 27 | | // This represents the model component, since we have several components model, we just use their data |
| | 28 | | private bool showFeedback = false; |
| | 29 | | private ActionButton button; |
| | 30 | | private string hoverText; |
| | 31 | | private float distance; |
| | 32 | |
|
| 8 | 33 | | public PointerInputRepresentantion(IDCLEntity entity,DataStore_ECS7 dataStore,PointerInputEventType type, IECSCo |
| | 34 | | { |
| 8 | 35 | | this.dataStore = dataStore; |
| 8 | 36 | | this.type = type; |
| 8 | 37 | | this.componentWriter = componentWriter; |
| 8 | 38 | | pointerEventHandler = new OnPointerEventHandler(); |
| | 39 | |
|
| 8 | 40 | | Initializate(entity); |
| 8 | 41 | | } |
| | 42 | |
|
| | 43 | | public void SetData(IParcelScene scene, IDCLEntity entity, bool showFeedback, ActionButton button, float distanc |
| | 44 | | { |
| 8 | 45 | | this.scene = scene; |
| 8 | 46 | | eventEntity = entity; |
| | 47 | |
|
| 8 | 48 | | this.showFeedback = showFeedback; |
| 8 | 49 | | this.button = button; |
| 8 | 50 | | this.distance = distance; |
| 8 | 51 | | this.hoverText = hoverText; |
| 8 | 52 | | } |
| | 53 | |
|
| | 54 | | public void Dispose() |
| | 55 | | { |
| 0 | 56 | | dataStore.shapesReady.OnAdded -= ConfigureColliders; |
| 0 | 57 | | pointerEventHandler.Dispose(); |
| 0 | 58 | | } |
| | 59 | |
|
| 0 | 60 | | public Transform GetTransform() { return eventEntity.gameObject.transform; } |
| | 61 | |
|
| 0 | 62 | | IDCLEntity IPointerEvent.entity => eventEntity; |
| | 63 | |
|
| | 64 | | void IPointerEvent.SetHoverState(bool hoverState) |
| | 65 | | { |
| 0 | 66 | | pointerEventHandler.SetFeedbackState(showFeedback, hoverState, button.ToString(), hoverText); |
| 0 | 67 | | } |
| | 68 | |
|
| 0 | 69 | | public bool /*IPointerEvent*/ IsAtHoverDistance(float distance) => distance <= this.distance; |
| | 70 | |
|
| | 71 | | public bool /*IPointerEvent*/ IsVisible() |
| | 72 | | { |
| 0 | 73 | | if (eventEntity == null) |
| 0 | 74 | | return false; |
| | 75 | |
|
| 0 | 76 | | bool isVisible = false; |
| | 77 | |
|
| 0 | 78 | | if (eventEntity.meshesInfo != null && |
| | 79 | | eventEntity.meshesInfo.renderers != null && |
| | 80 | | eventEntity.meshesInfo.renderers.Length > 0) |
| | 81 | | { |
| 0 | 82 | | isVisible = eventEntity.meshesInfo.renderers[0].enabled; |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | return isVisible; |
| | 86 | | } |
| | 87 | |
|
| | 88 | | void IPointerInputEvent.Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit) |
| | 89 | | { |
| 0 | 90 | | if (!IsVisible()) |
| 0 | 91 | | return; |
| | 92 | |
|
| 0 | 93 | | if (ShouldReportEvent(buttonId, hit)) |
| | 94 | | { |
| 0 | 95 | | string meshName = pointerEventHandler.GetMeshName(hit.collider); |
| 0 | 96 | | long entityId = eventEntity.entityId; |
| | 97 | | int componentId; |
| | 98 | |
|
| | 99 | | // We have to differentiate between OnPointerDown and OnPointerUp |
| 0 | 100 | | switch (type) |
| | 101 | | { |
| | 102 | | case PointerInputEventType.DOWN: |
| 0 | 103 | | componentId = ComponentID.ON_POINTER_DOWN_RESULT; |
| | 104 | |
|
| 0 | 105 | | PBOnPointerDownResult downPayload = ProtoConvertUtils.GetPointerDownResultModel(button, meshName |
| 0 | 106 | | downPayload.Timestamp = GetLamportTimestamp(); |
| 0 | 107 | | componentWriter.PutComponent(scene.sceneData.id, entityId, componentId, |
| | 108 | | downPayload); |
| 0 | 109 | | break; |
| | 110 | | case PointerInputEventType.UP: |
| 0 | 111 | | componentId = ComponentID.ON_POINTER_UP_RESULT; |
| | 112 | |
|
| 0 | 113 | | PBOnPointerUpResult payload = ProtoConvertUtils.GetPointerUpResultModel(button, meshName, ray, h |
| 0 | 114 | | payload.Timestamp = GetLamportTimestamp(); |
| 0 | 115 | | componentWriter.PutComponent(scene.sceneData.id, entityId, componentId, |
| | 116 | | payload); |
| | 117 | | break; |
| | 118 | | } |
| | 119 | | } |
| 0 | 120 | | } |
| | 121 | |
|
| 0 | 122 | | PointerInputEventType IPointerInputEvent.GetEventType() => type; |
| | 123 | |
|
| 0 | 124 | | WebInterface.ACTION_BUTTON IPointerInputEvent.GetActionButton() => (WebInterface.ACTION_BUTTON) button; |
| | 125 | |
|
| 0 | 126 | | bool IPointerInputEvent.ShouldShowHoverFeedback() => showFeedback; |
| | 127 | |
|
| 0 | 128 | | private void ConfigureColliders(long entityId, GameObject shapeGameObject) => pointerEventHandler.SetColliders(e |
| | 129 | |
|
| | 130 | | private bool ShouldReportEvent(WebInterface.ACTION_BUTTON buttonId, HitInfo hit) |
| | 131 | | { |
| 0 | 132 | | return IsVisible() && |
| | 133 | | IsAtHoverDistance(hit.distance) && |
| | 134 | | ((int)button == (int)WebInterface.ACTION_BUTTON.ANY || buttonId == (WebInterface.ACTION_BUTTON)button |
| | 135 | | } |
| | 136 | |
|
| | 137 | | private void Initializate(IDCLEntity entity) |
| | 138 | | { |
| 8 | 139 | | if(dataStore.shapesReady.ContainsKey(entity.entityId)) |
| 0 | 140 | | pointerEventHandler.SetColliders(entity); |
| | 141 | |
|
| 8 | 142 | | dataStore.shapesReady.OnAdded += ConfigureColliders; |
| 8 | 143 | | } |
| | 144 | |
|
| | 145 | | private int GetLamportTimestamp() |
| | 146 | | { |
| 0 | 147 | | int result = lamportTimestamp; |
| 0 | 148 | | lamportTimestamp++; |
| 0 | 149 | | return result; |
| | 150 | | } |
| | 151 | | } |
| | 152 | | } |