| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Interface; |
| | 6 | | using DCL.Models; |
| | 7 | | using DCLPlugins.UUIDEventComponentsPlugin.UUIDComponent.Interfaces; |
| | 8 | | using UnityEngine; |
| | 9 | | using Ray = UnityEngine.Ray; |
| | 10 | | using Decentraland.Sdk.Ecs6; |
| | 11 | |
|
| | 12 | | namespace DCL.Components |
| | 13 | | { |
| | 14 | | public class OnPointerEventHandler : IDisposable |
| | 15 | | { |
| | 16 | | public static bool enableInteractionHoverFeedback = true; |
| | 17 | | public OnPointerEventColliders eventColliders { get; private set; } |
| | 18 | |
|
| | 19 | | private IDCLEntity entity; |
| | 20 | |
|
| | 21 | | public OnPointerEventHandler() |
| | 22 | | { |
| | 23 | | eventColliders = new OnPointerEventColliders(); |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public void SetColliders(IDCLEntity entity) |
| | 27 | | { |
| | 28 | | this.entity = entity; |
| | 29 | | eventColliders.Initialize(entity); |
| | 30 | | } |
| | 31 | |
|
| | 32 | | public void UpdateCollidersEnabledBasedOnRenderers(IDCLEntity entity) |
| | 33 | | { |
| | 34 | | this.entity = entity; |
| | 35 | | eventColliders.UpdateCollidersEnabledBasedOnRenderers(entity); |
| | 36 | | } |
| | 37 | |
|
| | 38 | | public void SetFeedbackState(bool showFeedback, bool hoverState, string button, string hoverText) |
| | 39 | | { |
| | 40 | | if (!enableInteractionHoverFeedback) |
| | 41 | | return; |
| | 42 | |
|
| | 43 | | var cursorData = DataStore.i.Get<DataStore_Cursor>(); |
| | 44 | | cursorData.hoverFeedbackEnabled.Set(showFeedback); |
| | 45 | |
|
| | 46 | | if (showFeedback) |
| | 47 | | { |
| | 48 | | if (hoverState) |
| | 49 | | { |
| | 50 | | cursorData.hoverFeedbackButton.Set(button); |
| | 51 | | cursorData.hoverFeedbackText.Set(hoverText); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | cursorData.hoverFeedbackHoverState.Set(hoverState); |
| | 55 | | } |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public string GetMeshName(Collider collider) |
| | 59 | | { |
| | 60 | | if (collider == null || eventColliders == null) |
| | 61 | | return null; |
| | 62 | |
|
| | 63 | | return eventColliders.GetMeshName(collider); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | public void Dispose() |
| | 67 | | { |
| | 68 | | eventColliders.Dispose(); |
| | 69 | | } |
| | 70 | | } |
| | 71 | |
|
| | 72 | | public class OnPointerEvent : UUIDComponent, IPointerInputEvent, IOutOfSceneBoundariesHandler |
| | 73 | | { |
| 1 | 74 | | public static bool enableInteractionHoverFeedback = true; |
| | 75 | |
|
| | 76 | | [Serializable] |
| | 77 | | public new class Model : UUIDComponent.Model |
| | 78 | | { |
| 934 | 79 | | public string button = WebInterface.ACTION_BUTTON.ANY.ToString(); |
| 934 | 80 | | public string hoverText = "Interact"; |
| 934 | 81 | | public float distance = 10f; |
| 934 | 82 | | public bool showFeedback = true; |
| | 83 | |
|
| | 84 | | public override BaseModel GetDataFromJSON(string json) => |
| 124 | 85 | | Utils.SafeFromJson<Model>(json); |
| | 86 | |
|
| | 87 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 88 | | { |
| 0 | 89 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.UuidCallback) |
| 0 | 90 | | return Utils.SafeUnimplemented<OnPointerEvent, Model>(expected: ComponentBodyPayload.PayloadOneofCas |
| | 91 | |
|
| 0 | 92 | | var pb = new Model(); |
| | 93 | |
|
| 0 | 94 | | if (pbModel.UuidCallback.HasUuid) pb.uuid = pbModel.UuidCallback.Uuid; |
| 0 | 95 | | if (pbModel.UuidCallback.HasType) pb.type = pbModel.UuidCallback.Type; |
| 0 | 96 | | if (pbModel.UuidCallback.HasButton) pb.button = pbModel.UuidCallback.Button; |
| 0 | 97 | | if (pbModel.UuidCallback.HasHoverText) pb.hoverText = pbModel.UuidCallback.HoverText; |
| 0 | 98 | | if (pbModel.UuidCallback.HasDistance) pb.distance = pbModel.UuidCallback.Distance; |
| 0 | 99 | | if (pbModel.UuidCallback.HasShowFeedback) pb.showFeedback = pbModel.UuidCallback.ShowFeedback; |
| | 100 | |
|
| 0 | 101 | | return pb; |
| | 102 | | } |
| | 103 | |
|
| | 104 | | public WebInterface.ACTION_BUTTON GetActionButton() => |
| 6499 | 105 | | button switch |
| | 106 | | { |
| 1 | 107 | | "PRIMARY" => WebInterface.ACTION_BUTTON.PRIMARY, |
| 0 | 108 | | "SECONDARY" => WebInterface.ACTION_BUTTON.SECONDARY, |
| 0 | 109 | | "POINTER" => WebInterface.ACTION_BUTTON.POINTER, |
| 6498 | 110 | | _ => WebInterface.ACTION_BUTTON.ANY, |
| | 111 | | }; |
| | 112 | | } |
| | 113 | |
|
| | 114 | | public OnPointerEventHandler pointerEventHandler; |
| | 115 | |
|
| | 116 | | public override void Initialize(IParcelScene scene, IDCLEntity entity) |
| | 117 | | { |
| 38 | 118 | | base.Initialize(scene, entity); |
| | 119 | |
|
| 38 | 120 | | if (model == null) |
| 38 | 121 | | model = new OnPointerEvent.Model(); |
| | 122 | |
|
| 38 | 123 | | pointerEventHandler = new OnPointerEventHandler(); |
| 38 | 124 | | SetEventColliders(entity); |
| | 125 | |
|
| 38 | 126 | | entity.OnShapeUpdated -= SetEventColliders; |
| 38 | 127 | | entity.OnShapeUpdated += SetEventColliders; |
| | 128 | |
|
| 38 | 129 | | DataStore.i.sceneBoundariesChecker.Add(entity,this); |
| 38 | 130 | | } |
| | 131 | |
|
| | 132 | | public WebInterface.ACTION_BUTTON GetActionButton() |
| | 133 | | { |
| 6499 | 134 | | return ((Model) this.model).GetActionButton(); |
| | 135 | | } |
| | 136 | |
|
| | 137 | | public void SetHoverState(bool hoverState) |
| | 138 | | { |
| 6503 | 139 | | Model model = (Model) this.model; |
| 6503 | 140 | | pointerEventHandler.SetFeedbackState(model.showFeedback, hoverState, model.button, model.hoverText); |
| 6503 | 141 | | } |
| | 142 | |
|
| | 143 | | void SetEventColliders(IDCLEntity entity) |
| | 144 | | { |
| 77 | 145 | | pointerEventHandler.SetColliders(entity); |
| 77 | 146 | | } |
| | 147 | |
|
| | 148 | | public bool IsVisible() |
| | 149 | | { |
| 6512 | 150 | | if (entity == null) |
| 0 | 151 | | return false; |
| | 152 | |
|
| 6512 | 153 | | bool isVisible = false; |
| | 154 | |
|
| 6512 | 155 | | if (entity.meshesInfo != null && |
| | 156 | | entity.meshesInfo.renderers != null && |
| | 157 | | entity.meshesInfo.renderers.Length > 0) |
| | 158 | | { |
| 6512 | 159 | | isVisible = entity.meshesInfo.renderers[0].enabled; |
| | 160 | | } |
| | 161 | |
|
| 6512 | 162 | | return isVisible; |
| | 163 | | } |
| | 164 | |
|
| | 165 | | public bool IsAtHoverDistance(float distance) |
| | 166 | | { |
| 6508 | 167 | | Model model = this.model as Model; |
| 6508 | 168 | | return distance <= model.distance; |
| | 169 | | } |
| | 170 | |
|
| | 171 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 172 | | { |
| 54 | 173 | | this.model = newModel ?? new Model(); |
| 54 | 174 | | return null; |
| | 175 | | } |
| | 176 | |
|
| | 177 | | public bool ShouldShowHoverFeedback() |
| | 178 | | { |
| 6497 | 179 | | Model model = this.model as Model; |
| 6497 | 180 | | return model.showFeedback; |
| | 181 | | } |
| | 182 | |
|
| | 183 | | void OnDestroy() |
| | 184 | | { |
| 38 | 185 | | if (entity != null) |
| 38 | 186 | | entity.OnShapeUpdated -= SetEventColliders; |
| | 187 | |
|
| 38 | 188 | | DataStore.i.sceneBoundariesChecker.Remove(entity,this); |
| | 189 | |
|
| 38 | 190 | | pointerEventHandler.Dispose(); |
| 38 | 191 | | } |
| | 192 | |
|
| | 193 | | public virtual void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit) |
| | 194 | | { |
| 0 | 195 | | } |
| | 196 | |
|
| | 197 | | public virtual PointerInputEventType GetEventType() |
| | 198 | | { |
| 0 | 199 | | return PointerInputEventType.NONE; |
| | 200 | | } |
| | 201 | |
|
| | 202 | | public void UpdateOutOfBoundariesState(bool enable) |
| | 203 | | { |
| 1 | 204 | | pointerEventHandler.UpdateCollidersEnabledBasedOnRenderers(entity); |
| 1 | 205 | | } |
| | 206 | | } |
| | 207 | | } |