-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardZoneManager.cs
213 lines (184 loc) · 7.64 KB
/
CardZoneManager.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace CardEase
{
public abstract class CardZoneManager<T> : MonoBehaviour, IPointerEnterHandler where T : CardModel
{
[Header("--------------Prefabs----------------")]
[Tooltip("Card Group prefab to add new group in zone")]
[SerializeField] GameObject cardGroupPrefab;
[Header("--------------Data----------------")]
[Tooltip("minimum spacing needed between card groups")]
[SerializeField] float minSpacing = -99999f;
[Tooltip("maximum spacing allowed between card groups")]
[SerializeField] float maxSpacing = 99999f;
[Tooltip("Indicate if empty group should be removed")]
[SerializeField] bool shouldRemoveEmptyGroup = true;
[Tooltip("Label to differentiate card zone and cards in it")]
public string label = "CardZone";
[Header("--------------PRIVATE----------------")]
[Tooltip("layout group used to adjust spacing")]
HorizontalLayoutGroup horizontalLayoutGroup;
// --------------------------MONO methods------------------------
#region methods related to MonoBehaviour
protected virtual void Awake()
{
horizontalLayoutGroup = GetComponent<HorizontalLayoutGroup>();
}
#endregion
// --------------------------HELPER METHODS------------------------
#region it's mostly public and can be used by other classes as well
/// <summary>
/// Adds a new group to the card zone.
/// </summary>
/// <param name="cards">An optional list of cards to be added to the new group.</param>
/// <returns>A <see cref="CardGroupManager{T}"/> representing the newly added group.</returns>
/// <remarks>
/// This method creates a new empty group in the card zone. If a list of cards is provided, those cards will be added to the new group
/// </remarks>
public CardGroupManager<T> AddGroup(List<T> cards = null)
{
GameObject cardGroup = Instantiate(cardGroupPrefab, transform);
if (cards != null)
{
foreach (T card in cards)
{
cardGroup.GetComponent<CardGroupManager<T>>().AddCard(card);
}
}
return cardGroup.GetComponent<CardGroupManager<T>>();
}
/// <summary>
/// Adjusts the spacing between elements in the card zone.
/// </summary>
/// <remarks>
/// This method can arrange the elements(cardGroups) in it so it will cover the screen area properly
/// </remarks>
/// <returns>
/// void
/// </returns>
public void RefreshCardZone()
{
//checking if there is any group in the zone or not
int groupCount = transform.childCount;
if (groupCount <= 0)
{
return;
}
//adjust spacing based on group count and zone width
float desiredSpacing = GetComponent<RectTransform>().rect.width / groupCount * 0.1f;
desiredSpacing = Mathf.Clamp(desiredSpacing, minSpacing, maxSpacing);
horizontalLayoutGroup.spacing = desiredSpacing;
foreach (Transform child in transform)
{
if (shouldRemoveEmptyGroup)
{
if (child.childCount <= 0)
{
Destroy(child.gameObject);
continue;
}
}
CardGroupManager<T> cardGroup = child.GetComponent<CardGroupManager<T>>();
cardGroup.RefreshCardGroup();
}
}
/// <summary>
/// Get the list of total cards in the card zone
/// </summary>
/// <remarks>
/// This method will get the list of all cards in the card zone
/// </remarks>
/// <returns>
/// A list of <see cref="CardManager{T}"/> objects representing all the cards in the card zone.
/// </returns>
public virtual List<CardManager<T>> GetAllCards()
{
List<CardManager<T>> cards = new List<CardManager<T>>();
foreach (Transform groupTransform in transform)
{
foreach (Transform cardTransform in groupTransform)
{
cards.Add(cardTransform.gameObject.GetComponent<CardManager<T>>());
}
}
return cards;
}
/// <summary>
/// Get the list of total SELECTED cards in the card zone
/// </summary>
/// <remarks>
/// This method will get the list of all SELECTED cards in that perticular card zone
/// </remarks>
/// <returns>
/// A list of <see cref="CardManager{T}"/> objects representing all the cards in the card zone.
/// </returns>
public virtual List<CardManager<T>> GetAllSelectedCards()
{
List<CardManager<T>> selectedCards = new List<CardManager<T>>();
foreach (Transform groupTransform in transform)
{
foreach (Transform cardTransform in groupTransform)
{
CardManager<T> card = cardTransform.gameObject.GetComponent<CardManager<T>>();
if (card.isSelected)
{
selectedCards.Add(card);
}
}
}
return selectedCards;
}
/// <summary>
/// This method will make a new group of selected cards and return the group
/// </summary>
/// <remarks>
/// if no card is selected then it will return empty group
/// </remarks>
/// <returns>
/// A list of <see cref="CardGroupManager{T}"/> objects representing all the cards in the card zone.
/// </returns>
public CardGroupManager<T> GroupSelectedCards()
{
List<CardManager<T>> selectedCards = new List<CardManager<T>>();
foreach (CardManager<T> card in GetAllCards())
{
if (card.isSelected)
{
selectedCards.Add(card);
}
}
GameObject cardGroup = Instantiate(cardGroupPrefab, transform);
foreach (CardManager<T> card in selectedCards)
{
card.transform.SetParent(cardGroup.transform);
card.UpdateSelection(false);
}
RefreshCardZone();
return cardGroup.GetComponent<CardGroupManager<T>>();
}
#endregion
// ------------------------------------DRAG/DROP LOGIC--------------------------------------------------
#region all LOGIC for drag-drop on group
public void OnPointerEnter(PointerEventData eventData)
{
if (eventData.pointerDrag)
{
CardManager<T> cardManager = eventData.pointerDrag.GetComponent<CardManager<T>>();
if (cardManager && cardManager.label == this.label)
{
if (transform.childCount <= 0)
{
CardGroupManager<T> groupManager = AddGroup();
Vector2 sizeDelta = groupManager.transform.GetComponent<RectTransform>().sizeDelta;
sizeDelta.x = GetComponent<RectTransform>().sizeDelta.x;
groupManager.transform.GetComponent<RectTransform>().sizeDelta = sizeDelta;
}
}
}
}
#endregion
}
}