| | 1 | | using DCL; |
| | 2 | | using DCL.Interface; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.EventSystems; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | public enum RealmsSorting |
| | 11 | | { |
| | 12 | | BY_NAME, |
| | 13 | | BY_NUMBER_OF_PLAYERS |
| | 14 | | } |
| | 15 | |
|
| | 16 | | public enum RealmsSortingDirection |
| | 17 | | { |
| | 18 | | ASC, |
| | 19 | | DESC |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public interface IRealmSelectorComponentView |
| | 23 | | { |
| | 24 | | /// <summary> |
| | 25 | | /// Set the current realm label. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="realm">Current realm.</param> |
| | 28 | | void SetCurrentRealm(string realm); |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Set the available realms component with a list of realms. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="realms">List of realms (model) to be loaded.</param> |
| | 34 | | void SetAvailableRealms(List<RealmRowComponentModel> realms); |
| | 35 | | } |
| | 36 | |
|
| | 37 | | public class RealmSelectorComponentView : BaseComponentView, IRealmSelectorComponentView, IComponentModelConfig |
| | 38 | | { |
| | 39 | | internal const string REALMS_POOL_NAME = "RealmSelector_RealmRowsPool"; |
| | 40 | | internal const int REALMS_POOL_PREWARM = 20; |
| | 41 | |
|
| | 42 | | [Header("Assets References")] |
| | 43 | | [SerializeField] internal RealmRowComponentView realmRowPrefab; |
| | 44 | |
|
| | 45 | | [Header("Prefab References")] |
| | 46 | | [SerializeField] internal TMP_Text currentRealmText; |
| | 47 | | [SerializeField] internal ButtonComponentView sortByNameButton; |
| | 48 | | [SerializeField] internal Image sortByNameArrowUp; |
| | 49 | | [SerializeField] internal Image sortByNameArrowDown; |
| | 50 | | [SerializeField] internal ButtonComponentView sortByNumberOfPlayersButton; |
| | 51 | | [SerializeField] internal Image sortByNumberOfPlayersArrowUp; |
| | 52 | | [SerializeField] internal Image sortByNumberOfPlayersArrowDown; |
| | 53 | | [SerializeField] internal GridContainerComponentView availableRealms; |
| | 54 | | [SerializeField] internal Button modalBackgroundButton; |
| | 55 | | [SerializeField] internal ButtonComponentView closeCardButton; |
| | 56 | | [SerializeField] internal InputAction_Trigger closeAction; |
| | 57 | |
|
| | 58 | | [Header("Configuration")] |
| | 59 | | [SerializeField] internal RealmSelectorComponentModel model; |
| | 60 | | [SerializeField] internal Color colorForEvenRows; |
| | 61 | | [SerializeField] internal Color colorForOddRows; |
| | 62 | | [SerializeField] internal Color colorForFocusedRows; |
| | 63 | | [SerializeField] internal Color colorForActiveSortingArrow; |
| | 64 | | [SerializeField] internal Color colorForUnactiveSortingArrow; |
| | 65 | | [SerializeField] internal Color[] friendColors = null; |
| | 66 | |
|
| | 67 | | internal Pool realmsPool; |
| 14 | 68 | | internal RealmsSorting currentSorting = RealmsSorting.BY_NUMBER_OF_PLAYERS; |
| 14 | 69 | | internal RealmsSortingDirection currentSortingDirection = RealmsSortingDirection.DESC; |
| | 70 | | internal RealmTrackerController friendsTrackerController; |
| | 71 | |
|
| | 72 | | public override void Awake() |
| | 73 | | { |
| 13 | 74 | | base.Awake(); |
| | 75 | |
|
| 13 | 76 | | friendsTrackerController = new RealmTrackerController(FriendsController.i, friendColors); |
| | 77 | |
|
| 13 | 78 | | if (sortByNameButton != null) |
| 13 | 79 | | sortByNameButton.onClick.AddListener(() => |
| | 80 | | { |
| 0 | 81 | | ApplySorting( |
| | 82 | | RealmsSorting.BY_NAME, |
| | 83 | | currentSorting != RealmsSorting.BY_NAME || currentSortingDirection != RealmsSortingDirection.ASC ? R |
| | 84 | |
|
| 0 | 85 | | EventSystem.current?.SetSelectedGameObject(null); |
| 0 | 86 | | }); |
| | 87 | |
|
| 13 | 88 | | if (sortByNumberOfPlayersButton != null) |
| 13 | 89 | | sortByNumberOfPlayersButton.onClick.AddListener(() => |
| | 90 | | { |
| 0 | 91 | | ApplySorting( |
| | 92 | | RealmsSorting.BY_NUMBER_OF_PLAYERS, |
| | 93 | | currentSorting != RealmsSorting.BY_NUMBER_OF_PLAYERS || currentSortingDirection != RealmsSortingDire |
| | 94 | |
|
| 0 | 95 | | EventSystem.current?.SetSelectedGameObject(null); |
| 0 | 96 | | }); |
| | 97 | |
|
| 13 | 98 | | if (closeCardButton != null) |
| 13 | 99 | | closeCardButton.onClick.AddListener(CloseModal); |
| | 100 | |
|
| 13 | 101 | | if (closeAction != null) |
| 13 | 102 | | closeAction.OnTriggered += OnCloseActionTriggered; |
| | 103 | |
|
| 13 | 104 | | if (modalBackgroundButton != null) |
| 13 | 105 | | modalBackgroundButton.onClick.AddListener(CloseModal); |
| | 106 | |
|
| 13 | 107 | | ConfigureRealmsPool(); |
| 13 | 108 | | RefreshSortingArrows(); |
| 13 | 109 | | } |
| | 110 | |
|
| | 111 | | public void Configure(BaseComponentModel newModel) |
| | 112 | | { |
| 1 | 113 | | model = (RealmSelectorComponentModel)newModel; |
| 1 | 114 | | RefreshControl(); |
| 1 | 115 | | } |
| | 116 | |
|
| | 117 | | public override void RefreshControl() |
| | 118 | | { |
| 1 | 119 | | if (model == null) |
| 0 | 120 | | return; |
| | 121 | |
|
| 1 | 122 | | SetCurrentRealm(model.currentRealmName); |
| 1 | 123 | | availableRealms.RefreshControl(); |
| 1 | 124 | | } |
| | 125 | |
|
| | 126 | | public override void Show(bool instant = false) |
| | 127 | | { |
| 2 | 128 | | base.Show(instant); |
| | 129 | |
|
| 2 | 130 | | DataStore.i.exploreV2.isSomeModalOpen.Set(true); |
| 2 | 131 | | } |
| | 132 | |
|
| | 133 | | public override void Hide(bool instant = false) |
| | 134 | | { |
| 43 | 135 | | base.Hide(instant); |
| | 136 | |
|
| 43 | 137 | | DataStore.i.exploreV2.isSomeModalOpen.Set(false); |
| 43 | 138 | | } |
| | 139 | |
|
| | 140 | | public override void Dispose() |
| | 141 | | { |
| 66 | 142 | | base.Dispose(); |
| | 143 | |
|
| 66 | 144 | | if (sortByNameButton != null) |
| 66 | 145 | | sortByNameButton.onClick.RemoveAllListeners(); |
| | 146 | |
|
| 66 | 147 | | if (sortByNameButton != null) |
| 66 | 148 | | sortByNumberOfPlayersButton.onClick.RemoveAllListeners(); |
| | 149 | |
|
| 66 | 150 | | if (closeCardButton != null) |
| 66 | 151 | | closeCardButton.onClick.RemoveAllListeners(); |
| | 152 | |
|
| 66 | 153 | | if (closeAction != null) |
| 66 | 154 | | closeAction.OnTriggered -= OnCloseActionTriggered; |
| | 155 | |
|
| 66 | 156 | | if (modalBackgroundButton != null) |
| 66 | 157 | | modalBackgroundButton.onClick.RemoveAllListeners(); |
| 66 | 158 | | } |
| | 159 | |
|
| | 160 | | public void SetCurrentRealm(string realm) |
| | 161 | | { |
| 2 | 162 | | model.currentRealmName = realm; |
| | 163 | |
|
| | 164 | | // Set the current realm in the modal header |
| 2 | 165 | | if (currentRealmText != null) |
| 2 | 166 | | currentRealmText.text = $"You are in <b>{realm.ToUpper()}</b>"; |
| | 167 | |
|
| | 168 | | // Search the current realm in the available ones and set it as connected |
| 2 | 169 | | var instantiatedRealms = availableRealms.GetItems(); |
| 4 | 170 | | foreach (RealmRowComponentView realmRow in instantiatedRealms) |
| | 171 | | { |
| 0 | 172 | | realmRow.SetAsConnected(realmRow.model.name == realm); |
| | 173 | | } |
| 2 | 174 | | } |
| | 175 | |
|
| | 176 | | public void SetAvailableRealms(List<RealmRowComponentModel> realms) |
| | 177 | | { |
| 1 | 178 | | friendsTrackerController?.RemoveAllHandlers(); |
| | 179 | |
|
| 1 | 180 | | availableRealms.ExtractItems(); |
| 1 | 181 | | realmsPool.ReleaseAll(); |
| | 182 | |
|
| 1 | 183 | | List<BaseComponentView> realmsToAdd = new List<BaseComponentView>(); |
| 6 | 184 | | foreach (RealmRowComponentModel realm in realms) |
| | 185 | | { |
| 2 | 186 | | RealmRowComponentView newRealmRow = realmsPool.Get().gameObject.GetComponent<RealmRowComponentView>(); |
| 2 | 187 | | newRealmRow.Configure(realm); |
| 2 | 188 | | newRealmRow.SetOnHoverColor(colorForFocusedRows); |
| 2 | 189 | | newRealmRow.onWarpInClick.RemoveAllListeners(); |
| 2 | 190 | | newRealmRow.onWarpInClick.AddListener(() => |
| | 191 | | { |
| 0 | 192 | | WebInterface.SendChatMessage(new ChatMessage |
| | 193 | | { |
| | 194 | | messageType = ChatMessage.Type.NONE, |
| | 195 | | recipient = string.Empty, |
| | 196 | | body = $"/changerealm {realm.name}" |
| | 197 | | }); |
| 0 | 198 | | }); |
| 2 | 199 | | realmsToAdd.Add(newRealmRow); |
| | 200 | |
|
| 2 | 201 | | friendsTrackerController?.AddHandler(newRealmRow.friendsHandler); |
| | 202 | | } |
| | 203 | |
|
| 1 | 204 | | availableRealms.SetItems(realmsToAdd); |
| 1 | 205 | | RefreshRowColours(); |
| | 206 | |
|
| 1 | 207 | | ApplySorting(currentSorting, currentSortingDirection); |
| 1 | 208 | | } |
| | 209 | |
|
| | 210 | | internal void ApplySorting(RealmsSorting sortBy, RealmsSortingDirection sortingDirection) |
| | 211 | | { |
| 1 | 212 | | List<BaseComponentView> realmsToSort = availableRealms.ExtractItems(); |
| | 213 | |
|
| | 214 | | switch (sortBy) |
| | 215 | | { |
| | 216 | | case RealmsSorting.BY_NAME: |
| 0 | 217 | | if (sortingDirection == RealmsSortingDirection.ASC) |
| 0 | 218 | | realmsToSort = realmsToSort |
| 0 | 219 | | .OrderBy(d => ((RealmRowComponentView)d).model.name) |
| | 220 | | .ToList(); |
| | 221 | | else |
| 0 | 222 | | realmsToSort = realmsToSort |
| 0 | 223 | | .OrderByDescending(d => ((RealmRowComponentView)d).model.name) |
| | 224 | | .ToList(); |
| 0 | 225 | | break; |
| | 226 | | case RealmsSorting.BY_NUMBER_OF_PLAYERS: |
| 1 | 227 | | if (sortingDirection == RealmsSortingDirection.ASC) |
| 0 | 228 | | realmsToSort = realmsToSort |
| 0 | 229 | | .OrderBy(d => ((RealmRowComponentView)d).model.players) |
| | 230 | | .ToList(); |
| | 231 | | else |
| 1 | 232 | | realmsToSort = realmsToSort |
| 2 | 233 | | .OrderByDescending(d => ((RealmRowComponentView)d).model.players) |
| | 234 | | .ToList(); |
| | 235 | | break; |
| | 236 | | } |
| | 237 | |
|
| 1 | 238 | | availableRealms.SetItems(realmsToSort); |
| | 239 | |
|
| 1 | 240 | | currentSorting = sortBy; |
| 1 | 241 | | currentSortingDirection = sortingDirection; |
| | 242 | |
|
| 1 | 243 | | RefreshSortingArrows(); |
| 1 | 244 | | RefreshRowColours(); |
| 1 | 245 | | } |
| | 246 | |
|
| | 247 | | internal void RefreshSortingArrows() |
| | 248 | | { |
| 14 | 249 | | sortByNameArrowUp.color = currentSorting == RealmsSorting.BY_NAME && currentSortingDirection == RealmsSortingDir |
| 14 | 250 | | sortByNameArrowDown.color = currentSorting == RealmsSorting.BY_NAME && currentSortingDirection == RealmsSortingD |
| 14 | 251 | | sortByNumberOfPlayersArrowUp.color = currentSorting == RealmsSorting.BY_NUMBER_OF_PLAYERS && currentSortingDirec |
| 14 | 252 | | sortByNumberOfPlayersArrowDown.color = currentSorting == RealmsSorting.BY_NUMBER_OF_PLAYERS && currentSortingDir |
| 14 | 253 | | } |
| | 254 | |
|
| | 255 | | internal void RefreshRowColours() |
| | 256 | | { |
| 2 | 257 | | var instantiatedRealms = availableRealms.GetItems(); |
| 2 | 258 | | bool isAnOddRow = true; |
| 12 | 259 | | foreach (RealmRowComponentView realmRow in instantiatedRealms) |
| | 260 | | { |
| 4 | 261 | | realmRow.SetRowColor(isAnOddRow ? colorForOddRows : colorForEvenRows); |
| 4 | 262 | | isAnOddRow = !isAnOddRow; |
| | 263 | | } |
| 2 | 264 | | } |
| | 265 | |
|
| 4 | 266 | | internal void CloseModal() { Hide(); } |
| | 267 | |
|
| 2 | 268 | | internal void OnCloseActionTriggered(DCLAction_Trigger action) { CloseModal(); } |
| | 269 | |
|
| | 270 | | internal void ConfigureRealmsPool() |
| | 271 | | { |
| 14 | 272 | | realmsPool = PoolManager.i.GetPool(REALMS_POOL_NAME); |
| 14 | 273 | | if (realmsPool == null) |
| | 274 | | { |
| 1 | 275 | | realmsPool = PoolManager.i.AddPool( |
| | 276 | | REALMS_POOL_NAME, |
| | 277 | | GameObject.Instantiate(realmRowPrefab).gameObject, |
| | 278 | | maxPrewarmCount: REALMS_POOL_PREWARM, |
| | 279 | | isPersistent: true); |
| | 280 | |
|
| 1 | 281 | | realmsPool.ForcePrewarm(); |
| | 282 | | } |
| 14 | 283 | | } |
| | 284 | | } |