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