| | 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 | |
|
| | 11 | | namespace DCL.Components |
| | 12 | | { |
| | 13 | | public class OnPointerEventHandler : IDisposable |
| | 14 | | { |
| 1 | 15 | | public static bool enableInteractionHoverFeedback = true; |
| 37 | 16 | | public OnPointerEventColliders eventColliders { get; private set; } |
| | 17 | |
|
| | 18 | | private IDCLEntity entity; |
| | 19 | |
|
| 37 | 20 | | public OnPointerEventHandler() |
| | 21 | | { |
| 37 | 22 | | eventColliders = new OnPointerEventColliders(); |
| 37 | 23 | | } |
| | 24 | |
|
| | 25 | | public void SetColliders(IDCLEntity entity) |
| | 26 | | { |
| 75 | 27 | | this.entity = entity; |
| 75 | 28 | | eventColliders.Initialize(entity); |
| 75 | 29 | | } |
| | 30 | |
|
| | 31 | | public void SetFeedbackState(bool showFeedback, bool hoverState, string button, string hoverText) |
| | 32 | | { |
| 1150 | 33 | | if (!enableInteractionHoverFeedback) |
| 0 | 34 | | return; |
| | 35 | |
|
| 1150 | 36 | | var cursorData = DataStore.i.Get<DataStore_Cursor>(); |
| 1150 | 37 | | cursorData.hoverFeedbackEnabled.Set(showFeedback); |
| | 38 | |
|
| 1150 | 39 | | if (showFeedback) |
| | 40 | | { |
| 1150 | 41 | | if (hoverState) |
| | 42 | | { |
| 1144 | 43 | | cursorData.hoverFeedbackButton.Set(button); |
| 1144 | 44 | | cursorData.hoverFeedbackText.Set(hoverText); |
| | 45 | | } |
| | 46 | |
|
| 1150 | 47 | | cursorData.hoverFeedbackHoverState.Set(hoverState); |
| | 48 | | } |
| 1150 | 49 | | } |
| | 50 | |
|
| | 51 | | public string GetMeshName(Collider collider) |
| | 52 | | { |
| 6 | 53 | | if (collider == null || eventColliders == null) |
| 0 | 54 | | return null; |
| | 55 | |
|
| 6 | 56 | | return eventColliders.GetMeshName(collider); |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public void Dispose() |
| | 60 | | { |
| 37 | 61 | | eventColliders.Dispose(); |
| 37 | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| | 65 | | public class OnPointerEvent : UUIDComponent, IPointerInputEvent |
| | 66 | | { |
| | 67 | | public static bool enableInteractionHoverFeedback = true; |
| | 68 | |
|
| | 69 | | [System.Serializable] |
| | 70 | | public new class Model : UUIDComponent.Model |
| | 71 | | { |
| | 72 | | public string button = WebInterface.ACTION_BUTTON.ANY.ToString(); |
| | 73 | | public string hoverText = "Interact"; |
| | 74 | | public float distance = 10f; |
| | 75 | | public bool showFeedback = true; |
| | 76 | |
|
| | 77 | | public override BaseModel GetDataFromJSON(string json) |
| | 78 | | { |
| | 79 | | return Utils.SafeFromJson<Model>(json); |
| | 80 | | } |
| | 81 | |
|
| | 82 | | public WebInterface.ACTION_BUTTON GetActionButton() |
| | 83 | | { |
| | 84 | | switch (button) |
| | 85 | | { |
| | 86 | | case "PRIMARY": |
| | 87 | | return WebInterface.ACTION_BUTTON.PRIMARY; |
| | 88 | | case "SECONDARY": |
| | 89 | | return WebInterface.ACTION_BUTTON.SECONDARY; |
| | 90 | | case "POINTER": |
| | 91 | | return WebInterface.ACTION_BUTTON.POINTER; |
| | 92 | | default: |
| | 93 | | return WebInterface.ACTION_BUTTON.ANY; |
| | 94 | | } |
| | 95 | | } |
| | 96 | | } |
| | 97 | |
|
| | 98 | | public OnPointerEventHandler pointerEventHandler; |
| | 99 | |
|
| | 100 | | public override void Initialize(IParcelScene scene, IDCLEntity entity) |
| | 101 | | { |
| | 102 | | base.Initialize(scene, entity); |
| | 103 | |
|
| | 104 | | if (model == null) |
| | 105 | | model = new OnPointerEvent.Model(); |
| | 106 | |
|
| | 107 | | pointerEventHandler = new OnPointerEventHandler(); |
| | 108 | | SetEventColliders(entity); |
| | 109 | |
|
| | 110 | | entity.OnShapeUpdated -= SetEventColliders; |
| | 111 | | entity.OnShapeUpdated += SetEventColliders; |
| | 112 | | } |
| | 113 | |
|
| | 114 | | public WebInterface.ACTION_BUTTON GetActionButton() |
| | 115 | | { |
| | 116 | | return ((Model) this.model).GetActionButton(); |
| | 117 | | } |
| | 118 | |
|
| | 119 | | public void SetHoverState(bool hoverState) |
| | 120 | | { |
| | 121 | | Model model = (Model) this.model; |
| | 122 | | pointerEventHandler.SetFeedbackState(model.showFeedback, hoverState, model.button, model.hoverText); |
| | 123 | | } |
| | 124 | |
|
| | 125 | | void SetEventColliders(IDCLEntity entity) |
| | 126 | | { |
| | 127 | | pointerEventHandler.SetColliders(entity); |
| | 128 | | } |
| | 129 | |
|
| | 130 | | public bool IsVisible() |
| | 131 | | { |
| | 132 | | if (entity == null) |
| | 133 | | return false; |
| | 134 | |
|
| | 135 | | bool isVisible = false; |
| | 136 | |
|
| | 137 | | if (entity.meshesInfo != null && |
| | 138 | | entity.meshesInfo.renderers != null && |
| | 139 | | entity.meshesInfo.renderers.Length > 0) |
| | 140 | | { |
| | 141 | | isVisible = entity.meshesInfo.renderers[0].enabled; |
| | 142 | | } |
| | 143 | |
|
| | 144 | | return isVisible; |
| | 145 | | } |
| | 146 | |
|
| | 147 | | public bool IsAtHoverDistance(float distance) |
| | 148 | | { |
| | 149 | | Model model = this.model as Model; |
| | 150 | | return distance <= model.distance; |
| | 151 | | } |
| | 152 | |
|
| | 153 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 154 | | { |
| | 155 | | this.model = newModel ?? new Model(); |
| | 156 | | return null; |
| | 157 | | } |
| | 158 | |
|
| | 159 | | public bool ShouldShowHoverFeedback() |
| | 160 | | { |
| | 161 | | Model model = this.model as Model; |
| | 162 | | return model.showFeedback; |
| | 163 | | } |
| | 164 | |
|
| | 165 | | void OnDestroy() |
| | 166 | | { |
| | 167 | | if (entity != null) |
| | 168 | | entity.OnShapeUpdated -= SetEventColliders; |
| | 169 | |
|
| | 170 | | pointerEventHandler.Dispose(); |
| | 171 | | } |
| | 172 | |
|
| | 173 | | public virtual void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit) |
| | 174 | | { |
| | 175 | | } |
| | 176 | |
|
| | 177 | | public virtual PointerInputEventType GetEventType() |
| | 178 | | { |
| | 179 | | return PointerInputEventType.NONE; |
| | 180 | | } |
| | 181 | | } |
| | 182 | | } |