| | 1 | | using DCL; |
| | 2 | | using System; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.EventSystems; |
| | 5 | |
|
| | 6 | | public interface IQuickBarController |
| | 7 | | { |
| | 8 | | event Action<int> OnQuickBarShortcutSelected; |
| | 9 | | event Action<CatalogItem> OnCatalogItemSelected; |
| | 10 | | event Action<CatalogItem> OnCatalogItemAssigned; |
| | 11 | |
|
| | 12 | | void Initialize(IQuickBarView view, IDragAndDropSceneObjectController dragAndDropSceneObjectController); |
| | 13 | | void Dispose(); |
| | 14 | | int GetSlotsCount(); |
| | 15 | | CatalogItem QuickBarObjectSelected(int index); |
| | 16 | | void SetIndexToDrop(int index); |
| | 17 | | void SceneObjectDroppedFromQuickBar(int fromQuickBarIndex, int toQuickBarIndex, Texture texture); |
| | 18 | | void SceneObjectDroppedFromCatalog(BaseEventData data); |
| | 19 | | void SetQuickBarShortcut(CatalogItem catalogItem, int index, Texture texture); |
| | 20 | | void QuickBarInput(int quickBarSlot); |
| | 21 | | void CancelDragging(); |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public class QuickBarController : IQuickBarController |
| | 25 | | { |
| | 26 | | const int AMOUNT_OF_QUICK_SLOTS = 9; |
| | 27 | |
|
| | 28 | | public event Action<int> OnQuickBarShortcutSelected; |
| | 29 | | public event Action<CatalogItem> OnCatalogItemSelected; |
| | 30 | | public event Action<CatalogItem> OnCatalogItemAssigned; |
| | 31 | |
|
| | 32 | | internal IQuickBarView quickBarView; |
| | 33 | | internal IDragAndDropSceneObjectController dragAndDropController; |
| | 34 | |
|
| 15 | 35 | | internal CatalogItem[] quickBarShortcutsCatalogItems = new CatalogItem[AMOUNT_OF_QUICK_SLOTS]; |
| 15 | 36 | | internal AssetPromise_Texture[] quickBarShortcutsThumbnailPromises = new AssetPromise_Texture[AMOUNT_OF_QUICK_SLOTS] |
| 15 | 37 | | internal int lastIndexDroped = -1; |
| | 38 | |
|
| | 39 | | public void Initialize(IQuickBarView quickBarView, IDragAndDropSceneObjectController dragAndDropController) |
| | 40 | | { |
| 14 | 41 | | this.quickBarView = quickBarView; |
| 14 | 42 | | this.dragAndDropController = dragAndDropController; |
| | 43 | |
|
| 14 | 44 | | quickBarView.OnQuickBarObjectSelected += OnQuickBarObjectSelected; |
| 14 | 45 | | quickBarView.OnSetIndexToDrop += SetIndexToDrop; |
| 14 | 46 | | quickBarView.OnSceneObjectDroppedFromQuickBar += SceneObjectDroppedFromQuickBar; |
| 14 | 47 | | quickBarView.OnSceneObjectDroppedFromCatalog += SceneObjectDroppedFromCatalog; |
| 14 | 48 | | quickBarView.OnQuickBarInputTriggered += QuickBarInput; |
| 14 | 49 | | dragAndDropController.OnStopInput += CancelDragging; |
| 14 | 50 | | } |
| | 51 | |
|
| | 52 | | public void Dispose() |
| | 53 | | { |
| 13 | 54 | | quickBarView.OnQuickBarObjectSelected -= OnQuickBarObjectSelected; |
| 13 | 55 | | quickBarView.OnSetIndexToDrop -= SetIndexToDrop; |
| 13 | 56 | | quickBarView.OnSceneObjectDroppedFromQuickBar -= SceneObjectDroppedFromQuickBar; |
| 13 | 57 | | quickBarView.OnSceneObjectDroppedFromCatalog -= SceneObjectDroppedFromCatalog; |
| 13 | 58 | | quickBarView.OnQuickBarInputTriggered -= QuickBarInput; |
| 13 | 59 | | dragAndDropController.OnStopInput -= CancelDragging; |
| | 60 | |
|
| 260 | 61 | | foreach (AssetPromise_Texture loadedThumbnailPromise in quickBarShortcutsThumbnailPromises) |
| | 62 | | { |
| 117 | 63 | | if (loadedThumbnailPromise == null) |
| | 64 | | continue; |
| | 65 | |
|
| 0 | 66 | | ClearThumbnailPromise(loadedThumbnailPromise); |
| | 67 | | } |
| 13 | 68 | | } |
| | 69 | |
|
| 0 | 70 | | public int GetSlotsCount() { return AMOUNT_OF_QUICK_SLOTS; } |
| | 71 | |
|
| | 72 | | public CatalogItem QuickBarObjectSelected(int index) |
| | 73 | | { |
| 12 | 74 | | if (quickBarShortcutsCatalogItems.Length > index && quickBarShortcutsCatalogItems[index] != null) |
| | 75 | | { |
| 12 | 76 | | OnCatalogItemSelected?.Invoke(quickBarShortcutsCatalogItems[index]); |
| 12 | 77 | | return quickBarShortcutsCatalogItems[index]; |
| | 78 | | } |
| | 79 | |
|
| 0 | 80 | | return null; |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | private void OnQuickBarObjectSelected(int obj) { QuickBarObjectSelected(obj); } |
| | 84 | |
|
| 0 | 85 | | public void SetIndexToDrop(int index) { lastIndexDroped = index; } |
| | 86 | |
|
| | 87 | | public void SceneObjectDroppedFromQuickBar(int fromQuickBarIndex, int toQuickBarIndex, Texture texture) |
| | 88 | | { |
| 1 | 89 | | SetQuickBarShortcut(quickBarShortcutsCatalogItems[fromQuickBarIndex], toQuickBarIndex, texture); |
| 1 | 90 | | MoveQuickbarThumbnailPromise(fromQuickBarIndex, toQuickBarIndex); |
| 1 | 91 | | RemoveQuickBarShortcut(fromQuickBarIndex); |
| 1 | 92 | | } |
| | 93 | |
|
| | 94 | | private void MoveQuickbarThumbnailPromise(int fromQuickBarIndex, int toQuickBarIndex) |
| | 95 | | { |
| 1 | 96 | | quickBarShortcutsThumbnailPromises[toQuickBarIndex] = quickBarShortcutsThumbnailPromises[fromQuickBarIndex]; |
| 1 | 97 | | quickBarShortcutsThumbnailPromises[fromQuickBarIndex] = null; |
| 1 | 98 | | } |
| | 99 | |
|
| | 100 | | public void SceneObjectDroppedFromCatalog(BaseEventData data) |
| | 101 | | { |
| 1 | 102 | | CatalogItemAdapter adapter = dragAndDropController.GetLastAdapterDragged(); |
| | 103 | |
|
| 1 | 104 | | if (adapter != null) |
| 0 | 105 | | SetCatalogItemToShortcut(adapter.GetContent()); |
| 1 | 106 | | } |
| | 107 | |
|
| | 108 | | private void SetCatalogItemToShortcut(CatalogItem catalogItem) |
| | 109 | | { |
| 0 | 110 | | if (catalogItem == null) |
| 0 | 111 | | return; |
| | 112 | |
|
| 0 | 113 | | var url = catalogItem.GetThumbnailUrl(); |
| | 114 | |
|
| 0 | 115 | | if (string.IsNullOrEmpty(url)) |
| 0 | 116 | | return; |
| | 117 | |
|
| 0 | 118 | | ClearThumbnailPromise(quickBarShortcutsThumbnailPromises[lastIndexDroped]); |
| 0 | 119 | | quickBarShortcutsThumbnailPromises[lastIndexDroped] = new AssetPromise_Texture(url); |
| | 120 | |
|
| 0 | 121 | | quickBarShortcutsThumbnailPromises[lastIndexDroped].OnSuccessEvent += x => |
| | 122 | | { |
| 0 | 123 | | SetQuickBarShortcut(catalogItem, lastIndexDroped, x.texture); |
| 0 | 124 | | }; |
| | 125 | |
|
| 0 | 126 | | quickBarShortcutsThumbnailPromises[lastIndexDroped].OnFailEvent += (x, error) => |
| | 127 | | { |
| 0 | 128 | | Debug.Log($"Error downloading: {url}, Exception: {error}"); |
| 0 | 129 | | }; |
| | 130 | |
|
| 0 | 131 | | AssetPromiseKeeper_Texture.i.Keep(quickBarShortcutsThumbnailPromises[lastIndexDroped]); |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | private void ClearThumbnailPromise(AssetPromise_Texture thumbnailToClear) |
| | 135 | | { |
| 1 | 136 | | if (thumbnailToClear != null) |
| | 137 | | { |
| 0 | 138 | | thumbnailToClear.ClearEvents(); |
| 0 | 139 | | AssetPromiseKeeper_Texture.i.Forget(thumbnailToClear); |
| 0 | 140 | | thumbnailToClear = null; |
| | 141 | | } |
| 1 | 142 | | } |
| | 143 | |
|
| | 144 | | private void RemoveQuickBarShortcut(int index) |
| | 145 | | { |
| 1 | 146 | | quickBarShortcutsCatalogItems[index] = null; |
| 1 | 147 | | quickBarView.SetShortcutAsEmpty(index); |
| 1 | 148 | | ClearThumbnailPromise(quickBarShortcutsThumbnailPromises[index]); |
| 1 | 149 | | } |
| | 150 | |
|
| | 151 | | public void SetQuickBarShortcut(CatalogItem catalogItem, int index, Texture texture) |
| | 152 | | { |
| 13 | 153 | | quickBarShortcutsCatalogItems[index] = catalogItem; |
| 13 | 154 | | quickBarView.SetTextureToShortcut(index, texture); |
| 13 | 155 | | OnCatalogItemAssigned?.Invoke(catalogItem); |
| 0 | 156 | | } |
| | 157 | |
|
| 6 | 158 | | public void QuickBarInput(int quickBarSlot) { OnQuickBarShortcutSelected?.Invoke(quickBarSlot); } |
| | 159 | |
|
| 2 | 160 | | public void CancelDragging() { quickBarView.CancelCurrentDragging(); } |
| | 161 | | } |