| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.EventSystems; |
| | 4 | | using UnityEngine.UI; |
| | 5 | | using Object = UnityEngine.Object; |
| | 6 | |
|
| | 7 | | public interface IQuickBarView |
| | 8 | | { |
| | 9 | | event Action<int> OnQuickBarInputTriggered; |
| | 10 | | event Action<int> OnQuickBarObjectSelected; |
| | 11 | | event Action<int, int, Texture> OnSceneObjectDroppedFromQuickBar; |
| | 12 | | event Action<BaseEventData> OnSceneObjectDroppedFromCatalog; |
| | 13 | | event Action<int> OnSetIndexToBeginDrag; |
| | 14 | | event Action<int> OnSetIndexToDrop; |
| | 15 | |
|
| | 16 | | void RaiseQuickBarInputTriggered(int index); |
| | 17 | | void QuickBarObjectSelected(int index); |
| | 18 | | void SceneObjectDroppedFromQuickBar(int fromIndex, int toIndex, Texture texture); |
| | 19 | | void SceneObjectDroppedFromCatalog(BaseEventData data); |
| | 20 | | void SetIndexToBeginDrag(int index); |
| | 21 | | void SetIndexToDrop(int index); |
| | 22 | | void SetTextureToShortcut(int shortcutIndex, Texture texture); |
| | 23 | | void SetShortcutAsEmpty(int shortcutIndex); |
| | 24 | | void BeginDragSlot(int triggerIndex); |
| | 25 | | void DragSlot(BaseEventData eventData, int triggerIndex); |
| | 26 | | void EndDragSlot(int triggerIndex); |
| | 27 | | void CancelCurrentDragging(); |
| | 28 | | } |
| | 29 | |
|
| | 30 | | public class QuickBarView : MonoBehaviour, IQuickBarView |
| | 31 | | { |
| | 32 | | private const string VIEW_PATH = "Common/QuickBarView"; |
| | 33 | |
|
| | 34 | | public event Action<int> OnQuickBarObjectSelected; |
| | 35 | | public event Action<int> OnSetIndexToBeginDrag; |
| | 36 | | public event Action<int> OnSetIndexToDrop; |
| | 37 | | public event Action<int, int, Texture> OnSceneObjectDroppedFromQuickBar; |
| | 38 | | public event Action<BaseEventData> OnSceneObjectDroppedFromCatalog; |
| | 39 | | public event Action<int> OnQuickBarInputTriggered; |
| | 40 | |
|
| | 41 | | [SerializeField] internal Canvas generalCanvas; |
| | 42 | | [SerializeField] internal QuickBarSlot[] shortcutsImgs; |
| | 43 | | [SerializeField] internal Button[] shortcutsButtons; |
| | 44 | | [SerializeField] internal EventTrigger[] shortcutsEventTriggers; |
| | 45 | | [SerializeField] internal InputAction_Trigger quickBar1InputAction; |
| | 46 | | [SerializeField] internal InputAction_Trigger quickBar2InputAction; |
| | 47 | | [SerializeField] internal InputAction_Trigger quickBar3InputAction; |
| | 48 | | [SerializeField] internal InputAction_Trigger quickBar4InputAction; |
| | 49 | | [SerializeField] internal InputAction_Trigger quickBar5InputAction; |
| | 50 | | [SerializeField] internal InputAction_Trigger quickBar6InputAction; |
| | 51 | | [SerializeField] internal InputAction_Trigger quickBar7InputAction; |
| | 52 | | [SerializeField] internal InputAction_Trigger quickBar8InputAction; |
| | 53 | | [SerializeField] internal InputAction_Trigger quickBar9InputAction; |
| | 54 | |
|
| 32 | 55 | | internal int lastIndexToBeginDrag = -1; |
| | 56 | | internal QuickBarSlot draggedSlot = null; |
| | 57 | |
|
| | 58 | | internal static QuickBarView Create() |
| | 59 | | { |
| 11 | 60 | | var view = Instantiate(Resources.Load<GameObject>(VIEW_PATH)).GetComponent<QuickBarView>(); |
| 11 | 61 | | view.gameObject.name = "_QuickBarView"; |
| | 62 | |
|
| 11 | 63 | | return view; |
| | 64 | | } |
| | 65 | |
|
| | 66 | | private void Awake() |
| | 67 | | { |
| 30 | 68 | | CreateSlotToDrag(); |
| | 69 | |
|
| 600 | 70 | | for (int i = 0; i < shortcutsButtons.Length; i++) |
| | 71 | | { |
| 270 | 72 | | int buttonIndex = i; |
| 270 | 73 | | shortcutsButtons[buttonIndex].onClick.AddListener(() => QuickBarObjectSelected(buttonIndex)); |
| | 74 | | } |
| | 75 | |
|
| 600 | 76 | | for (int i = 0; i < shortcutsEventTriggers.Length; i++) |
| | 77 | | { |
| 270 | 78 | | int triggerIndex = i; |
| | 79 | |
|
| 270 | 80 | | BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.BeginDrag, (eventData) |
| | 81 | | { |
| 0 | 82 | | BeginDragSlot(triggerIndex); |
| 0 | 83 | | }); |
| | 84 | |
|
| 270 | 85 | | BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.Drag, (eventData) => |
| | 86 | | { |
| 0 | 87 | | DragSlot(eventData, triggerIndex); |
| 0 | 88 | | }); |
| | 89 | |
|
| 270 | 90 | | BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.EndDrag, (eventData) = |
| | 91 | | { |
| 0 | 92 | | EndDragSlot(triggerIndex); |
| 0 | 93 | | }); |
| | 94 | |
|
| 270 | 95 | | BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.Drop, (eventData) => |
| | 96 | | { |
| 0 | 97 | | SetIndexToDrop(triggerIndex); |
| | 98 | |
|
| 0 | 99 | | if (lastIndexToBeginDrag != -1) |
| | 100 | | { |
| 0 | 101 | | SceneObjectDroppedFromQuickBar(lastIndexToBeginDrag, triggerIndex, shortcutsImgs[lastIndexToBeginDra |
| 0 | 102 | | CancelCurrentDragging(); |
| 0 | 103 | | } |
| | 104 | | else |
| | 105 | | { |
| 0 | 106 | | SceneObjectDroppedFromCatalog(eventData); |
| | 107 | | } |
| 0 | 108 | | }); |
| | 109 | | } |
| | 110 | |
|
| 30 | 111 | | quickBar1InputAction.OnTriggered += OnQuickBar1InputTriggered; |
| 30 | 112 | | quickBar2InputAction.OnTriggered += OnQuickBar2InputTriggered; |
| 30 | 113 | | quickBar3InputAction.OnTriggered += OnQuickBar3InputTriggered; |
| 30 | 114 | | quickBar4InputAction.OnTriggered += OnQuickBar4InputTriggered; |
| 30 | 115 | | quickBar5InputAction.OnTriggered += OnQuickBar5InputTriggered; |
| 30 | 116 | | quickBar6InputAction.OnTriggered += OnQuickBar6InputTriggered; |
| 30 | 117 | | quickBar7InputAction.OnTriggered += OnQuickBar7InputTriggered; |
| 30 | 118 | | quickBar8InputAction.OnTriggered += OnQuickBar8InputTriggered; |
| 30 | 119 | | quickBar9InputAction.OnTriggered += OnQuickBar9InputTriggered; |
| 30 | 120 | | } |
| | 121 | |
|
| | 122 | | private void OnDestroy() |
| | 123 | | { |
| 600 | 124 | | for (int i = 0; i < shortcutsButtons.Length; i++) |
| | 125 | | { |
| 270 | 126 | | int buttonIndex = i; |
| 270 | 127 | | shortcutsButtons[buttonIndex].onClick.RemoveAllListeners(); |
| | 128 | | } |
| | 129 | |
|
| 600 | 130 | | for (int i = 0; i < shortcutsEventTriggers.Length; i++) |
| | 131 | | { |
| 270 | 132 | | int triggerIndex = i; |
| 270 | 133 | | BIWUtils.RemoveEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.Drop); |
| | 134 | | } |
| | 135 | |
|
| | 136 | | // TODO(Brian): We should use an array here |
| 30 | 137 | | quickBar1InputAction.OnTriggered -= OnQuickBar1InputTriggered; |
| 30 | 138 | | quickBar2InputAction.OnTriggered -= OnQuickBar2InputTriggered; |
| 30 | 139 | | quickBar3InputAction.OnTriggered -= OnQuickBar3InputTriggered; |
| 30 | 140 | | quickBar4InputAction.OnTriggered -= OnQuickBar4InputTriggered; |
| 30 | 141 | | quickBar5InputAction.OnTriggered -= OnQuickBar5InputTriggered; |
| 30 | 142 | | quickBar6InputAction.OnTriggered -= OnQuickBar6InputTriggered; |
| 30 | 143 | | quickBar7InputAction.OnTriggered -= OnQuickBar7InputTriggered; |
| 30 | 144 | | quickBar8InputAction.OnTriggered -= OnQuickBar8InputTriggered; |
| 30 | 145 | | quickBar9InputAction.OnTriggered -= OnQuickBar9InputTriggered; |
| | 146 | |
|
| 30 | 147 | | if (draggedSlot != null) |
| 29 | 148 | | Object.Destroy(draggedSlot.gameObject); |
| 30 | 149 | | } |
| | 150 | |
|
| 4 | 151 | | public void QuickBarObjectSelected(int index) { OnQuickBarObjectSelected?.Invoke(index); } |
| | 152 | |
|
| 3 | 153 | | public void SetIndexToBeginDrag(int index) { OnSetIndexToBeginDrag?.Invoke(index); } |
| | 154 | |
|
| 2 | 155 | | public void SetIndexToDrop(int index) { OnSetIndexToDrop?.Invoke(index); } |
| | 156 | |
|
| 2 | 157 | | public void SceneObjectDroppedFromQuickBar(int fromIndex, int toIndex, Texture texture) { OnSceneObjectDroppedFromQu |
| | 158 | |
|
| 2 | 159 | | public void SceneObjectDroppedFromCatalog(BaseEventData data) { OnSceneObjectDroppedFromCatalog?.Invoke(data); } |
| | 160 | |
|
| | 161 | | public void SetTextureToShortcut(int shortcutIndex, Texture texture) |
| | 162 | | { |
| 9 | 163 | | if (shortcutIndex >= shortcutsImgs.Length) |
| 0 | 164 | | return; |
| | 165 | |
|
| 9 | 166 | | if (shortcutsImgs[shortcutIndex] != null && texture != null) |
| 0 | 167 | | shortcutsImgs[shortcutIndex].SetTexture(texture); |
| 9 | 168 | | } |
| | 169 | |
|
| | 170 | | public void SetShortcutAsEmpty(int shortcutIndex) |
| | 171 | | { |
| 0 | 172 | | if (shortcutIndex >= shortcutsImgs.Length) |
| 0 | 173 | | return; |
| | 174 | |
|
| 0 | 175 | | shortcutsImgs[shortcutIndex].SetEmpty(); |
| 0 | 176 | | } |
| | 177 | |
|
| 0 | 178 | | private void OnQuickBar1InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(0); } |
| | 179 | |
|
| 0 | 180 | | private void OnQuickBar2InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(1); } |
| | 181 | |
|
| 0 | 182 | | private void OnQuickBar3InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(2); } |
| | 183 | |
|
| 0 | 184 | | private void OnQuickBar4InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(3); } |
| | 185 | |
|
| 0 | 186 | | private void OnQuickBar5InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(4); } |
| | 187 | |
|
| 0 | 188 | | private void OnQuickBar6InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(5); } |
| | 189 | |
|
| 0 | 190 | | private void OnQuickBar7InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(6); } |
| | 191 | |
|
| 0 | 192 | | private void OnQuickBar8InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(7); } |
| | 193 | |
|
| 0 | 194 | | private void OnQuickBar9InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(8); } |
| | 195 | |
|
| 2 | 196 | | public void RaiseQuickBarInputTriggered(int index) { OnQuickBarInputTriggered?.Invoke(index); } |
| | 197 | |
|
| | 198 | | internal void CreateSlotToDrag() |
| | 199 | | { |
| 30 | 200 | | if (shortcutsImgs.Length == 0) |
| 0 | 201 | | return; |
| | 202 | |
|
| 30 | 203 | | draggedSlot = Instantiate(shortcutsImgs[0], generalCanvas != null ? generalCanvas.transform : null); |
| 30 | 204 | | draggedSlot.EnableDragMode(); |
| 30 | 205 | | draggedSlot.SetEmpty(); |
| 30 | 206 | | draggedSlot.SetActive(false); |
| 30 | 207 | | } |
| | 208 | |
|
| | 209 | | public void BeginDragSlot(int triggerIndex) |
| | 210 | | { |
| 1 | 211 | | if (draggedSlot == null || shortcutsImgs[triggerIndex].isEmpty) |
| 0 | 212 | | return; |
| | 213 | |
|
| 1 | 214 | | lastIndexToBeginDrag = triggerIndex; |
| 1 | 215 | | SetIndexToBeginDrag(triggerIndex); |
| 1 | 216 | | draggedSlot.SetActive(true); |
| 1 | 217 | | draggedSlot.SetTexture(shortcutsImgs[triggerIndex].image.texture); |
| 1 | 218 | | } |
| | 219 | |
|
| | 220 | | public void DragSlot(BaseEventData eventData, int triggerIndex) |
| | 221 | | { |
| 1 | 222 | | if (draggedSlot == null || shortcutsImgs[triggerIndex].isEmpty) |
| 0 | 223 | | return; |
| | 224 | |
|
| 1 | 225 | | draggedSlot.slotTransform.position = ((PointerEventData)eventData).position; |
| 1 | 226 | | } |
| | 227 | |
|
| | 228 | | public void EndDragSlot(int triggerIndex) |
| | 229 | | { |
| 1 | 230 | | if (draggedSlot == null || shortcutsImgs[triggerIndex].isEmpty) |
| 0 | 231 | | return; |
| | 232 | |
|
| 1 | 233 | | draggedSlot.SetEmpty(); |
| 1 | 234 | | draggedSlot.SetActive(false); |
| 1 | 235 | | QuickBarObjectSelected(triggerIndex); |
| 1 | 236 | | } |
| | 237 | |
|
| | 238 | | public void CancelCurrentDragging() |
| | 239 | | { |
| 1 | 240 | | lastIndexToBeginDrag = -1; |
| | 241 | |
|
| 1 | 242 | | if (draggedSlot != null) |
| | 243 | | { |
| 1 | 244 | | draggedSlot.SetEmpty(); |
| 1 | 245 | | draggedSlot.SetActive(false); |
| | 246 | | } |
| 1 | 247 | | } |
| | 248 | | } |