summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/UICommon/XUIComboBox.cs
blob: 8aedb1764f1ca8a76b95f6c306d6cf9f2b03ac29 (plain)
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
using System.Collections.Generic;
using UILib;
using UnityEngine;

public class XUIComboBox : XUIObject, IXUIComboBox
{
    Dictionary<int, string> items = new Dictionary<int, string>(); 
    Dictionary<int, GameObject> itemObjects = new Dictionary<int, GameObject>();

    public void ModuleInit()
    {
        itemtpl = transform.Find("Difficulty/DropList/ItemTpl").GetComponent<UISprite>();
        itemtpl.gameObject.SetActive(false);

        droplist = transform.Find("Difficulty/DropList");
        droplist.gameObject.SetActive(false);

        selecttext = transform.Find("Difficulty/SelectedText").GetComponent<UILabel>();

        Transform t = transform.Find("Difficulty/DropList/Close");

        if (t != null) close = t.GetComponent<UIPlayTween>();

        count = 0;
    }

    public void AddItem(string text, int value)
    {
        GameObject newItem = Instantiate(itemtpl.gameObject) as GameObject;
        newItem.SetActive(true);
        newItem.name = value.ToString();

        newItem.transform.parent = droplist;
        newItem.transform.localPosition = new Vector3(0, -count * itemtpl.height);
        newItem.transform.localScale = Vector3.one;

        count ++;

        UILabel lb = newItem.transform.Find("ItemText").GetComponent<UILabel>();
        lb.text = text;

        items.Add(value, text);
        itemObjects.Add(value, newItem);

        XUISprite sp = newItem.GetComponent<XUISprite>();
        sp.ID = (ulong)value;
        sp.RegisterSpriteClickEventHandler(OnItemSelect);
    }

    public GameObject GetItem(int value)
    {
        GameObject go = null;
        itemObjects.TryGetValue(value, out go);
        return go;
    }

    public void ClearItems()
    {
        foreach (KeyValuePair<int, GameObject> pair in itemObjects)
        {
            Destroy(pair.Value);
        }
        items.Clear();
        itemObjects.Clear();
        count = 0;
    }

    protected void OnItemSelect(IXUISprite sp)
    {
        if (_callback != null)
            _callback((int) sp.ID);

        if (close != null) close.Play(true);

        selecttext.text = items[(int) sp.ID];
    }

    public bool SelectItem(int value, bool withCallback)
    {
        string _text = null;
        if(items.TryGetValue(value, out _text))
        {
            selecttext.text = _text;
            if(withCallback && null != _callback)
            {
                _callback(value);
            }
            return true;
        }
        return false;
    }

    public void RegisterSpriteClickEventHandler(ComboboxClickEventHandler eventHandler)
    {
        _callback = eventHandler;
    }

    public void ResetState()
    {
        if(items.Count == 0)
        {
            selecttext.text = "";
        }

        droplist.gameObject.SetActive(false);

        foreach (KeyValuePair<int, string> pair in items)
        {
            selecttext.text = pair.Value;

            if (_callback != null) _callback(pair.Key);

            break;
        }
    }


    private UISprite itemtpl = null;
    private int count = 0;
    private Transform droplist = null;
    private UIPlayTween close = null;
    private UILabel selecttext = null;
    private ComboboxClickEventHandler _callback = null;
}