-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGDropDown.cs
More file actions
449 lines (397 loc) · 13.4 KB
/
GDropDown.cs
File metadata and controls
449 lines (397 loc) · 13.4 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* Unity Module By Garson(https://github.com/garsonlab)
* -------------------------------------------------------------------
* FileName: GDropDown
* Date : 2018/08/11
* Version : v1.0
* Describe: 下拉列表, 支持无限循环滚动列表
*/
using System;
using System.Collections.Generic;
using UnityEngine.Events;
namespace UnityEngine.UI
{
[AddComponentMenu("UI/GDropdown", 100)]
[RequireComponent(typeof (RectTransform))]
[DisallowMultipleComponent]
public class GDropDown : MonoBehaviour
{
#region Template
[SerializeField]
private Toggle m_CaptionToggle;
/// <summary>
/// Button of Whole Component
/// </summary>
public Toggle CaptionToggle { get { return m_CaptionToggle; } set { SetCaptionButton(value);} }
[Tooltip("Display Text of Selected Item")]
[SerializeField]
private Text m_CaptionText;
/// <summary>
/// Display Text of Selected Item
/// </summary>
public Text CaptionText { get { return m_CaptionText; } set { m_CaptionText = value; } }
[Tooltip("Display Image of Selected Item")]
[SerializeField]
private Image m_CaptionImage;
/// <summary>
/// Display Image of Selected Item
/// </summary>
public Image CaptionImage { get { return m_CaptionImage; } set { m_CaptionImage = value; } }
[Tooltip("Flag is Open List")]
[SerializeField]
private Transform m_Flag;
private float m_defaultAngle;
#endregion
[Space]
#region Other
[SerializeField]
private ScrollView m_ScrollView;
[SerializeField]
private int m_SelectIndex;
/// <summary>
/// Current Select Index
/// </summary>
public int selectIndex { get { return m_SelectIndex; } set { SetSelectIndex(value); }}
/// <summary>
/// Current Select Value
/// </summary>
public string selectValue
{
get { return m_DropData.Count > m_SelectIndex ? m_DropData[m_SelectIndex].text : ""; }
set
{
int count = m_DropData.Count;
for (int i = 0; i < count; i++)
{
if (m_DropData[i].text == value)
{
SetSelectIndex(i);
break;
}
}
}
}
/// <summary>
/// Drop Down Value Changed, Used to Lua
/// </summary>
public Action<int> OnValueChanged;
[SerializeField]
List<GDropData> m_DropData = new List<GDropData>();
[Serializable]
public class GDropDownEvent : UnityEvent<int> { }
public GDropDownEvent onValueChanged = new GDropDownEvent();
private Dictionary<Transform, GItem> m_Items = new Dictionary<Transform, GItem>();
private RectTransform m_PointerMask;
private Transform m_Canvas;
#endregion
void Awake()
{
if (m_Flag)
m_defaultAngle = m_Flag.localEulerAngles.z;
if (m_ScrollView)
m_ScrollView.onItemRender.AddListener(OnItemRender);
SetCaptionButton(m_CaptionToggle);
m_CaptionToggle.isOn = false;
SetSelectIndex(m_SelectIndex);
CloseMask();
}
public void AddOptions(GDropData[] options)
{
for (int i = 0; i < options.Length; i++)
this.m_DropData.Add(options[i]);
if (m_CaptionToggle.isOn)
OpenMask();
}
public void AddOptions(Sprite[] options)
{
for (int i = 0; i < options.Length; i++)
this.m_DropData.Add(new GDropData(options[i]));
if (m_CaptionToggle.isOn)
OpenMask();
}
public void AddOptions(string[] options)
{
for (int i = 0; i < options.Length; i++)
this.m_DropData.Add(new GDropData(options[i]));
if (m_CaptionToggle.isOn)
OpenMask();
}
public void RemoveAt(int index)
{
if(index < 0 || index >= m_DropData.Count)
return;
m_DropData.RemoveAt(index);
if (index == m_SelectIndex)
{
SetSelectIndex(index - 1);
}
}
public void RemoveValue(string value)
{
int index = -1;
int count = m_DropData.Count;
for (int i = 0; i < count; i++)
{
if (m_DropData[i].text == value)
{
index = i;
break;
}
}
if (index >= 0)
{
RemoveAt(index);
}
}
public void ClearOptions()
{
this.m_DropData.Clear();
if (m_CaptionToggle.isOn)
OpenMask();
}
/// <summary>
/// Refresh Display View
/// </summary>
private void RefreshShowValue()
{
if (m_DropData.Count > m_SelectIndex)
{
GDropData data = m_DropData[m_SelectIndex];
if (m_CaptionText)
m_CaptionText.text = data.text;
if (m_CaptionImage)
m_CaptionImage.sprite = data.image;
}
else
{
if (m_CaptionText)
m_CaptionText.text = "";
if (m_CaptionImage)
m_CaptionImage.sprite = null;
}
}
/// <summary>
/// Render Item in List
/// </summary>
/// <param name="index"></param>
/// <param name="child"></param>
private void OnItemRender(int index, Transform child)
{
GItem item;
if (!m_Items.TryGetValue(child, out item))
item = new GItem(this, child);
if (m_DropData.Count > index)
{
item.Reset(m_DropData[index].text, m_DropData[index].image, index == m_SelectIndex);
}
}
/// <summary>
/// Set Cur Select Index When Click
/// </summary>
/// <param name="p"></param>
private void SetSelectIndex(int p)
{
p = Mathf.Clamp(p, 0, m_DropData.Count);
m_SelectIndex = p;
RefreshShowValue();
onValueChanged.Invoke(p);
if (OnValueChanged != null)
OnValueChanged(p);
m_CaptionToggle.isOn = false;
}
/// <summary>
/// Open Mask to Poniters Out of List
/// </summary>
private void OpenMask()
{
if (m_PointerMask == null) //Create Mask
{
GameObject o = new GameObject("Pointer Mask");
o.transform.SetParent(transform);
Image mask = o.AddComponent<Image>();
mask.color = new Color(1, 1, 1, 0);
m_PointerMask = o.transform as RectTransform;
m_PointerMask.sizeDelta = new Vector2(Screen.width, Screen.height);
Button btnMask = o.AddComponent<Button>();
btnMask.onClick.AddListener(() =>
{
m_CaptionToggle.isOn = false;
});
}
if (m_Canvas == null) //Find Canvas
{
Canvas canvas = GameObject.FindObjectOfType<Canvas>();
m_Canvas = canvas.transform;
}
m_PointerMask.gameObject.SetActive(true);
m_PointerMask.SetParent(m_Canvas);
m_PointerMask.localPosition = Vector3.zero;
if (m_ScrollView != null)
{
m_ScrollView.transform.SetParent(m_Canvas);
m_ScrollView.gameObject.SetActive(true);
m_ScrollView.numItems = (uint)m_DropData.Count;
}
if (m_Flag)
m_Flag.localEulerAngles = new Vector3(0, 0, m_defaultAngle+180);
}
/// <summary>
/// Close Mask to Other Pointers
/// </summary>
private void CloseMask()
{
if (m_PointerMask != null)
{
m_PointerMask.transform.SetParent(transform);
m_PointerMask.gameObject.SetActive(false);
}
if (m_ScrollView != null)
{
m_ScrollView.transform.SetParent(transform);
m_ScrollView.gameObject.SetActive(false);
}
if (m_Flag)
m_Flag.localEulerAngles = new Vector3(0, 0, m_defaultAngle);
}
/// <summary>
/// Set Outter Button of Whole Component
/// </summary>
/// <param name="btn"></param>
private void SetCaptionButton(Toggle btn)
{
if (m_CaptionToggle != null)
m_CaptionToggle.onValueChanged.RemoveListener(OnCaptionButtonClicked);
m_CaptionToggle = btn;
if (m_CaptionToggle != null)
m_CaptionToggle.onValueChanged.AddListener(OnCaptionButtonClicked);
}
/// <summary>
/// On Caption Button Clicked
/// </summary>
private void OnCaptionButtonClicked(bool active)
{
if (active)
OpenMask();
else
CloseMask();
}
/// <summary>
/// Get Or Add Component on O
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="o"></param>
/// <returns></returns>
T GetOrAddComponent<T>(GameObject o) where T : Component
{
T com = o.GetComponent<T>();
if (com == null)
com = o.AddComponent<T>();
return com;
}
/// <summary>
/// Release All
/// </summary>
protected void OnDestroy()
{
//If Release on Drop State, Delete Mask
//if (m_PointerMask != null)
// m_PointerMask.SetParent(transform);
CloseMask();
}
/// <summary>
/// Renderer Item in Endless List
/// </summary>
protected internal class GItem
{
GDropDown dropDown;
Transform item;
Button btn;
Text text;
Image image;
GameObject selected;
bool activeSelf;
public string m_Text { get { return text.text; } set { text.text = value; } }
public Sprite m_Image { get { return image.sprite; } set { image.sprite = value; } }
public GItem(GDropDown parent, Transform item)
{
this.dropDown = parent;
this.item = item;
activeSelf = false;
Transform t_trans = item.Find("title");
if (t_trans)
{
text = t_trans.gameObject.GetComponent<Text>();
}
Transform t_image = item.Find("icon");
if (t_image)
{
image = t_image.gameObject.GetComponent<Image>();
}
Transform t_selected = item.Find("selected");
if (t_selected)
{
selected = t_selected.gameObject;
}
btn = item.GetComponent<Button>();
if (btn == null)
{
Transform t_btn = item.Find("btn");
if (t_btn != null)
btn = dropDown.GetOrAddComponent<Button>(t_btn.gameObject);
else
btn = dropDown.GetOrAddComponent<Button>(item.gameObject);
}
btn.onClick.AddListener(OnBtnItemClicked);
}
private void OnBtnItemClicked()
{
if (!activeSelf)
{
SetActive(true);
dropDown.SetSelectIndex(int.Parse(item.name));
}
}
internal void SetActive(bool active)
{
this.activeSelf = active;
if (selected != null)
selected.SetActive(active);
}
internal void Reset(string txt, Sprite sprite, bool active)
{
if (text != null)
m_Text = txt;
if (image != null)
m_Image = sprite;
SetActive(active);
}
}
/// <summary>
/// Cache Data
/// </summary>
[Serializable]
public class GDropData
{
[SerializeField]
private string m_Text;
[SerializeField]
private Sprite m_Image;
public string text { get { return m_Text; } set { m_Text = value; } }
public Sprite image { get { return m_Image; } set { m_Image = value; } }
public GDropData(){}
public GDropData(string text)
{
this.text = text;
}
public GDropData(Sprite image)
{
this.image = image;
}
public GDropData(string text, Sprite image)
{
this.text = text;
this.image = image;
}
}
}
}