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