| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.EventSystems; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | public interface IQuickBarView |
| | 7 | | { |
| | 8 | | event Action<int> OnQuickBarInputTriggered; |
| | 9 | | event Action<int> OnQuickBarObjectSelected; |
| | 10 | | event Action<int, int, Texture> OnSceneObjectDroppedFromQuickBar; |
| | 11 | | event Action<BaseEventData> OnSceneObjectDroppedFromCatalog; |
| | 12 | | event Action<int> OnSetIndexToBeginDrag; |
| | 13 | | event Action<int> OnSetIndexToDrop; |
| | 14 | |
|
| | 15 | | void OnQuickBarInputTriggedered(int index); |
| | 16 | | void QuickBarObjectSelected(int index); |
| | 17 | | void SceneObjectDroppedFromQuickBar(int fromIndex, int toIndex, Texture texture); |
| | 18 | | void SceneObjectDroppedFromCatalog(BaseEventData data); |
| | 19 | | void SetIndexToBeginDrag(int index); |
| | 20 | | void SetIndexToDrop(int index); |
| | 21 | | void SetTextureToShortcut(int shortcutIndex, Texture texture); |
| | 22 | | void SetShortcutAsEmpty(int shortcutIndex); |
| | 23 | | void BeginDragSlot(int triggerIndex); |
| | 24 | | void DragSlot(BaseEventData eventData, int triggerIndex); |
| | 25 | | void EndDragSlot(int triggerIndex); |
| | 26 | | void CancelCurrentDragging(); |
| | 27 | |
|
| | 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 | |
|
| 85 | 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 | | { |
| 56 | 68 | | CreateSlotToDrag(); |
| | 69 | |
|
| 1120 | 70 | | for (int i = 0; i < shortcutsButtons.Length; i++) |
| | 71 | | { |
| 504 | 72 | | int buttonIndex = i; |
| 504 | 73 | | shortcutsButtons[buttonIndex].onClick.AddListener(() => QuickBarObjectSelected(buttonIndex)); |
| | 74 | | } |
| | 75 | |
|
| 1120 | 76 | | for (int i = 0; i < shortcutsEventTriggers.Length; i++) |
| | 77 | | { |
| 504 | 78 | | int triggerIndex = i; |
| | 79 | |
|
| 504 | 80 | | BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.BeginDrag, (eventData) |
| | 81 | | { |
| 0 | 82 | | BeginDragSlot(triggerIndex); |
| 0 | 83 | | }); |
| | 84 | |
|
| 504 | 85 | | BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.Drag, (eventData) => |
| | 86 | | { |
| 0 | 87 | | DragSlot(eventData, triggerIndex); |
| 0 | 88 | | }); |
| | 89 | |
|
| 504 | 90 | | BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.EndDrag, (eventData) = |
| | 91 | | { |
| 0 | 92 | | EndDragSlot(triggerIndex); |
| 0 | 93 | | }); |
| | 94 | |
|
| 504 | 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 | |
|
| 56 | 111 | | quickBar1InputAction.OnTriggered += OnQuickBar1InputTriggedered; |
| 56 | 112 | | quickBar2InputAction.OnTriggered += OnQuickBar2InputTriggedered; |
| 56 | 113 | | quickBar3InputAction.OnTriggered += OnQuickBar3InputTriggedered; |
| 56 | 114 | | quickBar4InputAction.OnTriggered += OnQuickBar4InputTriggedered; |
| 56 | 115 | | quickBar5InputAction.OnTriggered += OnQuickBar5InputTriggedered; |
| 56 | 116 | | quickBar6InputAction.OnTriggered += OnQuickBar6InputTriggedered; |
| 56 | 117 | | quickBar7InputAction.OnTriggered += OnQuickBar7InputTriggedered; |
| 56 | 118 | | quickBar8InputAction.OnTriggered += OnQuickBar8InputTriggedered; |
| 56 | 119 | | quickBar9InputAction.OnTriggered += OnQuickBar9InputTriggedered; |
| 56 | 120 | | } |
| | 121 | |
|
| | 122 | | private void OnDestroy() |
| | 123 | | { |
| 1120 | 124 | | for (int i = 0; i < shortcutsButtons.Length; i++) |
| | 125 | | { |
| 504 | 126 | | int buttonIndex = i; |
| 504 | 127 | | shortcutsButtons[buttonIndex].onClick.RemoveAllListeners(); |
| | 128 | | } |
| | 129 | |
|
| 1120 | 130 | | for (int i = 0; i < shortcutsEventTriggers.Length; i++) |
| | 131 | | { |
| 504 | 132 | | int triggerIndex = i; |
| 504 | 133 | | BIWUtils.RemoveEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.Drop); |
| | 134 | | } |
| | 135 | |
|
| 56 | 136 | | quickBar1InputAction.OnTriggered -= OnQuickBar1InputTriggedered; |
| 56 | 137 | | quickBar2InputAction.OnTriggered -= OnQuickBar2InputTriggedered; |
| 56 | 138 | | quickBar3InputAction.OnTriggered -= OnQuickBar3InputTriggedered; |
| 56 | 139 | | quickBar4InputAction.OnTriggered -= OnQuickBar4InputTriggedered; |
| 56 | 140 | | quickBar5InputAction.OnTriggered -= OnQuickBar5InputTriggedered; |
| 56 | 141 | | quickBar6InputAction.OnTriggered -= OnQuickBar6InputTriggedered; |
| 56 | 142 | | quickBar7InputAction.OnTriggered -= OnQuickBar7InputTriggedered; |
| 56 | 143 | | quickBar8InputAction.OnTriggered -= OnQuickBar8InputTriggedered; |
| 56 | 144 | | quickBar9InputAction.OnTriggered -= OnQuickBar9InputTriggedered; |
| 56 | 145 | | } |
| | 146 | |
|
| 4 | 147 | | public void QuickBarObjectSelected(int index) { OnQuickBarObjectSelected?.Invoke(index); } |
| | 148 | |
|
| 3 | 149 | | public void SetIndexToBeginDrag(int index) { OnSetIndexToBeginDrag?.Invoke(index); } |
| | 150 | |
|
| 2 | 151 | | public void SetIndexToDrop(int index) { OnSetIndexToDrop?.Invoke(index); } |
| | 152 | |
|
| 2 | 153 | | public void SceneObjectDroppedFromQuickBar(int fromIndex, int toIndex, Texture texture) { OnSceneObjectDroppedFromQu |
| | 154 | |
|
| 2 | 155 | | public void SceneObjectDroppedFromCatalog(BaseEventData data) { OnSceneObjectDroppedFromCatalog?.Invoke(data); } |
| | 156 | |
|
| | 157 | | public void SetTextureToShortcut(int shortcutIndex, Texture texture) |
| | 158 | | { |
| 9 | 159 | | if (shortcutIndex >= shortcutsImgs.Length) |
| 0 | 160 | | return; |
| | 161 | |
|
| 9 | 162 | | if (shortcutsImgs[shortcutIndex] != null && texture != null) |
| 0 | 163 | | shortcutsImgs[shortcutIndex].SetTexture(texture); |
| 9 | 164 | | } |
| | 165 | |
|
| | 166 | | public void SetShortcutAsEmpty(int shortcutIndex) |
| | 167 | | { |
| 0 | 168 | | if (shortcutIndex >= shortcutsImgs.Length) |
| 0 | 169 | | return; |
| | 170 | |
|
| 0 | 171 | | shortcutsImgs[shortcutIndex].SetEmpty(); |
| 0 | 172 | | } |
| | 173 | |
|
| 0 | 174 | | private void OnQuickBar1InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(0); } |
| | 175 | |
|
| 0 | 176 | | private void OnQuickBar2InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(1); } |
| | 177 | |
|
| 0 | 178 | | private void OnQuickBar3InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(2); } |
| | 179 | |
|
| 0 | 180 | | private void OnQuickBar4InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(3); } |
| | 181 | |
|
| 0 | 182 | | private void OnQuickBar5InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(4); } |
| | 183 | |
|
| 0 | 184 | | private void OnQuickBar6InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(5); } |
| | 185 | |
|
| 0 | 186 | | private void OnQuickBar7InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(6); } |
| | 187 | |
|
| 0 | 188 | | private void OnQuickBar8InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(7); } |
| | 189 | |
|
| 0 | 190 | | private void OnQuickBar9InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(8); } |
| | 191 | |
|
| 2 | 192 | | public void OnQuickBarInputTriggedered(int index) { OnQuickBarInputTriggered?.Invoke(index); } |
| | 193 | |
|
| | 194 | | internal void CreateSlotToDrag() |
| | 195 | | { |
| 56 | 196 | | if (shortcutsImgs.Length == 0) |
| 0 | 197 | | return; |
| | 198 | |
|
| 56 | 199 | | draggedSlot = Instantiate(shortcutsImgs[0], generalCanvas != null ? generalCanvas.transform : null); |
| 56 | 200 | | draggedSlot.EnableDragMode(); |
| 56 | 201 | | draggedSlot.SetEmpty(); |
| 56 | 202 | | draggedSlot.SetActive(false); |
| 56 | 203 | | } |
| | 204 | |
|
| | 205 | | public void BeginDragSlot(int triggerIndex) |
| | 206 | | { |
| 1 | 207 | | if (draggedSlot == null || shortcutsImgs[triggerIndex].isEmpty) |
| 0 | 208 | | return; |
| | 209 | |
|
| 1 | 210 | | lastIndexToBeginDrag = triggerIndex; |
| 1 | 211 | | SetIndexToBeginDrag(triggerIndex); |
| 1 | 212 | | draggedSlot.SetActive(true); |
| 1 | 213 | | draggedSlot.SetTexture(shortcutsImgs[triggerIndex].image.texture); |
| 1 | 214 | | } |
| | 215 | |
|
| | 216 | | public void DragSlot(BaseEventData eventData, int triggerIndex) |
| | 217 | | { |
| 1 | 218 | | if (draggedSlot == null || shortcutsImgs[triggerIndex].isEmpty) |
| 0 | 219 | | return; |
| | 220 | |
|
| 1 | 221 | | draggedSlot.slotTransform.position = ((PointerEventData)eventData).position; |
| 1 | 222 | | } |
| | 223 | |
|
| | 224 | | public void EndDragSlot(int triggerIndex) |
| | 225 | | { |
| 1 | 226 | | if (draggedSlot == null || shortcutsImgs[triggerIndex].isEmpty) |
| 0 | 227 | | return; |
| | 228 | |
|
| 1 | 229 | | draggedSlot.SetEmpty(); |
| 1 | 230 | | draggedSlot.SetActive(false); |
| 1 | 231 | | QuickBarObjectSelected(triggerIndex); |
| 1 | 232 | | } |
| | 233 | |
|
| | 234 | | public void CancelCurrentDragging() |
| | 235 | | { |
| 1 | 236 | | lastIndexToBeginDrag = -1; |
| | 237 | |
|
| 1 | 238 | | if (draggedSlot != null) |
| | 239 | | { |
| 1 | 240 | | draggedSlot.SetEmpty(); |
| 1 | 241 | | draggedSlot.SetActive(false); |
| | 242 | | } |
| 1 | 243 | | } |
| | 244 | | } |