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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
|
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2013 Tasharen Entertainment
//----------------------------------------------
using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// Popup list can be used to display pop-up menus and drop-down lists.
/// </summary>
[ExecuteInEditMode]
[AddComponentMenu("NGUI/Interaction/Popup List")]
public class UIPopupList : UIWidgetContainer
{
/// <summary>
/// Current popup list. Only available during the OnSelectionChange event callback.
/// </summary>
static public UIPopupList current;
const float animSpeed = 0.15f;
public enum Position
{
Auto,
Above,
Below,
}
/// <summary>
/// Atlas used by the sprites.
/// </summary>
public UIAtlas atlas;
/// <summary>
/// Font used by the labels.
/// </summary>
public UIFont bitmapFont;
/// <summary>
/// True type font used by the labels. Alternative to specifying a bitmap font ('font').
/// </summary>
public Font trueTypeFont;
/// <summary>
/// Font used by the popup list. Conveniently wraps both dynamic and bitmap fonts into one property.
/// </summary>
public Object ambigiousFont
{
get
{
if (trueTypeFont != null) return trueTypeFont;
if (bitmapFont != null) return bitmapFont;
return font;
}
set
{
if (value is Font)
{
trueTypeFont = value as Font;
bitmapFont = null;
font = null;
}
else if (value is UIFont)
{
bitmapFont = value as UIFont;
trueTypeFont = null;
font = null;
}
}
}
/// <summary>
/// Size of the font to use for the popup list's labels.
/// </summary>
public int fontSize = 16;
/// <summary>
/// Font style used by the dynamic font.
/// </summary>
public FontStyle fontStyle = FontStyle.Normal;
/// <summary>
/// Name of the sprite used to create the popup's background.
/// </summary>
public string backgroundSprite;
/// <summary>
/// Name of the sprite used to highlight items.
/// </summary>
public string highlightSprite;
/// <summary>
/// Popup list's display style.
/// </summary>
public Position position = Position.Auto;
/// <summary>
/// New line-delimited list of items.
/// </summary>
public List<string> items = new List<string>();
/// <summary>
/// Amount of padding added to labels.
/// </summary>
public Vector2 padding = new Vector3(4f, 4f);
/// <summary>
/// Color tint applied to labels inside the list.
/// </summary>
public Color textColor = Color.white;
/// <summary>
/// Color tint applied to the background.
/// </summary>
public Color backgroundColor = Color.white;
/// <summary>
/// Color tint applied to the highlighter.
/// </summary>
public Color highlightColor = new Color(225f / 255f, 200f / 255f, 150f / 255f, 1f);
/// <summary>
/// Whether the popup list is animated or not. Disable for better performance.
/// </summary>
public bool isAnimated = true;
/// <summary>
/// Whether the popup list's values will be localized.
/// </summary>
public bool isLocalized = false;
/// <summary>
/// Callbacks triggered when the popup list gets a new item selection.
/// </summary>
public List<EventDelegate> onChange = new List<EventDelegate>();
// Currently selected item
[HideInInspector][SerializeField] string mSelectedItem;
UIPanel mPanel;
GameObject mChild;
UISprite mBackground;
UISprite mHighlight;
UILabel mHighlightedLabel = null;
List<UILabel> mLabelList = new List<UILabel>();
float mBgBorder = 0f;
// Deprecated functionality
[HideInInspector][SerializeField] GameObject eventReceiver;
[HideInInspector][SerializeField] string functionName = "OnSelectionChange";
[HideInInspector][SerializeField] float textScale = 0f;
[HideInInspector][SerializeField] UIFont font; // Use 'bitmapFont' instead
// This functionality is no longer needed as the same can be achieved by choosing a
// OnValueChange notification targeting a label's SetCurrentSelection function.
// If your code was list.textLabel = myLabel, change it to:
// EventDelegate.Add(list.onChange, lbl.SetCurrentSelection);
[HideInInspector][SerializeField] UILabel textLabel;
public delegate void LegacyEvent (string val);
LegacyEvent mLegacyEvent;
[System.Obsolete("Use EventDelegate.Add(popup.onChange, YourCallback) instead, and UIPopupList.current.value to determine the state")]
public LegacyEvent onSelectionChange { get { return mLegacyEvent; } set { mLegacyEvent = value; } }
/// <summary>
/// Whether the popup list is currently open.
/// </summary>
public bool isOpen { get { return mChild != null; } }
/// <summary>
/// Current selection.
/// </summary>
public string value
{
get
{
return mSelectedItem;
}
set
{
mSelectedItem = value;
if (mSelectedItem == null) return;
#if UNITY_EDITOR
if (!Application.isPlaying) return;
#endif
if (mSelectedItem != null)
TriggerCallbacks();
}
}
[System.Obsolete("Use 'value' instead")]
public string selection { get { return value; } set { this.value = value; } }
/// <summary>
/// Whether the popup list will be handling keyboard, joystick and controller events.
/// </summary>
bool handleEvents
{
get
{
UIButtonKeys keys = GetComponent<UIButtonKeys>();
return (keys == null || !keys.enabled);
}
set
{
UIButtonKeys keys = GetComponent<UIButtonKeys>();
if (keys != null) keys.enabled = !value;
}
}
/// <summary>
/// Whether the popup list is actually usable.
/// </summary>
bool isValid { get { return bitmapFont != null || trueTypeFont != null; } }
/// <summary>
/// Active font size.
/// </summary>
int activeFontSize { get { return (trueTypeFont != null || bitmapFont == null) ? fontSize : bitmapFont.defaultSize; } }
/// <summary>
/// Font scale applied to the popup list's text.
/// </summary>
float activeFontScale { get { return (trueTypeFont != null || bitmapFont == null) ? 1f : (float)fontSize / bitmapFont.defaultSize; } }
/// <summary>
/// Trigger all event notification callbacks.
/// </summary>
protected void TriggerCallbacks ()
{
current = this;
// Legacy functionality
if (mLegacyEvent != null) mLegacyEvent(mSelectedItem);
if (EventDelegate.IsValid(onChange))
{
EventDelegate.Execute(onChange);
}
else if (eventReceiver != null && !string.IsNullOrEmpty(functionName))
{
// Legacy functionality support (for backwards compatibility)
eventReceiver.SendMessage(functionName, mSelectedItem, SendMessageOptions.DontRequireReceiver);
}
current = null;
}
/// <summary>
/// Remove legacy functionality.
/// </summary>
void OnEnable ()
{
if (EventDelegate.IsValid(onChange))
{
eventReceiver = null;
functionName = null;
}
// 'font' is no longer used
if (font != null)
{
if (font.isDynamic)
{
trueTypeFont = font.dynamicFont;
fontStyle = font.dynamicFontStyle;
mUseDynamicFont = true;
}
else if (bitmapFont == null)
{
bitmapFont = font;
mUseDynamicFont = false;
}
font = null;
}
// 'textScale' is no longer used
if (textScale != 0f)
{
fontSize = (bitmapFont != null) ? Mathf.RoundToInt(bitmapFont.defaultSize * textScale) : 16;
textScale = 0f;
}
// Auto-upgrade to the true type font
if (trueTypeFont == null && bitmapFont != null && bitmapFont.isDynamic)
{
trueTypeFont = bitmapFont.dynamicFont;
bitmapFont = null;
}
}
bool mUseDynamicFont = false;
void OnValidate ()
{
Font ttf = trueTypeFont;
UIFont fnt = bitmapFont;
bitmapFont = null;
trueTypeFont = null;
if (ttf != null && (fnt == null || !mUseDynamicFont))
{
bitmapFont = null;
trueTypeFont = ttf;
mUseDynamicFont = true;
}
else if (fnt != null)
{
// Auto-upgrade from 3.0.2 and earlier
if (fnt.isDynamic)
{
trueTypeFont = fnt.dynamicFont;
fontStyle = fnt.dynamicFontStyle;
fontSize = fnt.defaultSize;
mUseDynamicFont = true;
}
else
{
bitmapFont = fnt;
mUseDynamicFont = false;
}
}
else
{
trueTypeFont = ttf;
mUseDynamicFont = true;
}
}
/// <summary>
/// Send out the selection message on start.
/// </summary>
void Start ()
{
// Auto-upgrade legacy functionality
if (textLabel != null)
{
EventDelegate.Add(onChange, textLabel.SetCurrentSelection);
textLabel = null;
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
}
if (Application.isPlaying)
{
// Automatically choose the first item
if (string.IsNullOrEmpty(mSelectedItem))
{
if (items.Count > 0) value = items[0];
}
else
{
string s = mSelectedItem;
mSelectedItem = null;
value = s;
}
}
}
/// <summary>
/// Localize the text label.
/// </summary>
void OnLocalize (Localization loc) { if (isLocalized) TriggerCallbacks(); }
/// <summary>
/// Visibly highlight the specified transform by moving the highlight sprite to be over it.
/// </summary>
void Highlight (UILabel lbl, bool instant)
{
if (mHighlight != null)
{
// Don't allow highlighting while the label is animating to its intended position
TweenPosition tp = lbl.GetComponent<TweenPosition>();
if (tp != null && tp.enabled) return;
mHighlightedLabel = lbl;
UISpriteData sp = mHighlight.GetAtlasSprite();
if (sp == null) return;
float scaleFactor = atlas.pixelSize;
float offsetX = sp.borderLeft * scaleFactor;
float offsetY = sp.borderTop * scaleFactor;
Vector3 pos = lbl.cachedTransform.localPosition + new Vector3(-offsetX, offsetY, 1f);
if (instant || !isAnimated)
{
mHighlight.cachedTransform.localPosition = pos;
}
else
{
TweenPosition.Begin(mHighlight.gameObject, 0.1f, pos).method = UITweener.Method.EaseOut;
}
}
}
/// <summary>
/// Event function triggered when the mouse hovers over an item.
/// </summary>
void OnItemHover (GameObject go, bool isOver)
{
if (isOver)
{
UILabel lbl = go.GetComponent<UILabel>();
Highlight(lbl, false);
}
}
/// <summary>
/// Select the specified label.
/// </summary>
void Select (UILabel lbl, bool instant)
{
Highlight(lbl, instant);
UIEventListener listener = lbl.gameObject.GetComponent<UIEventListener>();
value = listener.parameter as string;
UIPlaySound[] sounds = GetComponents<UIPlaySound>();
for (int i = 0, imax = sounds.Length; i < imax; ++i)
{
UIPlaySound snd = sounds[i];
if (snd.trigger == UIPlaySound.Trigger.OnClick)
{
NGUITools.PlaySound(snd.audioClip, snd.volume, 1f);
}
}
}
/// <summary>
/// Event function triggered when the drop-down list item gets clicked on.
/// </summary>
void OnItemPress (GameObject go, bool isPressed) { if (isPressed) Select(go.GetComponent<UILabel>(), true); }
/// <summary>
/// React to key-based input.
/// </summary>
void OnKey (KeyCode key)
{
if (enabled && NGUITools.GetActive(gameObject) && handleEvents)
{
int index = mLabelList.IndexOf(mHighlightedLabel);
if (key == KeyCode.UpArrow)
{
if (index > 0)
{
Select(mLabelList[--index], false);
}
}
else if (key == KeyCode.DownArrow)
{
if (index + 1 < mLabelList.Count)
{
Select(mLabelList[++index], false);
}
}
else if (key == KeyCode.Escape)
{
OnSelect(false);
}
}
}
/// <summary>
/// Get rid of the popup dialog when the selection gets lost.
/// </summary>
void OnSelect (bool isSelected)
{
if (!isSelected && mChild != null)
{
mLabelList.Clear();
handleEvents = false;
if (isAnimated)
{
UIWidget[] widgets = mChild.GetComponentsInChildren<UIWidget>();
for (int i = 0, imax = widgets.Length; i < imax; ++i)
{
UIWidget w = widgets[i];
Color c = w.color;
c.a = 0f;
TweenColor.Begin(w.gameObject, animSpeed, c).method = UITweener.Method.EaseOut;
}
Collider[] cols = mChild.GetComponentsInChildren<Collider>();
for (int i = 0, imax = cols.Length; i < imax; ++i) cols[i].enabled = false;
Destroy(mChild, animSpeed);
}
else
{
Destroy(mChild);
}
mBackground = null;
mHighlight = null;
mChild = null;
}
}
/// <summary>
/// Helper function that causes the widget to smoothly fade in.
/// </summary>
void AnimateColor (UIWidget widget)
{
Color c = widget.color;
widget.color = new Color(c.r, c.g, c.b, 0f);
TweenColor.Begin(widget.gameObject, animSpeed, c).method = UITweener.Method.EaseOut;
}
/// <summary>
/// Helper function that causes the widget to smoothly move into position.
/// </summary>
void AnimatePosition (UIWidget widget, bool placeAbove, float bottom)
{
Vector3 target = widget.cachedTransform.localPosition;
Vector3 start = placeAbove ? new Vector3(target.x, bottom, target.z) : new Vector3(target.x, 0f, target.z);
widget.cachedTransform.localPosition = start;
GameObject go = widget.gameObject;
TweenPosition.Begin(go, animSpeed, target).method = UITweener.Method.EaseOut;
}
/// <summary>
/// Helper function that causes the widget to smoothly grow until it reaches its original size.
/// </summary>
void AnimateScale (UIWidget widget, bool placeAbove, float bottom)
{
GameObject go = widget.gameObject;
Transform t = widget.cachedTransform;
float minHeight = activeFontSize * activeFontScale + mBgBorder * 2f;
t.localScale = new Vector3(1f, minHeight / widget.height, 1f);
TweenScale.Begin(go, animSpeed, Vector3.one).method = UITweener.Method.EaseOut;
if (placeAbove)
{
Vector3 pos = t.localPosition;
t.localPosition = new Vector3(pos.x, pos.y - widget.height + minHeight, pos.z);
TweenPosition.Begin(go, animSpeed, pos).method = UITweener.Method.EaseOut;
}
}
/// <summary>
/// Helper function used to animate widgets.
/// </summary>
void Animate (UIWidget widget, bool placeAbove, float bottom)
{
AnimateColor(widget);
AnimatePosition(widget, placeAbove, bottom);
}
/// <summary>
/// Display the drop-down list when the game object gets clicked on.
/// </summary>
void OnClick()
{
if (enabled && NGUITools.GetActive(gameObject) && mChild == null && atlas != null && isValid && items.Count > 0)
{
mLabelList.Clear();
// Disable the navigation script
handleEvents = true;
// Automatically locate the panel responsible for this object
if (mPanel == null) mPanel = UIPanel.Find(transform, true);
// Calculate the dimensions of the object triggering the popup list so we can position it below it
Transform myTrans = transform;
Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(myTrans.parent, myTrans);
// Create the root object for the list
mChild = new GameObject("Drop-down List");
mChild.layer = gameObject.layer;
Transform t = mChild.transform;
t.parent = myTrans.parent;
t.localPosition = bounds.min;
t.localRotation = Quaternion.identity;
t.localScale = Vector3.one;
// Add a sprite for the background
mBackground = NGUITools.AddSprite(mChild, atlas, backgroundSprite);
mBackground.pivot = UIWidget.Pivot.TopLeft;
mBackground.depth = NGUITools.CalculateNextDepth(mPanel.gameObject);
mBackground.color = backgroundColor;
// We need to know the size of the background sprite for padding purposes
Vector4 bgPadding = mBackground.border;
mBgBorder = bgPadding.y;
mBackground.cachedTransform.localPosition = new Vector3(0f, bgPadding.y, 0f);
// Add a sprite used for the selection
mHighlight = NGUITools.AddSprite(mChild, atlas, highlightSprite);
mHighlight.pivot = UIWidget.Pivot.TopLeft;
mHighlight.color = highlightColor;
UISpriteData hlsp = mHighlight.GetAtlasSprite();
if (hlsp == null) return;
float hlspHeight = hlsp.borderTop;
float pixelSize = (bitmapFont != null) ? bitmapFont.pixelSize : 1f;
float fontHeight = activeFontSize * pixelSize;
float dynScale = activeFontScale;
float labelHeight = fontHeight * dynScale;
float x = 0f, y = -padding.y;
int labelFontSize = (bitmapFont != null) ? bitmapFont.defaultSize : fontSize;
List<UILabel> labels = new List<UILabel>();
// Run through all items and create labels for each one
for (int i = 0, imax = items.Count; i < imax; ++i)
{
string s = items[i];
UILabel lbl = NGUITools.AddWidget<UILabel>(mChild);
lbl.pivot = UIWidget.Pivot.TopLeft;
lbl.bitmapFont = bitmapFont;
lbl.trueTypeFont = trueTypeFont;
lbl.fontSize = labelFontSize;
lbl.fontStyle = fontStyle;
lbl.text = (isLocalized && Localization.instance != null) ? Localization.instance.Get(s) : s;
lbl.color = textColor;
lbl.cachedTransform.localPosition = new Vector3(bgPadding.x + padding.x, y, -1f);
lbl.overflowMethod = UILabel.Overflow.ResizeFreely;
lbl.MakePixelPerfect();
if (dynScale != 1f) lbl.cachedTransform.localScale = Vector3.one * dynScale;
labels.Add(lbl);
y -= labelHeight;
y -= padding.y;
x = Mathf.Max(x, lbl.printedSize.x);
// Add an event listener
UIEventListener listener = UIEventListener.Get(lbl.gameObject);
listener.onHover = OnItemHover;
listener.onPress = OnItemPress;
listener.parameter = s;
// Move the selection here if this is the right label
if (mSelectedItem == s) Highlight(lbl, true);
// Add this label to the list
mLabelList.Add(lbl);
}
// The triggering widget's width should be the minimum allowed width
x = Mathf.Max(x, bounds.size.x * dynScale - (bgPadding.x + padding.x) * 2f);
float cx = x / dynScale;
Vector3 bcCenter = new Vector3(cx * 0.5f, -fontHeight * 0.5f, 0f);
Vector3 bcSize = new Vector3(cx, (labelHeight + padding.y) / dynScale, 1f);
// Run through all labels and add colliders
for (int i = 0, imax = labels.Count; i < imax; ++i)
{
UILabel lbl = labels[i];
BoxCollider bc = NGUITools.AddWidgetCollider(lbl.gameObject);
bcCenter.z = bc.center.z;
bc.center = bcCenter;
bc.size = bcSize;
}
x += (bgPadding.x + padding.x) * 2f;
y -= bgPadding.y;
// Scale the background sprite to envelop the entire set of items
mBackground.width = Mathf.RoundToInt(x);
mBackground.height = Mathf.RoundToInt(-y + bgPadding.y);
// Scale the highlight sprite to envelop a single item
float scaleFactor = 2f * atlas.pixelSize;
float w = x - (bgPadding.x + padding.x) * 2f + hlsp.borderLeft * scaleFactor;
float h = labelHeight + hlspHeight * scaleFactor;
mHighlight.width = Mathf.RoundToInt(w);
mHighlight.height = Mathf.RoundToInt(h);
bool placeAbove = (position == Position.Above);
if (position == Position.Auto)
{
UICamera cam = UICamera.FindCameraForLayer(gameObject.layer);
if (cam != null)
{
Vector3 viewPos = cam.cachedCamera.WorldToViewportPoint(myTrans.position);
placeAbove = (viewPos.y < 0.5f);
}
}
// If the list should be animated, let's animate it by expanding it
if (isAnimated)
{
float bottom = y + labelHeight;
Animate(mHighlight, placeAbove, bottom);
for (int i = 0, imax = labels.Count; i < imax; ++i) Animate(labels[i], placeAbove, bottom);
AnimateColor(mBackground);
AnimateScale(mBackground, placeAbove, bottom);
}
// If we need to place the popup list above the item, we need to reposition everything by the size of the list
if (placeAbove)
{
t.localPosition = new Vector3(bounds.min.x, bounds.max.y - y - bgPadding.y, bounds.min.z);
}
}
else OnSelect(false);
}
}
|