| | 1 | | using DCL.Interface; |
| | 2 | | using System; |
| | 3 | |
|
| | 4 | | public class AirdroppingHUDController : IHUD |
| | 5 | | { |
| | 6 | |
|
| | 7 | | [Serializable] |
| | 8 | | public class Model |
| | 9 | | { |
| | 10 | | public string id; |
| | 11 | | public string title; |
| | 12 | | public string subtitle; |
| | 13 | | public ItemModel[] items; |
| | 14 | | } |
| | 15 | |
|
| | 16 | | [Serializable] |
| | 17 | | public class ItemModel |
| | 18 | | { |
| | 19 | | public string name; |
| | 20 | | public string thumbnailURL; |
| | 21 | | public string type; |
| | 22 | | public string rarity; |
| | 23 | | public string subtitle; |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public enum State |
| | 27 | | { |
| | 28 | | Hidden, |
| | 29 | | Initial, |
| | 30 | | SingleItem, |
| | 31 | | Summary, |
| | 32 | | Summary_NoItems, |
| | 33 | | Finish |
| | 34 | | } |
| | 35 | |
|
| | 36 | | internal AirdroppingHUDView view; |
| | 37 | | internal State currentState; |
| | 38 | | internal Model model; |
| 8 | 39 | | internal int currentItemShown = -1; |
| 7 | 40 | | internal int totalItems => model?.items?.Length ?? 0; |
| | 41 | |
|
| | 42 | | public static event System.Action OnAirdropFinished = null; |
| | 43 | |
|
| 8 | 44 | | public AirdroppingHUDController() |
| | 45 | | { |
| 8 | 46 | | view = AirdroppingHUDView.Create(); |
| 8 | 47 | | view.Initialize(MoveToNextState); |
| 8 | 48 | | currentState = State.Hidden; |
| 8 | 49 | | ApplyState(); |
| 8 | 50 | | } |
| | 51 | |
|
| | 52 | | public void AirdroppingRequested(Model model) |
| | 53 | | { |
| 3 | 54 | | if (model == null) |
| 0 | 55 | | return; |
| | 56 | |
|
| 3 | 57 | | this.model = model; |
| | 58 | |
|
| 14 | 59 | | for (var i = 0; i < this.model.items.Length; i++) |
| | 60 | | { |
| 4 | 61 | | ThumbnailsManager.PreloadThumbnail(this.model.items[i].thumbnailURL); |
| | 62 | | } |
| | 63 | |
|
| 3 | 64 | | currentState = State.Initial; |
| 3 | 65 | | ApplyState(); |
| 3 | 66 | | } |
| | 67 | |
|
| | 68 | | public void MoveToNextState() |
| | 69 | | { |
| 7 | 70 | | SetNextState(); |
| 7 | 71 | | ApplyState(); |
| 7 | 72 | | } |
| | 73 | |
|
| | 74 | | public void SetNextState() |
| | 75 | | { |
| 7 | 76 | | switch (currentState) |
| | 77 | | { |
| | 78 | | case State.Initial: |
| 2 | 79 | | if (currentItemShown > totalItems - 1) |
| 1 | 80 | | currentState = totalItems != 0 ? State.Summary : State.Summary_NoItems; |
| | 81 | | else |
| 1 | 82 | | currentState = State.SingleItem; |
| 1 | 83 | | break; |
| | 84 | | case State.SingleItem: |
| 2 | 85 | | currentItemShown++; |
| 2 | 86 | | if (currentItemShown > totalItems - 1) |
| 1 | 87 | | currentState = totalItems != 0 ? State.Summary : State.Summary_NoItems; |
| 1 | 88 | | break; |
| | 89 | | case State.Summary: |
| 1 | 90 | | currentState = State.Finish; |
| 1 | 91 | | break; |
| | 92 | | case State.Summary_NoItems: |
| 1 | 93 | | currentState = State.Hidden; |
| 1 | 94 | | break; |
| | 95 | | case State.Finish: |
| | 96 | | default: |
| 1 | 97 | | currentState = State.Hidden; |
| | 98 | | break; |
| | 99 | | } |
| 2 | 100 | | } |
| | 101 | |
|
| | 102 | | public void ApplyState() |
| | 103 | | { |
| 18 | 104 | | switch (currentState) |
| | 105 | | { |
| | 106 | | case State.Initial: |
| 3 | 107 | | currentItemShown = 0; |
| 3 | 108 | | view.SetContentActive(true); |
| 3 | 109 | | view.ShowInitialScreen(model.title, model.subtitle); |
| 3 | 110 | | break; |
| | 111 | | case State.SingleItem: |
| 2 | 112 | | view.ShowItemScreen(model.items[currentItemShown], model.items.Length - (currentItemShown + 1)); |
| 2 | 113 | | break; |
| | 114 | | case State.Summary: |
| 1 | 115 | | view.ShowSummaryScreen(model.items); |
| 1 | 116 | | break; |
| | 117 | | case State.Summary_NoItems: |
| 1 | 118 | | view.ShowSummaryNoItemsScreen(); |
| 1 | 119 | | break; |
| | 120 | | case State.Finish: |
| 1 | 121 | | WebInterface.SendUserAcceptedCollectibles(model.id); |
| | 122 | |
|
| 1 | 123 | | OnAirdropFinished?.Invoke(); |
| | 124 | |
|
| 1 | 125 | | MoveToNextState(); |
| 1 | 126 | | break; |
| | 127 | | case State.Hidden: |
| | 128 | | default: |
| 10 | 129 | | model = null; |
| 10 | 130 | | view.SetContentActive(false); |
| | 131 | | break; |
| | 132 | | } |
| 10 | 133 | | } |
| | 134 | |
|
| 2 | 135 | | public void SetVisibility(bool visible) { view.SetVisibility(visible); } |
| | 136 | |
|
| | 137 | | public void Dispose() |
| | 138 | | { |
| 8 | 139 | | if (view != null) |
| 8 | 140 | | UnityEngine.Object.Destroy(view.gameObject); |
| 8 | 141 | | } |
| | 142 | | } |