summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/LuaEngine/Core/LuaUIManager.cs
blob: b5633bcd65ae0cda0674ed08467eb437274a955c (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
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
using UnityEngine;
using System.Collections.Generic;
using XUtliPoolLib;

public struct LuaNode
{
    public uint id;
    public string name;
    public GameObject go;
    public LuaDlg dlg;
    public List<string> childs;
};



public class LuaUIManager : ILuaUIManager
{
    private Dictionary<uint, LuaNode> m_stask = new Dictionary<uint, LuaNode>();

    private static LuaUIManager _single;
    public static LuaUIManager Instance
    {
        get
        {
            if (_single == null) _single = new LuaUIManager();
            return _single;
        }
    }


    public bool IsUIShowed()
    {
        bool show = false;
        foreach (var item in m_stask)
        {
            if (item.Value.go != null && item.Value.go.activeInHierarchy)
            {
                show = true;
                break;
            }
        }
        return show;
    }


    public bool Load(string name)
    {
        uint id = XCommon.singleton.XHash(name);
        if (!Find(id))
        {
            GameObject root = UICamera.mainCamera.gameObject;
            GameObject go = XResourceLoaderMgr.singleton.CreateFromPrefab(name) as GameObject;
            go.transform.parent = UICamera.mainCamera.transform;
            go.transform.localPosition = Vector3.zero;
            go.transform.localRotation = Quaternion.identity;
            go.transform.localScale = Vector3.one;
            go.layer = root.layer;
            go.name = name.Substring(name.LastIndexOf('/') + 1);
            LuaNode node = AttachLuaDlg(go, name, id);
            if (!m_stask.ContainsKey(id))
            {
                m_stask.Add(id, node);
                node.dlg.OnShow();
            }
            return true;
        }
        else
        {
            m_stask[id].go.SetActive(true);
            m_stask[id].dlg.OnShow();
        }
        return false;
    }

    private LuaNode AttachLuaDlg(GameObject go,string name,uint id)
    {
        LuaNode node = new LuaNode();
        LuaDlg luadlg = go.AddComponent<LuaDlg>();
        node.dlg = luadlg;
        node.go = go;
        node.name = name;
        node.id = id;
        return node;
    }

    private GameObject SetupChild(Transform parent, string child)
    {
        uint id = XCommon.singleton.XHash(child);
        bool exist = Find(id);
        GameObject go = exist ? m_stask[id].go : XResourceLoaderMgr.singleton.CreateFromPrefab(child) as GameObject;
        go.transform.parent = parent;
        go.transform.localPosition = Vector3.zero;
        go.transform.localRotation = Quaternion.identity;
        go.transform.localScale = Vector3.one;
        if (!exist)
        {
            go.name = child.Substring(child.LastIndexOf('/') + 1);
            LuaNode node = AttachLuaDlg(go, child, id);
            if (!m_stask.ContainsKey(id))
            {
                m_stask.Add(id, node);
                node.dlg.OnShow();
            }
        }
        else
        {
            m_stask[id].go.SetActive(true);
            m_stask[id].dlg.OnShow();
        }
        return go;
    }


    public GameObject AttachHandler(string root, string child)
    {
        uint root_id = XCommon.singleton.XHash(root);
        if (m_stask.ContainsKey(root_id))
        {
            var childs = m_stask[root_id].childs;
            if (childs == null) childs = new List<string>();
            if (!childs.Contains(child)) childs.Add(child);
            GameObject go = m_stask[root_id].go;
            if (go != null)
            {
                Transform t = go.transform.Find("Bg/Handler");
                if (t != null)
                {
                    return SetupChild(t, child);
                }
            }
            else
            {
                XDebug.singleton.AddErrorLog("cache task go is nil");
            }
        }
        else
        {
            XDebug.singleton.AddErrorLog("There is no such root is stack ", root, " child: ", child);
        }
        return null;
    }

    public void AttachHandlers(string root, params string[] childs)
    {
        for (int i = 0; i < childs.Length; i++)
        {
            AttachHandler(root, childs[i]);
        }
    }


    public void DestroyChilds(string root)
    {
        uint root_id = XCommon.singleton.XHash(root);
        DestroyChilds(root_id);
    }


    private void DestroyChilds(uint root)
    {
        if (m_stask.ContainsKey(root))
        {
            var childs = m_stask[root].childs;
            if (childs != null)
            {
                for (int i = 0; i < childs.Count; i++)
                {
                    if (!string.IsNullOrEmpty(childs[i])) Destroy(childs[i]);
                }
            }
        }
    }

    /// <summary>
    /// 遍历所有 效率较低
    /// </summary>
    public void DetchHandler(string child)
    {
        foreach (var item in m_stask)
        {
            if (item.Value.childs != null)
            {
                item.Value.childs.RemoveAll(x => x == child);
            }
        }
    }

    public void DetchHandler(string root, string child)
    {
        uint root_id = XCommon.singleton.XHash(root);
        if (m_stask.ContainsKey(root_id))
        {
            var childs = m_stask[root_id].childs;
            if (childs != null)
            {
                childs.RemoveAll(x => x == child);
            }
        }
    }


    public bool Hide(string name)
    {
        uint id = XCommon.singleton.XHash(name);
        return IDHide(id);
    }


    public GameObject GetDlgObj(string name)
    {
        uint code = XCommon.singleton.XHash(name);
        if (m_stask.ContainsKey(code))
        {
            return m_stask[code].go;
        }
        return null;
    }

    public bool IDHide(uint id)
    {
        if (m_stask.Count > 0 && Find(id))
        {
            LuaNode node = m_stask[id];
            if (node.go != null)
                node.go.SetActive(false);
            node.dlg.OnHide();
            return true;
        }
        return true;
    }


    public bool Destroy(string name)
    {
        uint id = XCommon.singleton.XHash(name);
        return IDDestroy(id);
    }

    public bool IDDestroy(uint id)
    {
        if (m_stask.Count > 0 && Find(id))
        {
            LuaNode node = m_stask[id];
            //先删子节点 再删自身
            DestroyChilds(node.name);
            MonoBehaviour.Destroy(node.go);
            if (m_stask.ContainsKey(id)) m_stask.Remove(id);
            return true;
        }
        return false;
    }

    private void DestroyWithoutChild(uint id)
    {
        if (m_stask.Count > 0 && Find(id))
        {
            LuaNode node = m_stask[id];
            MonoBehaviour.Destroy(node.go);
        }
    }

    public void Clear()
    {
        List<uint> list = new List<uint>(m_stask.Keys);
        for (int i = 0; i < list.Count; i++)
        {
            DestroyWithoutChild(list[i]);
        }
        m_stask.Clear();
    }



    private bool Find(uint id)
    {
        if (m_stask.ContainsKey(id))
        {
            if (m_stask[id].go != null)
            {
                return true;
            }
            else
            {
                XDebug.singleton.AddGreenLog("remove id: "+id);
                m_stask.Remove(id);
                return false;
            }
        }
        return false;
    }


}