| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Runtime.CompilerServices; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | [assembly: InternalsVisibleTo("AvatarEditorHUDTests")] |
| | 7 | |
|
| | 8 | | public class AvatarEditorHUDView : MonoBehaviour |
| | 9 | | { |
| 1 | 10 | | private static readonly int RANDOMIZE_ANIMATOR_LOADING_BOOL = Animator.StringToHash("Loading"); |
| | 11 | | private const string VIEW_PATH = "AvatarEditorHUD"; |
| | 12 | | private const string VIEW_OBJECT_NAME = "_AvatarEditorHUD"; |
| | 13 | |
|
| 0 | 14 | | public bool isOpen { get; private set; } |
| | 15 | |
|
| | 16 | | [System.Serializable] |
| | 17 | | public class AvatarEditorNavigationInfo |
| | 18 | | { |
| | 19 | | public Toggle toggle; |
| | 20 | | public Canvas canvas; |
| | 21 | | public bool enabledByDefault; |
| | 22 | | public CharacterPreviewController.CameraFocus focus = CharacterPreviewController.CameraFocus.DefaultEditing; |
| | 23 | |
|
| | 24 | | //To remove when we refactor this to avoid ToggleGroup issues when quitting application |
| 1672 | 25 | | public void Initialize() { Application.quitting += () => toggle.onValueChanged.RemoveAllListeners(); } |
| | 26 | | } |
| | 27 | |
|
| | 28 | | [System.Serializable] |
| | 29 | | public class AvatarEditorWearableFilter |
| | 30 | | { |
| | 31 | | public string categoryFilter; |
| | 32 | | public ItemSelector selector; |
| | 33 | | } |
| | 34 | |
|
| | 35 | | [SerializeField] |
| | 36 | | internal InputAction_Trigger closeAction; |
| | 37 | |
|
| | 38 | | [SerializeField] |
| | 39 | | internal Canvas avatarEditorCanvas; |
| | 40 | |
|
| | 41 | | [SerializeField] |
| | 42 | | internal CanvasGroup avatarEditorCanvasGroup; |
| | 43 | |
|
| | 44 | | [SerializeField] |
| | 45 | | internal AvatarEditorNavigationInfo[] navigationInfos; |
| | 46 | |
|
| | 47 | | [SerializeField] |
| | 48 | | internal AvatarEditorWearableFilter[] wearableGridPairs; |
| | 49 | |
|
| | 50 | | [SerializeField] |
| | 51 | | internal AvatarEditorNavigationInfo collectiblesNavigationInfo; |
| | 52 | |
|
| | 53 | | [SerializeField] |
| | 54 | | internal ItemSelector collectiblesItemSelector; |
| | 55 | |
|
| | 56 | | [SerializeField] |
| | 57 | | internal ColorSelector skinColorSelector; |
| | 58 | |
|
| | 59 | | [SerializeField] |
| | 60 | | internal ColorSelector eyeColorSelector; |
| | 61 | |
|
| | 62 | | [SerializeField] |
| | 63 | | internal ColorSelector hairColorSelector; |
| | 64 | |
|
| | 65 | | [SerializeField] |
| | 66 | | internal GameObject characterPreviewPrefab; |
| | 67 | |
|
| | 68 | | [SerializeField] |
| | 69 | | internal PreviewCameraRotation characterPreviewRotation; |
| | 70 | |
|
| | 71 | | [SerializeField] |
| | 72 | | internal Button randomizeButton; |
| | 73 | |
|
| | 74 | | [SerializeField] |
| | 75 | | internal Animator randomizeAnimator; |
| | 76 | |
|
| | 77 | | [SerializeField] |
| | 78 | | internal Button doneButton; |
| | 79 | |
|
| | 80 | | [SerializeField] |
| | 81 | | internal Button exitButton; |
| | 82 | |
|
| | 83 | | [SerializeField] internal GameObject loadingSpinnerGameObject; |
| | 84 | |
|
| | 85 | | [Header("Collectibles")] |
| | 86 | | [SerializeField] |
| | 87 | | internal GameObject web3Container; |
| | 88 | |
|
| | 89 | | [SerializeField] |
| | 90 | | internal Button web3GoToMarketplaceButton; |
| | 91 | |
|
| | 92 | | [SerializeField] |
| | 93 | | internal GameObject noWeb3Container; |
| | 94 | |
|
| | 95 | | [SerializeField] |
| | 96 | | internal Button noWeb3GoToMarketplaceButton; |
| | 97 | |
|
| | 98 | | internal static CharacterPreviewController characterPreviewController; |
| | 99 | | private AvatarEditorHUDController controller; |
| 88 | 100 | | internal readonly Dictionary<string, ItemSelector> selectorsByCategory = new Dictionary<string, ItemSelector>(); |
| 88 | 101 | | private readonly HashSet<WearableItem> wearablesWithLoadingSpinner = new HashSet<WearableItem>(); |
| | 102 | |
|
| | 103 | | public event System.Action<AvatarModel> OnAvatarAppear; |
| | 104 | | public event System.Action<bool> OnSetVisibility; |
| | 105 | | public event System.Action OnRandomize; |
| | 106 | | public event System.Action OnCloseActionTriggered; |
| | 107 | |
|
| | 108 | | private void Awake() |
| | 109 | | { |
| 44 | 110 | | closeAction.OnTriggered += CloseAction_OnTriggered; |
| 44 | 111 | | loadingSpinnerGameObject.SetActive(false); |
| 44 | 112 | | doneButton.interactable = false; //the default state of the button should be disable until a profile has been lo |
| 44 | 113 | | if (characterPreviewController == null) |
| | 114 | | { |
| 44 | 115 | | characterPreviewController = GameObject.Instantiate(characterPreviewPrefab).GetComponent<CharacterPreviewCon |
| 44 | 116 | | characterPreviewController.name = "_CharacterPreviewController"; |
| | 117 | | } |
| | 118 | |
|
| 44 | 119 | | isOpen = false; |
| 44 | 120 | | } |
| | 121 | |
|
| 88 | 122 | | private void OnDestroy() { closeAction.OnTriggered -= CloseAction_OnTriggered; } |
| | 123 | |
|
| 0 | 124 | | private void CloseAction_OnTriggered(DCLAction_Trigger action) { OnCloseActionTriggered?.Invoke(); } |
| | 125 | |
|
| | 126 | | private void Initialize(AvatarEditorHUDController controller) |
| | 127 | | { |
| 44 | 128 | | ItemToggle.getEquippedWearablesReplacedByFunc = controller.GetWearablesReplacedBy; |
| 44 | 129 | | this.controller = controller; |
| 44 | 130 | | gameObject.name = VIEW_OBJECT_NAME; |
| | 131 | |
|
| 44 | 132 | | randomizeButton.onClick.AddListener(OnRandomizeButton); |
| 44 | 133 | | doneButton.onClick.AddListener(OnDoneButton); |
| 44 | 134 | | exitButton.onClick.AddListener(OnExitButton); |
| 44 | 135 | | InitializeNavigationEvents(); |
| 44 | 136 | | InitializeWearableChangeEvents(); |
| | 137 | |
|
| 44 | 138 | | web3GoToMarketplaceButton.onClick.RemoveAllListeners(); |
| 44 | 139 | | noWeb3GoToMarketplaceButton.onClick.RemoveAllListeners(); |
| 44 | 140 | | web3GoToMarketplaceButton.onClick.AddListener(controller.GoToMarketplace); |
| 44 | 141 | | noWeb3GoToMarketplaceButton.onClick.AddListener(controller.GoToMarketplace); |
| | 142 | |
|
| 44 | 143 | | characterPreviewController.camera.enabled = false; |
| 44 | 144 | | } |
| | 145 | |
|
| | 146 | | public void SetIsWeb3(bool isWeb3User) |
| | 147 | | { |
| 95 | 148 | | web3Container.SetActive(isWeb3User); |
| 95 | 149 | | noWeb3Container.SetActive(!isWeb3User); |
| 95 | 150 | | } |
| | 151 | |
|
| | 152 | | private void InitializeNavigationEvents() |
| | 153 | | { |
| 1672 | 154 | | for (int i = 0; i < navigationInfos.Length; i++) |
| | 155 | | { |
| 792 | 156 | | InitializeNavigationInfo(navigationInfos[i]); |
| | 157 | | } |
| | 158 | |
|
| 44 | 159 | | InitializeNavigationInfo(collectiblesNavigationInfo); |
| | 160 | |
|
| 44 | 161 | | characterPreviewRotation.OnHorizontalRotation += characterPreviewController.Rotate; |
| 44 | 162 | | } |
| | 163 | |
|
| | 164 | | private void InitializeNavigationInfo(AvatarEditorNavigationInfo current) |
| | 165 | | { |
| 836 | 166 | | current.Initialize(); |
| | 167 | |
|
| 836 | 168 | | current.toggle.isOn = current.enabledByDefault; |
| | 169 | |
|
| 836 | 170 | | current.canvas.gameObject.SetActive(current.enabledByDefault); |
| 836 | 171 | | current.toggle.onValueChanged.AddListener((on) => |
| | 172 | | { |
| 0 | 173 | | current.canvas.gameObject.SetActive(@on); |
| 0 | 174 | | characterPreviewController.SetFocus(current.focus); |
| 0 | 175 | | }); |
| 836 | 176 | | } |
| | 177 | |
|
| | 178 | | private void InitializeWearableChangeEvents() |
| | 179 | | { |
| 44 | 180 | | int nPairs = wearableGridPairs.Length; |
| 1496 | 181 | | for (int i = 0; i < nPairs; i++) |
| | 182 | | { |
| 704 | 183 | | wearableGridPairs[i].selector.OnItemClicked += controller.WearableClicked; |
| 704 | 184 | | wearableGridPairs[i].selector.OnSellClicked += controller.SellCollectible; |
| 704 | 185 | | selectorsByCategory.Add(wearableGridPairs[i].categoryFilter, wearableGridPairs[i].selector); |
| | 186 | | } |
| | 187 | |
|
| 44 | 188 | | collectiblesItemSelector.OnItemClicked += controller.WearableClicked; |
| 44 | 189 | | collectiblesItemSelector.OnSellClicked += controller.SellCollectible; |
| 44 | 190 | | collectiblesItemSelector.OnRetryClicked += controller.RetryLoadOwnedWearables; |
| | 191 | |
|
| 44 | 192 | | skinColorSelector.OnColorChanged += controller.SkinColorClicked; |
| 44 | 193 | | eyeColorSelector.OnColorChanged += controller.EyesColorClicked; |
| 44 | 194 | | hairColorSelector.OnColorChanged += controller.HairColorClicked; |
| 44 | 195 | | } |
| | 196 | |
|
| | 197 | | internal static AvatarEditorHUDView Create(AvatarEditorHUDController controller) |
| | 198 | | { |
| 44 | 199 | | var view = Instantiate(Resources.Load<GameObject>(VIEW_PATH)).GetComponent<AvatarEditorHUDView>(); |
| 44 | 200 | | view.Initialize(controller); |
| 44 | 201 | | return view; |
| | 202 | | } |
| | 203 | |
|
| | 204 | | public void UpdateSelectedBody(WearableItem bodyShape) |
| | 205 | | { |
| 1632 | 206 | | for (int i = 0; i < wearableGridPairs.Length; i++) |
| | 207 | | { |
| 768 | 208 | | if (wearableGridPairs[i].categoryFilter == WearableLiterals.Categories.BODY_SHAPE) |
| | 209 | | { |
| 48 | 210 | | wearableGridPairs[i].selector.UnselectAll(); |
| 48 | 211 | | wearableGridPairs[i].selector.Select(bodyShape.id); |
| 48 | 212 | | } |
| | 213 | | else |
| | 214 | | { |
| 720 | 215 | | wearableGridPairs[i].selector.SetBodyShape(bodyShape.id); |
| | 216 | | } |
| | 217 | | } |
| | 218 | |
|
| 48 | 219 | | collectiblesItemSelector.SetBodyShape(bodyShape.id); |
| 48 | 220 | | } |
| | 221 | |
|
| | 222 | | public void EquipWearable(WearableItem wearable) |
| | 223 | | { |
| 940 | 224 | | selectorsByCategory[wearable.data.category].Select(wearable.id); |
| 940 | 225 | | SetWearableLoadingSpinner(wearable, true); |
| 940 | 226 | | collectiblesItemSelector.Select(wearable.id); |
| 940 | 227 | | } |
| | 228 | |
|
| | 229 | | public void UnequipWearable(WearableItem wearable) |
| | 230 | | { |
| 419 | 231 | | selectorsByCategory[wearable.data.category].Unselect(wearable.id); |
| 419 | 232 | | SetWearableLoadingSpinner(wearable, false); |
| 419 | 233 | | collectiblesItemSelector.Unselect(wearable.id); |
| 419 | 234 | | } |
| | 235 | |
|
| | 236 | | internal void SetWearableLoadingSpinner(WearableItem wearable, bool isActive) |
| | 237 | | { |
| 1359 | 238 | | selectorsByCategory[wearable.data.category].SetWearableLoadingSpinner(wearable.id, isActive); |
| 1359 | 239 | | collectiblesItemSelector.SetWearableLoadingSpinner(wearable.id, isActive); |
| 1359 | 240 | | if (isActive) |
| 940 | 241 | | wearablesWithLoadingSpinner.Add(wearable); |
| | 242 | | else |
| 419 | 243 | | wearablesWithLoadingSpinner.Remove(wearable); |
| 419 | 244 | | } |
| | 245 | |
|
| | 246 | | internal void ClearWearablesLoadingSpinner() |
| | 247 | | { |
| 74 | 248 | | foreach (WearableItem wearable in wearablesWithLoadingSpinner) |
| | 249 | | { |
| 17 | 250 | | selectorsByCategory[wearable.data.category].SetWearableLoadingSpinner(wearable.id, false); |
| 17 | 251 | | collectiblesItemSelector.SetWearableLoadingSpinner(wearable.id, false); |
| | 252 | | } |
| | 253 | |
|
| 20 | 254 | | wearablesWithLoadingSpinner.Clear(); |
| 20 | 255 | | } |
| | 256 | |
|
| 200 | 257 | | public void SelectHairColor(Color hairColor) { hairColorSelector.Select(hairColor); } |
| | 258 | |
|
| 198 | 259 | | public void SelectSkinColor(Color skinColor) { skinColorSelector.Select(skinColor); } |
| | 260 | |
|
| 200 | 261 | | public void SelectEyeColor(Color eyesColor) { eyeColorSelector.Select(eyesColor); } |
| | 262 | |
|
| | 263 | | public void SetColors(List<Color> skinColors, List<Color> hairColors, List<Color> eyeColors) |
| | 264 | | { |
| 44 | 265 | | skinColorSelector.Populate(skinColors); |
| 44 | 266 | | eyeColorSelector.Populate(eyeColors); |
| 44 | 267 | | hairColorSelector.Populate(hairColors); |
| 44 | 268 | | } |
| | 269 | |
|
| | 270 | | public void UnselectAllWearables() |
| | 271 | | { |
| 3264 | 272 | | for (int i = 0; i < wearableGridPairs.Length; i++) |
| | 273 | | { |
| 1536 | 274 | | if (wearableGridPairs[i].categoryFilter != WearableLiterals.Categories.BODY_SHAPE) |
| | 275 | | { |
| 1440 | 276 | | wearableGridPairs[i].selector.UnselectAll(); |
| | 277 | | } |
| | 278 | | } |
| | 279 | |
|
| 96 | 280 | | collectiblesItemSelector.UnselectAll(); |
| 96 | 281 | | } |
| | 282 | |
|
| | 283 | | public void UpdateAvatarPreview(AvatarModel avatarModel) |
| | 284 | | { |
| 117 | 285 | | if (avatarModel?.wearables == null) |
| 0 | 286 | | return; |
| | 287 | |
|
| 117 | 288 | | doneButton.interactable = false; |
| 117 | 289 | | loadingSpinnerGameObject.SetActive(true); |
| 117 | 290 | | characterPreviewController.UpdateModel(avatarModel, |
| | 291 | | () => |
| | 292 | | { |
| 20 | 293 | | if (doneButton != null) |
| 0 | 294 | | doneButton.interactable = true; |
| | 295 | |
|
| 20 | 296 | | loadingSpinnerGameObject?.SetActive(false); |
| 20 | 297 | | OnAvatarAppear?.Invoke(avatarModel); |
| 20 | 298 | | ClearWearablesLoadingSpinner(); |
| 20 | 299 | | randomizeAnimator?.SetBool(RANDOMIZE_ANIMATOR_LOADING_BOOL, false); |
| 0 | 300 | | }); |
| 117 | 301 | | } |
| | 302 | |
|
| | 303 | | public void AddWearable(WearableItem wearableItem, int amount) |
| | 304 | | { |
| 3880 | 305 | | if (wearableItem == null) |
| 0 | 306 | | return; |
| | 307 | |
|
| 3880 | 308 | | if (!selectorsByCategory.ContainsKey(wearableItem.data.category)) |
| | 309 | | { |
| 0 | 310 | | Debug.LogError($"Category couldn't find selector for category: {wearableItem.data.category} "); |
| 0 | 311 | | return; |
| | 312 | | } |
| | 313 | |
|
| 3880 | 314 | | selectorsByCategory[wearableItem.data.category].AddItemToggle(wearableItem, amount); |
| 3880 | 315 | | if (wearableItem.IsCollectible()) |
| | 316 | | { |
| 16 | 317 | | collectiblesItemSelector.AddItemToggle(wearableItem, amount); |
| | 318 | | } |
| 3880 | 319 | | } |
| | 320 | |
|
| | 321 | | public void RemoveWearable(WearableItem wearableItem) |
| | 322 | | { |
| 0 | 323 | | if (wearableItem == null) |
| 0 | 324 | | return; |
| | 325 | |
|
| 0 | 326 | | if (!selectorsByCategory.ContainsKey(wearableItem.data.category)) |
| | 327 | | { |
| 0 | 328 | | Debug.LogError($"Category couldn't find selector for category: {wearableItem.data.category} "); |
| 0 | 329 | | return; |
| | 330 | | } |
| | 331 | |
|
| 0 | 332 | | selectorsByCategory[wearableItem.data.category].RemoveItemToggle(wearableItem.id); |
| 0 | 333 | | if (wearableItem.IsCollectible()) |
| 0 | 334 | | collectiblesItemSelector.RemoveItemToggle(wearableItem.id); |
| 0 | 335 | | } |
| | 336 | |
|
| | 337 | | public void RemoveAllWearables() |
| | 338 | | { |
| 139 | 339 | | using (var enumerator = selectorsByCategory.GetEnumerator()) |
| | 340 | | { |
| 2363 | 341 | | while (enumerator.MoveNext()) |
| | 342 | | { |
| 2224 | 343 | | enumerator.Current.Value.RemoveAllItemToggle(); |
| | 344 | | } |
| 139 | 345 | | } |
| | 346 | |
|
| 139 | 347 | | collectiblesItemSelector.RemoveAllItemToggle(); |
| 139 | 348 | | } |
| | 349 | |
|
| | 350 | | private void OnRandomizeButton() |
| | 351 | | { |
| 0 | 352 | | OnRandomize?.Invoke(); |
| 0 | 353 | | controller.RandomizeWearables(); |
| 0 | 354 | | randomizeAnimator?.SetBool(RANDOMIZE_ANIMATOR_LOADING_BOOL, true); |
| 0 | 355 | | } |
| | 356 | |
|
| | 357 | | private void OnDoneButton() |
| | 358 | | { |
| 0 | 359 | | doneButton.interactable = false; |
| 0 | 360 | | characterPreviewController.TakeSnapshots(OnSnapshotsReady, OnSnapshotsFailed); |
| 0 | 361 | | } |
| | 362 | |
|
| 0 | 363 | | private void OnExitButton() { OnCloseActionTriggered?.Invoke(); } |
| | 364 | |
|
| | 365 | | private void OnSnapshotsReady(Texture2D face, Texture2D face128, Texture2D face256, Texture2D body) |
| | 366 | | { |
| 0 | 367 | | doneButton.interactable = true; |
| 0 | 368 | | controller.SaveAvatar(face, face128, face256, body); |
| 0 | 369 | | } |
| | 370 | |
|
| 0 | 371 | | private void OnSnapshotsFailed() { doneButton.interactable = true; } |
| | 372 | |
|
| | 373 | | public void SetVisibility(bool visible) |
| | 374 | | { |
| 84 | 375 | | characterPreviewController.camera.enabled = visible; |
| 84 | 376 | | avatarEditorCanvas.enabled = visible; |
| 84 | 377 | | avatarEditorCanvasGroup.blocksRaycasts = visible; |
| | 378 | |
|
| 84 | 379 | | if (visible && !isOpen) |
| | 380 | | { |
| 39 | 381 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 39 | 382 | | OnSetVisibility?.Invoke(visible); |
| 0 | 383 | | } |
| 45 | 384 | | else if (!visible && isOpen) |
| | 385 | | { |
| 1 | 386 | | AudioScriptableObjects.dialogClose.Play(true); |
| 1 | 387 | | OnSetVisibility?.Invoke(visible); |
| | 388 | | } |
| | 389 | |
|
| 84 | 390 | | isOpen = visible; |
| 84 | 391 | | } |
| | 392 | |
|
| | 393 | | public void CleanUp() |
| | 394 | | { |
| 44 | 395 | | loadingSpinnerGameObject = null; |
| 44 | 396 | | randomizeAnimator = null; |
| 44 | 397 | | if (wearableGridPairs != null) |
| | 398 | | { |
| 44 | 399 | | int nPairs = wearableGridPairs.Length; |
| 1496 | 400 | | for (int i = 0; i < nPairs; i++) |
| | 401 | | { |
| 704 | 402 | | var itemSelector = wearableGridPairs[i].selector; |
| 704 | 403 | | if (itemSelector != null) |
| | 404 | | { |
| 704 | 405 | | itemSelector.OnItemClicked -= controller.WearableClicked; |
| 704 | 406 | | itemSelector.OnSellClicked -= controller.SellCollectible; |
| | 407 | | } |
| | 408 | | } |
| | 409 | | } |
| | 410 | |
|
| 44 | 411 | | if (collectiblesItemSelector != null) |
| | 412 | | { |
| 44 | 413 | | collectiblesItemSelector.OnItemClicked -= controller.WearableClicked; |
| 44 | 414 | | collectiblesItemSelector.OnSellClicked -= controller.SellCollectible; |
| 44 | 415 | | collectiblesItemSelector.OnRetryClicked -= controller.RetryLoadOwnedWearables; |
| | 416 | | } |
| | 417 | |
|
| 44 | 418 | | if (skinColorSelector != null) |
| 44 | 419 | | skinColorSelector.OnColorChanged -= controller.SkinColorClicked; |
| 44 | 420 | | if (eyeColorSelector != null) |
| 44 | 421 | | eyeColorSelector.OnColorChanged -= controller.EyesColorClicked; |
| 44 | 422 | | if (hairColorSelector != null) |
| 44 | 423 | | hairColorSelector.OnColorChanged -= controller.HairColorClicked; |
| | 424 | |
|
| 44 | 425 | | if (this != null) |
| 44 | 426 | | Destroy(gameObject); |
| | 427 | |
|
| 44 | 428 | | if (characterPreviewController != null) |
| | 429 | | { |
| 44 | 430 | | Destroy(characterPreviewController.gameObject); |
| 44 | 431 | | characterPreviewController = null; |
| | 432 | | } |
| 44 | 433 | | } |
| | 434 | |
|
| 2 | 435 | | public void ShowCollectiblesLoadingSpinner(bool isActive) { collectiblesItemSelector.ShowLoading(isActive); } |
| | 436 | |
|
| 2 | 437 | | public void ShowCollectiblesLoadingRetry(bool isActive) { collectiblesItemSelector.ShowRetryLoading(isActive); } |
| | 438 | | } |