| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine; |
| | 5 | | using TMPro; |
| | 6 | | using System.Linq; |
| | 7 | | using DCL; |
| | 8 | | using static DCL.Interface.WebInterface; |
| | 9 | |
|
| | 10 | | internal class HotSceneCellView : MonoBehaviour |
| | 11 | | { |
| | 12 | | const int THMBL_MARKETPLACE_WIDTH = 196; |
| | 13 | | const int THMBL_MARKETPLACE_HEIGHT = 143; |
| | 14 | | const int THMBL_MARKETPLACE_SIZEFACTOR = 50; |
| | 15 | |
|
| | 16 | | [Header("Animators")] |
| | 17 | | [SerializeField] Animator viewAnimator; |
| | 18 | | [SerializeField] ShowHideAnimator jumpInButtonAnimator; |
| | 19 | |
|
| | 20 | | [Header("Crowd")] |
| | 21 | | [SerializeField] GameObject crowdCountContainer; |
| | 22 | | [SerializeField] TextMeshProUGUI crowdCount; |
| | 23 | |
|
| | 24 | | [Header("Events")] |
| | 25 | | [SerializeField] GameObject eventsContainer; |
| | 26 | |
|
| | 27 | | [Header("Friends")] |
| | 28 | | [SerializeField] ExploreFriendsView friendsView; |
| | 29 | | [SerializeField] GameObject friendsContainer; |
| | 30 | |
|
| | 31 | | [Header("Scene")] |
| | 32 | | [SerializeField] TextMeshProUGUI sceneName; |
| | 33 | | [SerializeField] internal RawImageFillParent thumbnailImage; |
| | 34 | | [SerializeField] UIHoverCallback sceneInfoButton; |
| | 35 | |
|
| | 36 | | [Header("UI")] |
| | 37 | | [SerializeField] UIHoverCallback jumpInHoverArea; |
| | 38 | | [SerializeField] Button_OnPointerDown jumpIn; |
| | 39 | | [SerializeField] Sprite errorThumbnail; |
| | 40 | |
|
| | 41 | | public delegate void JumpInDelegate(Vector2Int coords, string serverName, string layerName); |
| | 42 | | public static event JumpInDelegate OnJumpIn; |
| | 43 | |
|
| | 44 | | public static event Action<HotSceneCellView> OnInfoButtonPointerDown; |
| | 45 | | public static event Action OnInfoButtonPointerExit; |
| | 46 | |
|
| | 47 | | public event Action<Texture2D> OnThumbnailSet; |
| | 48 | |
|
| 0 | 49 | | public MapInfoHandler mapInfoHandler { private set; get; } |
| 0 | 50 | | public FriendsHandler friendsHandler { private set; get; } |
| 0 | 51 | | public ThumbnailHandler thumbnailHandler { private set; get; } |
| 0 | 52 | | public AnimationHandler animationHandler { private set; get; } |
| | 53 | |
|
| | 54 | | private ViewPool<ExploreFriendsView> friendPool; |
| | 55 | | private Dictionary<string, ExploreFriendsView> friendViewById; |
| | 56 | | private bool isLoaded = false; |
| | 57 | | private bool isInitialized = false; |
| 44 | 58 | | private Queue<HotScenesController.HotSceneInfo.Realm> nextMostPopulatedRealms = new Queue<HotScenesController.HotSce |
| 44 | 59 | | private JumpInPayload lastJumpInTried = new JumpInPayload(); |
| | 60 | |
|
| | 61 | | HotScenesController.HotSceneInfo hotSceneInfo; |
| | 62 | |
|
| | 63 | | protected void Awake() |
| | 64 | | { |
| 8 | 65 | | crowdCountContainer.SetActive(false); |
| 8 | 66 | | eventsContainer.SetActive(false); |
| | 67 | |
|
| 8 | 68 | | jumpInHoverArea.OnPointerEnter += () => |
| | 69 | | { |
| 0 | 70 | | jumpInButtonAnimator.gameObject.SetActive(true); |
| 0 | 71 | | jumpInButtonAnimator.Show(); |
| 0 | 72 | | }; |
| 8 | 73 | | jumpInHoverArea.OnPointerExit += () => jumpInButtonAnimator.Hide(); |
| 8 | 74 | | sceneInfoButton.OnPointerDown += () => jumpInButtonAnimator.Hide(true); |
| | 75 | |
|
| | 76 | | // NOTE: we don't use the pointer down callback to avoid being mistakenly pressed while dragging |
| 8 | 77 | | jumpIn.onClick.AddListener(JumpInPressed); |
| | 78 | |
|
| 8 | 79 | | sceneInfoButton.OnPointerDown += () => OnInfoButtonPointerDown?.Invoke(this); |
| 8 | 80 | | sceneInfoButton.OnPointerExit += () => OnInfoButtonPointerExit?.Invoke(); |
| | 81 | |
|
| 8 | 82 | | Initialize(); |
| 8 | 83 | | } |
| | 84 | |
|
| | 85 | | public void Initialize() |
| | 86 | | { |
| 14 | 87 | | if (isInitialized) |
| 6 | 88 | | return; |
| | 89 | |
|
| 8 | 90 | | isInitialized = true; |
| | 91 | |
|
| 8 | 92 | | friendPool = new ViewPool<ExploreFriendsView>(friendsView, 0); |
| 8 | 93 | | friendViewById = new Dictionary<string, ExploreFriendsView>(); |
| | 94 | |
|
| 8 | 95 | | mapInfoHandler = new MapInfoHandler(); |
| | 96 | |
|
| 8 | 97 | | friendsHandler = new FriendsHandler(mapInfoHandler); |
| 8 | 98 | | friendsHandler.onFriendAdded += OnFriendAdded; |
| 8 | 99 | | friendsHandler.onFriendRemoved += OnFriendRemoved; |
| | 100 | |
|
| 8 | 101 | | thumbnailHandler = new ThumbnailHandler(); |
| 8 | 102 | | animationHandler = new AnimationHandler(viewAnimator); |
| 8 | 103 | | } |
| | 104 | |
|
| | 105 | | public void Setup(HotScenesController.HotSceneInfo sceneInfo) |
| | 106 | | { |
| 6 | 107 | | hotSceneInfo = sceneInfo; |
| 6 | 108 | | mapInfoHandler.SetMinimapSceneInfo(sceneInfo); |
| | 109 | |
|
| 6 | 110 | | OnCrowdInfoUpdated(sceneInfo); |
| 6 | 111 | | OnMapInfoUpdated(sceneInfo); |
| 6 | 112 | | } |
| | 113 | |
|
| | 114 | | public void Clear() |
| | 115 | | { |
| 0 | 116 | | thumbnailImage.texture = null; |
| 0 | 117 | | thumbnailHandler.Dispose(); |
| 0 | 118 | | thumbnailImage.gameObject.SetActive(false); |
| 0 | 119 | | isLoaded = false; |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | public void JumpInPressed() |
| | 123 | | { |
| 0 | 124 | | HotScenesController.HotSceneInfo.Realm realm = new HotScenesController.HotSceneInfo.Realm() { layer = null, serv |
| 0 | 125 | | hotSceneInfo.realms = hotSceneInfo.realms.OrderByDescending(x => x.usersCount).ToArray(); |
| 0 | 126 | | nextMostPopulatedRealms.Clear(); |
| 0 | 127 | | for (int i = 0; i < hotSceneInfo.realms.Length; i++) |
| | 128 | | { |
| 0 | 129 | | if (hotSceneInfo.realms[i].usersCount < hotSceneInfo.realms[i].usersMax) |
| | 130 | | { |
| 0 | 131 | | realm = hotSceneInfo.realms[i]; |
| 0 | 132 | | if (i < hotSceneInfo.realms.Length - 1) |
| | 133 | | { |
| 0 | 134 | | nextMostPopulatedRealms = new Queue<HotScenesController.HotSceneInfo.Realm>(hotSceneInfo.realms.ToLi |
| | 135 | | } |
| 0 | 136 | | break; |
| | 137 | | } |
| | 138 | | } |
| | 139 | |
|
| 0 | 140 | | RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess; |
| 0 | 141 | | RealmsInfoBridge.OnRealmConnectionSuccess += OnRealmConnectionSuccess; |
| 0 | 142 | | RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed; |
| 0 | 143 | | RealmsInfoBridge.OnRealmConnectionFailed += OnRealmConnectionFailed; |
| | 144 | |
|
| 0 | 145 | | lastJumpInTried.gridPosition = hotSceneInfo.baseCoords; |
| 0 | 146 | | lastJumpInTried.realm.serverName = realm.serverName; |
| 0 | 147 | | lastJumpInTried.realm.layer = realm.layer; |
| | 148 | |
|
| 0 | 149 | | OnJumpIn?.Invoke(hotSceneInfo.baseCoords, realm.serverName, realm.layer); |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | private void OnRealmConnectionSuccess(JumpInPayload successRealm) |
| | 153 | | { |
| 0 | 154 | | if (successRealm.gridPosition != lastJumpInTried.gridPosition || |
| | 155 | | successRealm.realm.serverName != lastJumpInTried.realm.serverName || |
| | 156 | | successRealm.realm.layer != lastJumpInTried.realm.layer) |
| 0 | 157 | | return; |
| | 158 | |
|
| 0 | 159 | | RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess; |
| 0 | 160 | | RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed; |
| 0 | 161 | | } |
| | 162 | |
|
| | 163 | | private void OnRealmConnectionFailed(JumpInPayload failedRealm) |
| | 164 | | { |
| 0 | 165 | | if (failedRealm.gridPosition != lastJumpInTried.gridPosition || |
| | 166 | | failedRealm.realm.serverName != lastJumpInTried.realm.serverName || |
| | 167 | | failedRealm.realm.layer != lastJumpInTried.realm.layer) |
| 0 | 168 | | return; |
| | 169 | |
|
| 0 | 170 | | if (nextMostPopulatedRealms.Count > 0) |
| | 171 | | { |
| 0 | 172 | | HotScenesController.HotSceneInfo.Realm nextRealmToTry = nextMostPopulatedRealms.Dequeue(); |
| 0 | 173 | | OnJumpIn?.Invoke(hotSceneInfo.baseCoords, nextRealmToTry.serverName, nextRealmToTry.layer); |
| 0 | 174 | | } |
| | 175 | | else |
| | 176 | | { |
| 0 | 177 | | RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess; |
| 0 | 178 | | RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed; |
| 0 | 179 | | OnJumpIn?.Invoke(hotSceneInfo.baseCoords, null, null); |
| | 180 | | } |
| 0 | 181 | | } |
| | 182 | |
|
| | 183 | | private void OnDestroy() |
| | 184 | | { |
| 8 | 185 | | friendPool.Dispose(); |
| 8 | 186 | | thumbnailHandler.Dispose(); |
| | 187 | |
|
| 8 | 188 | | friendsHandler.onFriendAdded -= OnFriendAdded; |
| 8 | 189 | | friendsHandler.onFriendRemoved -= OnFriendRemoved; |
| | 190 | |
|
| 8 | 191 | | RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed; |
| 8 | 192 | | RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess; |
| 8 | 193 | | } |
| | 194 | |
|
| | 195 | | private void OnEnable() |
| | 196 | | { |
| 10 | 197 | | jumpInButtonAnimator.gameObject.SetActive(false); |
| 10 | 198 | | jumpInHoverArea.enabled = isLoaded; |
| 10 | 199 | | thumbnailImage.gameObject.SetActive(isLoaded); |
| | 200 | |
|
| 10 | 201 | | if (isLoaded) |
| | 202 | | { |
| 0 | 203 | | animationHandler.SetLoaded(); |
| | 204 | | } |
| 10 | 205 | | } |
| | 206 | |
|
| | 207 | | private void OnCrowdInfoUpdated(HotScenesController.HotSceneInfo info) |
| | 208 | | { |
| 6 | 209 | | crowdCount.text = info.usersTotalCount.ToString(); |
| 6 | 210 | | crowdCountContainer.SetActive(info.usersTotalCount > 0); |
| 6 | 211 | | } |
| | 212 | |
|
| | 213 | | private void OnFriendAdded(UserProfile profile, Color backgroundColor) |
| | 214 | | { |
| 2 | 215 | | var view = friendPool.GetView(); |
| 2 | 216 | | view.SetUserProfile(profile, backgroundColor); |
| 2 | 217 | | friendViewById.Add(profile.userId, view); |
| 2 | 218 | | } |
| | 219 | |
|
| | 220 | | private void OnFriendRemoved(UserProfile profile) |
| | 221 | | { |
| 2 | 222 | | if (friendViewById.TryGetValue(profile.userId, out ExploreFriendsView view)) |
| | 223 | | { |
| 2 | 224 | | friendPool.PoolView(view); |
| 2 | 225 | | friendViewById.Remove(profile.userId); |
| | 226 | | } |
| 2 | 227 | | } |
| | 228 | |
|
| | 229 | | private void OnMapInfoUpdated(HotScenesController.HotSceneInfo sceneInfo) |
| | 230 | | { |
| 6 | 231 | | sceneName.text = sceneInfo.name; |
| | 232 | |
|
| 6 | 233 | | FetchThumbnail(sceneInfo.thumbnail, |
| 6 | 234 | | onFail: () => FetchThumbnail(MapUtils.GetMarketPlaceThumbnailUrl(sceneInfo.parcels, THMBL_MARKETPLACE_WIDTH, |
| 0 | 235 | | onFail: () => SetThumbnail(errorThumbnail.texture))); |
| 6 | 236 | | } |
| | 237 | |
|
| 24 | 238 | | private void FetchThumbnail(string url, Action onFail) { thumbnailHandler.FetchThumbnail(url, SetThumbnail, onFail); |
| | 239 | |
|
| | 240 | | private void SetThumbnail(Texture2D texture) |
| | 241 | | { |
| 0 | 242 | | thumbnailImage.texture = texture; |
| 0 | 243 | | thumbnailImage.gameObject.SetActive(true); |
| 0 | 244 | | OnThumbnailSet?.Invoke(texture); |
| | 245 | |
|
| 0 | 246 | | SetLoaded(); |
| 0 | 247 | | } |
| | 248 | |
|
| | 249 | | private void SetLoaded() |
| | 250 | | { |
| 0 | 251 | | isLoaded = true; |
| 0 | 252 | | animationHandler.SetLoaded(); |
| 0 | 253 | | jumpInHoverArea.enabled = true; |
| 0 | 254 | | } |
| | 255 | | } |