-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecipeCardController.cs
191 lines (160 loc) · 7.17 KB
/
RecipeCardController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System.Collections.Generic;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
namespace Cocktailor
{
/// <summary>
/// Controls the behavior of the recipe card in the user interface.
/// </summary>
[RequireComponent(typeof(RecipeCardSwipeHandler))]
public class RecipeCardController : MonoBehaviour
{
[Header("Managers and Controllers")]
[SerializeField] private SfxManager sfxManager;
[SerializeField] private CocktailIconManager iconManager;
[SerializeField] private RecipeCardMarkerController recipeCardMarkerController;
[SerializeField] public PanelAnimControl panelAnimControl;
[Header("QuizCard Components")]
[SerializeField] private RectTransform recipePanelScaler;
[SerializeField] private Image myIcon, noteButton;
[SerializeField] private TMP_InputField noteInputField;
[SerializeField] private Transform initialPosition, basePosition;
[SerializeField] private Text nameText, descriptionText, indexText;
[SerializeField] private GameObject closeButton, transparentBackground;
[SerializeField] private RecipeCardVisibilityController iconVisibility, glasswareVisibility, methodVisibility, garnishVisibility, ingredientVisibility;
public RecipeCardSwipeHandler RecipeCardSwipeHandler { get; private set; }
public int CurrentRecipeIndex { get; private set; }
private void Awake()
{
RecipeCardSwipeHandler = GetComponent<RecipeCardSwipeHandler>();
PlayerData.OnuserMemorizedStatehange += WiggleCard;
}
private void OnDestroy()
{
PlayerData.OnuserMemorizedStatehange -= WiggleCard;
}
public void UpdateCocktailInfo(RecipeCardData cardInfo)
{
CurrentRecipeIndex = cardInfo.RecipeIndex;
var cardIndex = cardInfo.CardIndex;
var maxRecipeCount = cardInfo.CardMaxIndex;
var recipe = CocktailRecipeManger.GetCocktailRecipeByIndex(CurrentRecipeIndex);
UpdateCardUISize(recipe);
UpdateCardUIElements(recipe, cardInfo);
UpdateCardButtonElements(maxRecipeCount);
}
private void UpdateCardUISize(CocktailRecipe recipe)
{
gameObject.GetComponent<RectTransform>().anchoredPosition = new Vector3(0, 30, 0);
SetIngredientUIHeight(GetIngredientUIHeightByAmount(recipe.Ingredient.Count));
myIcon.sprite = iconManager.GetIconSpriteAtIndexOf(CurrentRecipeIndex);
}
private void UpdateCardButtonElements(int maxRecipeCount)
{
var alpha = string.IsNullOrEmpty(PlayerData.GetNote(CurrentRecipeIndex)) ? 0.3f : 0.8f;
noteButton.color = SetColorAlpha(noteButton.color, alpha);
closeButton.SetActive(maxRecipeCount == 1);
indexText.gameObject.SetActive(maxRecipeCount != 1);
recipeCardMarkerController.InitializeMarker(CurrentRecipeIndex);
}
private void UpdateCardUIElements(CocktailRecipe recipe, RecipeCardData cardInfo)
{
indexText.text = cardInfo.CardIndex + 1 + " / " + cardInfo.CardMaxIndex;
RecipeCardSwipeHandler.InitDefaultPosition();
nameText.text = recipe.Name;
descriptionText.text = recipe.Description;
glasswareVisibility.SetCardDescriptionText(recipe.Glassware);
methodVisibility.SetCardDescriptionText(recipe.PreparationMethod);
garnishVisibility.SetCardDescriptionText(ParseListToString(recipe.Garnish));
var ingredientsString = "";
var quantitiesSting = "";
for (var j = 0; j < recipe.Ingredient.Count; j++)
{
ingredientsString += ParseListToString(recipe.Ingredient[j]);
quantitiesSting += recipe.IngredientQuantities[j];
if (j < recipe.Ingredient.Count - 1)
{
ingredientsString += "\n";
quantitiesSting += "\n";
}
}
ingredientVisibility.SetCardDescriptionText(ingredientsString, quantitiesSting);
}
private string ParseListToString(List<string> garnishes)
{
if (garnishes == null || garnishes.Count == 0) return string.Empty;
return garnishes.Count == 1 ? garnishes[0] : $"{garnishes[0]} or {garnishes[1]}";
}
private float GetIngredientUIHeightByAmount(int count)
{
return count switch
{
<= 2 => 145,
<= 3 => 180,
<= 4 => 200,
_ => 250
};
}
private void SetIngredientUIHeight(float y)
{
recipePanelScaler.anchoredPosition = new Vector2(0, -y / 2f);
recipePanelScaler.sizeDelta = new Vector2(recipePanelScaler.sizeDelta.x, y);
}
private Color SetColorAlpha(Color color, float alpha)
{
return new Color(color.r, color.g, color.b, alpha);
}
public void ShowAllTabs(bool shouldSHow, bool skipAnimation = false)
{
iconVisibility.UpdateCardVisibility(shouldSHow, skipAnimation);
glasswareVisibility.UpdateCardVisibility(shouldSHow, skipAnimation);
methodVisibility.UpdateCardVisibility(shouldSHow, skipAnimation);
garnishVisibility.UpdateCardVisibility(shouldSHow, skipAnimation);
ingredientVisibility.UpdateCardVisibility(shouldSHow, skipAnimation);
}
public void SetNoteVisiblity(bool show)
{
if (DOTween.IsTweening(noteInputField.transform)) DOTween.Kill(noteInputField.transform);
if (show)
{
sfxManager.PlaySfx(5);
noteInputField.transform.position = initialPosition.transform.position;
noteInputField.transform.DOMove(basePosition.transform.position, 0.7f)
.SetEase(Ease.OutExpo);
LoadNoteData();
noteInputField.gameObject.SetActive(true);
transparentBackground.SetActive(true);
return;
}
sfxManager.PlaySfx(4);
noteInputField.transform.DOMove(initialPosition.transform.position, 0.7f)
.SetEase(Ease.OutExpo)
.OnComplete(HideNote);
SaveNoteData();
}
private void HideNote()
{
noteInputField.gameObject.SetActive(false);
transparentBackground.SetActive(false);
}
private void SaveNoteData()
{
PlayerData.SetNote(CurrentRecipeIndex, noteInputField.text);
}
private void LoadNoteData()
{
noteInputField.text = PlayerData.GetNote(CurrentRecipeIndex);
}
private void WiggleCard(int index, MemorizedState memorizedState)
{
if (CurrentRecipeIndex != index) return;
if (memorizedState == MemorizedState.Yes)
panelAnimControl.PlayAnim(CardAnimationType.WiggleRight);
else
panelAnimControl.PlayAnim(CardAnimationType.WiggleLeft);
}
}
}