| | 1 | | using System.Collections.Generic; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | public interface IRealmRowComponentView |
| | 7 | | { |
| | 8 | | RealmHandler friendsHandler { get; set; } |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Event that will be triggered when the Warp In button is clicked. |
| | 12 | | /// </summary> |
| | 13 | | Button.ButtonClickedEvent onWarpInClick { get; } |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// Set the name label. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="name">A string.</param> |
| | 19 | | void SetName(string name); |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Set the number of players label. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="numberOfPlayers">Number of players.</param> |
| | 25 | | void SetNumberOfPlayers(int numberOfPlayers); |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Show/hide the connected mark. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="isConnected">True for showing the connected mark.</param> |
| | 31 | | void SetAsConnected(bool isConnected); |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Set the background color of the row. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="color">Color to apply.</param> |
| | 37 | | void SetRowColor(Color color); |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Set the background color of the row when it is hovered. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="color">Color to apply.</param> |
| | 43 | | void SetOnHoverColor(Color color); |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public class RealmRowComponentView : BaseComponentView, IRealmRowComponentView, IComponentModelConfig |
| | 47 | | { |
| | 48 | | [Header("Assets References")] |
| | 49 | | [SerializeField] internal FriendHeadForPlaceCardComponentView friendHeadPrefab; |
| | 50 | |
|
| | 51 | | [Header("Prefab References")] |
| | 52 | | [SerializeField] internal TMP_Text nameText; |
| | 53 | | [SerializeField] internal TMP_Text playersText; |
| | 54 | | [SerializeField] internal ButtonComponentView warpInButton; |
| | 55 | | [SerializeField] internal GameObject connectedMark; |
| | 56 | | [SerializeField] internal Image backgroundImage; |
| | 57 | | [SerializeField] internal GridContainerComponentView friendsGrid; |
| | 58 | |
|
| | 59 | | [Header("Configuration")] |
| 15 | 60 | | [SerializeField] internal int maxFriendsToShow = 6; |
| | 61 | | [SerializeField] internal RealmRowComponentModel model; |
| | 62 | |
|
| | 63 | | internal Color originalBackgroundColor; |
| | 64 | | internal Color onHoverColor; |
| | 65 | |
|
| 0 | 66 | | public RealmHandler friendsHandler { get; set; } |
| 0 | 67 | | internal RealmInfoHandler realmInfoHandler { get; set; } |
| | 68 | |
|
| 0 | 69 | | public Button.ButtonClickedEvent onWarpInClick => warpInButton?.onClick; |
| | 70 | |
|
| 15 | 71 | | internal Dictionary<string, BaseComponentView> currentFriendHeads = new Dictionary<string, BaseComponentView>(); |
| | 72 | |
|
| | 73 | | public override void Awake() |
| | 74 | | { |
| 14 | 75 | | base.Awake(); |
| | 76 | |
|
| 14 | 77 | | CleanFriendHeadsItems(); |
| | 78 | |
|
| 14 | 79 | | originalBackgroundColor = backgroundImage.color; |
| 14 | 80 | | onHoverColor = backgroundImage.color; |
| 14 | 81 | | } |
| | 82 | |
|
| | 83 | | public void Configure(BaseComponentModel newModel) |
| | 84 | | { |
| 3 | 85 | | model = (RealmRowComponentModel)newModel; |
| | 86 | |
|
| 3 | 87 | | InitializeFriendsTracker(); |
| | 88 | |
|
| 3 | 89 | | if (realmInfoHandler != null) |
| 3 | 90 | | realmInfoHandler.SetRealmInfo(model.name); |
| | 91 | |
|
| 3 | 92 | | RefreshControl(); |
| 3 | 93 | | } |
| | 94 | |
|
| | 95 | | public override void RefreshControl() |
| | 96 | | { |
| 3 | 97 | | if (model == null) |
| 0 | 98 | | return; |
| | 99 | |
|
| 3 | 100 | | SetName(model.name); |
| 3 | 101 | | SetNumberOfPlayers(model.players); |
| 3 | 102 | | SetAsConnected(model.isConnected); |
| 3 | 103 | | SetRowColor(model.backgroundColor); |
| 3 | 104 | | SetOnHoverColor(model.onHoverColor); |
| 3 | 105 | | } |
| | 106 | |
|
| | 107 | | public override void OnFocus() |
| | 108 | | { |
| 0 | 109 | | base.OnFocus(); |
| | 110 | |
|
| 0 | 111 | | backgroundImage.color = onHoverColor; |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | public override void OnLoseFocus() |
| | 115 | | { |
| 18 | 116 | | base.OnLoseFocus(); |
| | 117 | |
|
| 18 | 118 | | backgroundImage.color = originalBackgroundColor; |
| 18 | 119 | | } |
| | 120 | |
|
| | 121 | | public override void Dispose() |
| | 122 | | { |
| 25 | 123 | | base.Dispose(); |
| | 124 | |
|
| 25 | 125 | | if (friendsHandler != null) |
| | 126 | | { |
| 6 | 127 | | friendsHandler.OnFriendAddedEvent -= OnFriendAdded; |
| 6 | 128 | | friendsHandler.OnFriendRemovedEvent -= OnFriendRemoved; |
| | 129 | | } |
| | 130 | |
|
| 25 | 131 | | if (friendsGrid != null) |
| 24 | 132 | | friendsGrid.Dispose(); |
| 25 | 133 | | } |
| | 134 | |
|
| | 135 | | public void SetName(string name) |
| | 136 | | { |
| 4 | 137 | | model.name = name; |
| | 138 | |
|
| 4 | 139 | | if (nameText == null) |
| 0 | 140 | | return; |
| | 141 | |
|
| 4 | 142 | | nameText.text = name.ToUpper(); |
| 4 | 143 | | } |
| | 144 | |
|
| | 145 | | public void SetNumberOfPlayers(int numberOfPlayers) |
| | 146 | | { |
| 4 | 147 | | model.players = numberOfPlayers; |
| | 148 | |
|
| 4 | 149 | | if (playersText == null) |
| 0 | 150 | | return; |
| | 151 | |
|
| 4 | 152 | | playersText.text = ExploreV2CommonUtils.FormatNumberToString(numberOfPlayers); |
| 4 | 153 | | } |
| | 154 | |
|
| | 155 | | public void SetAsConnected(bool isConnected) |
| | 156 | | { |
| 4 | 157 | | model.isConnected = isConnected; |
| | 158 | |
|
| 4 | 159 | | if (connectedMark == null) |
| 0 | 160 | | return; |
| | 161 | |
|
| 4 | 162 | | connectedMark.SetActive(isConnected); |
| 4 | 163 | | warpInButton.gameObject.SetActive(!isConnected); |
| 4 | 164 | | } |
| | 165 | |
|
| | 166 | | public void SetRowColor(Color color) |
| | 167 | | { |
| 8 | 168 | | model.backgroundColor = color; |
| | 169 | |
|
| 8 | 170 | | if (backgroundImage == null) |
| 0 | 171 | | return; |
| | 172 | |
|
| 8 | 173 | | backgroundImage.color = color; |
| 8 | 174 | | originalBackgroundColor = color; |
| 8 | 175 | | } |
| | 176 | |
|
| | 177 | | public void SetOnHoverColor(Color color) |
| | 178 | | { |
| 6 | 179 | | model.onHoverColor = color; |
| 6 | 180 | | onHoverColor = color; |
| 6 | 181 | | } |
| | 182 | |
|
| | 183 | | internal void InitializeFriendsTracker() |
| | 184 | | { |
| 4 | 185 | | CleanFriendHeadsItems(); |
| | 186 | |
|
| 4 | 187 | | if (realmInfoHandler == null) |
| 4 | 188 | | realmInfoHandler = new RealmInfoHandler(); |
| | 189 | |
|
| 4 | 190 | | if (friendsHandler == null) |
| | 191 | | { |
| 4 | 192 | | friendsHandler = new RealmHandler(realmInfoHandler); |
| 4 | 193 | | friendsHandler.OnFriendAddedEvent += OnFriendAdded; |
| 4 | 194 | | friendsHandler.OnFriendRemovedEvent += OnFriendRemoved; |
| | 195 | | } |
| 4 | 196 | | } |
| | 197 | |
|
| | 198 | | internal void OnFriendAdded(UserProfile profile, Color backgroundColor) |
| | 199 | | { |
| 3 | 200 | | if (currentFriendHeads.Count == maxFriendsToShow) |
| 0 | 201 | | return; |
| | 202 | |
|
| 3 | 203 | | if (currentFriendHeads.ContainsKey(profile.userId)) |
| 0 | 204 | | return; |
| | 205 | |
|
| 3 | 206 | | BaseComponentView newFriend = InstantiateAndConfigureFriendHead( |
| | 207 | | new FriendHeadForPlaceCardComponentModel |
| | 208 | | { |
| | 209 | | userProfile = profile, |
| | 210 | | backgroundColor = backgroundColor |
| | 211 | | }, |
| | 212 | | friendHeadPrefab); |
| | 213 | |
|
| 3 | 214 | | if (friendsGrid != null) |
| 3 | 215 | | friendsGrid.AddItem(newFriend); |
| | 216 | |
|
| 3 | 217 | | currentFriendHeads.Add(profile.userId, newFriend); |
| 3 | 218 | | } |
| | 219 | |
|
| | 220 | | internal void OnFriendRemoved(UserProfile profile) |
| | 221 | | { |
| 1 | 222 | | if (!currentFriendHeads.ContainsKey(profile.userId)) |
| 0 | 223 | | return; |
| | 224 | |
|
| 1 | 225 | | if (friendsGrid != null) |
| 1 | 226 | | friendsGrid.RemoveItem(currentFriendHeads[profile.userId]); |
| | 227 | |
|
| 1 | 228 | | currentFriendHeads.Remove(profile.userId); |
| 1 | 229 | | } |
| | 230 | |
|
| | 231 | | internal void CleanFriendHeadsItems() |
| | 232 | | { |
| 19 | 233 | | if (friendsGrid != null) |
| | 234 | | { |
| 19 | 235 | | friendsGrid.RemoveItems(); |
| 19 | 236 | | currentFriendHeads.Clear(); |
| | 237 | | } |
| 19 | 238 | | } |
| | 239 | |
|
| | 240 | | internal BaseComponentView InstantiateAndConfigureFriendHead(FriendHeadForPlaceCardComponentModel friendInfo, Friend |
| | 241 | | { |
| 4 | 242 | | FriendHeadForPlaceCardComponentView friendHeadGO = GameObject.Instantiate(prefabToUse); |
| 4 | 243 | | friendHeadGO.Configure(friendInfo); |
| | 244 | |
|
| 4 | 245 | | return friendHeadGO; |
| | 246 | | } |
| | 247 | | } |