| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DG.Tweening; |
| | 3 | | using MainScripts.DCL.Controllers.HUD.CharacterPreview; |
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | namespace DCL.Backpack |
| | 10 | | { |
| | 11 | | public class OutfitsSectionComponentView : BaseComponentView, IOutfitsSectionComponentView |
| | 12 | | { |
| | 13 | | [SerializeField] internal Button backButton; |
| | 14 | | [SerializeField] internal OutfitComponentView[] outfitComponentViews; |
| | 15 | | [SerializeField] private RawImage avatarPreviewImage; |
| | 16 | | [SerializeField] private GameObject discardOutfitModal; |
| | 17 | | [SerializeField] private Button confirmDiscardOutfit; |
| | 18 | | [SerializeField] private Button cancelDiscardOutfit; |
| | 19 | | [SerializeField] private Button closeDiscardOutfit; |
| | 20 | |
|
| | 21 | | private ICharacterPreviewController characterPreviewController; |
| | 22 | | private bool isGuest; |
| | 23 | |
|
| | 24 | | public event Action OnBackButtonPressed; |
| | 25 | | public event Action<OutfitItem> OnOutfitEquipped; |
| | 26 | | public event Action<int> OnOutfitDiscarded; |
| | 27 | | public event Action<int> OnOutfitLocalSave; |
| | 28 | | public event Action OnTrySaveAsGuest; |
| | 29 | |
|
| | 30 | | private int indexToBeDiscarded; |
| | 31 | |
|
| | 32 | | public override void Awake() |
| | 33 | | { |
| 0 | 34 | | base.Awake(); |
| | 35 | |
|
| 0 | 36 | | backButton.onClick.RemoveAllListeners(); |
| 0 | 37 | | backButton.onClick.AddListener(() => OnBackButtonPressed?.Invoke()); |
| | 38 | |
|
| 0 | 39 | | foreach (OutfitComponentView outfitComponentView in outfitComponentViews) |
| | 40 | | { |
| 0 | 41 | | outfitComponentView.OnEquipOutfit += OnEquipOutfit; |
| 0 | 42 | | outfitComponentView.OnSaveOutfit += OnSaveOutfit; |
| 0 | 43 | | outfitComponentView.OnDiscardOutfit += DiscardOutfit; |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | if (confirmDiscardOutfit != null) |
| | 47 | | { |
| 0 | 48 | | confirmDiscardOutfit.onClick.RemoveAllListeners(); |
| 0 | 49 | | confirmDiscardOutfit.onClick.AddListener(CompleteDiscardOutfit); |
| | 50 | | } |
| | 51 | |
|
| 0 | 52 | | if (cancelDiscardOutfit != null) |
| | 53 | | { |
| 0 | 54 | | cancelDiscardOutfit.onClick.RemoveAllListeners(); |
| 0 | 55 | | cancelDiscardOutfit.onClick.AddListener(() => discardOutfitModal.SetActive(false)); |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | if (closeDiscardOutfit != null) |
| | 59 | | { |
| 0 | 60 | | closeDiscardOutfit.onClick.RemoveAllListeners(); |
| 0 | 61 | | closeDiscardOutfit.onClick.AddListener(() => discardOutfitModal.SetActive(false)); |
| | 62 | | } |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | private void CompleteDiscardOutfit() |
| | 66 | | { |
| 0 | 67 | | outfitComponentViews[indexToBeDiscarded].SetIsEmpty(true); |
| 0 | 68 | | OnOutfitDiscarded?.Invoke(indexToBeDiscarded); |
| 0 | 69 | | DeleteAnimation(outfitComponentViews[indexToBeDiscarded].transform); |
| 0 | 70 | | discardOutfitModal.SetActive(false); |
| 0 | 71 | | } |
| | 72 | |
|
| 0 | 73 | | public override void RefreshControl() { } |
| | 74 | |
|
| | 75 | | private void DiscardOutfit(int outfitIndex) |
| | 76 | | { |
| 0 | 77 | | indexToBeDiscarded = outfitIndex; |
| 0 | 78 | | discardOutfitModal.SetActive(true); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | public void Initialize(ICharacterPreviewFactory characterPreviewFactory) |
| | 82 | | { |
| 0 | 83 | | characterPreviewController = characterPreviewFactory.Create( |
| | 84 | | loadingMode: CharacterPreviewMode.WithoutHologram, |
| | 85 | | renderTexture: (RenderTexture)avatarPreviewImage.texture, |
| | 86 | | isVisible: false, |
| | 87 | | previewCameraFocus: PreviewCameraFocus.DefaultEditing, |
| | 88 | | isAvatarShadowActive: true); |
| | 89 | |
|
| 0 | 90 | | characterPreviewController.SetFocus(PreviewCameraFocus.BodySnapshot); |
| 0 | 91 | | } |
| | 92 | |
|
| | 93 | | public async UniTask<bool> ShowOutfit(OutfitItem outfit, AvatarModel newModel, CancellationToken ct) |
| | 94 | | { |
| 0 | 95 | | if (string.IsNullOrEmpty(outfit.outfit.bodyShape)) |
| 0 | 96 | | return false; |
| | 97 | |
|
| 0 | 98 | | characterPreviewController.SetEnabled(true); |
| 0 | 99 | | outfitComponentViews[outfit.slot].SetIsEmpty(false); |
| 0 | 100 | | outfitComponentViews[outfit.slot].SetOutfit(outfit); |
| 0 | 101 | | await characterPreviewController.TryUpdateModelAsync(newModel, ct); |
| 0 | 102 | | Texture2D bodySnapshot = await characterPreviewController.TakeBodySnapshotAsync(); |
| 0 | 103 | | AudioScriptableObjects.listItemAppear.Play(true); |
| 0 | 104 | | outfitComponentViews[outfit.slot].SetOutfitPreviewImage(bodySnapshot); |
| 0 | 105 | | outfitComponentViews[outfit.slot].SetIsLoading(false); |
| 0 | 106 | | characterPreviewController.SetEnabled(false); |
| 0 | 107 | | return true; |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | public void SetSlotsAsLoading(OutfitItem[] outfitsToShow) |
| | 111 | | { |
| 0 | 112 | | foreach (var outfitItem in outfitsToShow) |
| | 113 | | { |
| 0 | 114 | | if (string.IsNullOrEmpty(outfitItem.outfit.bodyShape)) |
| | 115 | | { |
| 0 | 116 | | outfitComponentViews[outfitItem.slot].SetIsLoading(false); |
| 0 | 117 | | outfitComponentViews[outfitItem.slot].SetIsEmpty(true); |
| | 118 | | } |
| 0 | 119 | | else { outfitComponentViews[outfitItem.slot].SetIsLoading(true); } |
| | 120 | | } |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | private void OnEquipOutfit(OutfitItem outfitItem) => |
| 0 | 124 | | OnOutfitEquipped?.Invoke(outfitItem); |
| | 125 | |
|
| | 126 | | private void OnSaveOutfit(int outfitIndex) |
| | 127 | | { |
| 0 | 128 | | if (isGuest) |
| | 129 | | { |
| 0 | 130 | | OnTrySaveAsGuest?.Invoke(); |
| 0 | 131 | | return; |
| | 132 | | } |
| | 133 | |
|
| 0 | 134 | | outfitComponentViews[outfitIndex].SetIsLoading(true); |
| 0 | 135 | | outfitComponentViews[outfitIndex].SetIsEmpty(false); |
| 0 | 136 | | OnOutfitLocalSave?.Invoke(outfitIndex); |
| 0 | 137 | | SaveAnimation(outfitComponentViews[outfitIndex].transform); |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | public void SetIsGuest(bool guest) |
| | 141 | | { |
| 0 | 142 | | this.isGuest = guest; |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | private void SaveAnimation(Transform transformToAnimate) => |
| 0 | 146 | | transformToAnimate.DOJump(transformToAnimate.position, 20, 1, 0.6f); |
| | 147 | |
|
| | 148 | | private void DeleteAnimation(Transform transformToAnimate) => |
| 0 | 149 | | transformToAnimate.DOPunchPosition(new Vector3(5, 2, 1), 0.6f); |
| | 150 | | } |
| | 151 | | } |