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