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