-
Notifications
You must be signed in to change notification settings - Fork 0
/
WrapContentImpl.cs
114 lines (83 loc) · 2.65 KB
/
WrapContentImpl.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
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class WrapVo
{
public int index;
}
public abstract class WrapItem : MonoBehaviour
{
public GameObject root;
public int index;
public abstract void SetItem(WrapVo vo);
}
public class WrapContentImpl<TItem> where TItem : WrapItem, new()
{
public static Action<WrapContentImpl<TItem>, int, int> Action_ShowItems;
public UIWrapContentEx m_List;
Dictionary<GameObject, TItem> m_Contents = new Dictionary<GameObject, TItem>();
Dictionary<int, GameObject> m_ContentIndexs = new Dictionary<int, GameObject>();
public void Init(int poolsCount, GameObject prefabs, UIWrapContentEx parent, Action<TItem> callInit = null, bool usePrefab = true)
{
for (int k = 0; k < poolsCount; ++k)
{
GameObject root = null;
TItem item = null;
if (k == 0 && usePrefab )
{
root = prefabs;
}
else
{
root = NGUITools.AddChild(parent.gameObject, prefabs);
}
item = root.GetComponent<TItem>();
item.root = root;
item.root.name = k.ToString();
//Callback for init item UIWidgets
if( callInit != null ) callInit(item);
m_Contents.Add(item.root, item);
}
parent.SortBasedOnScrollMovement();
parent.ResetContent(poolsCount);
parent.onInitializeItem = OnItems;
m_List = parent;
}
void OnItems(GameObject go, int realIndex, int rowIndex)
{
if (go == null)
return;
int iDeShowIndex = -1;
if (m_Contents.ContainsKey(go))
{
iDeShowIndex = m_Contents[go].index;
}
m_ContentIndexs[realIndex] = go;
m_Contents[go].index = realIndex;
if (Action_ShowItems != null)
{
Action_ShowItems(this, realIndex, iDeShowIndex);
}
}
public void ResetAwards(int iCount, bool rePos)
{
m_List.ResetContent(iCount, rePos);
}
public TItem GetItem(int index)
{
if (!m_ContentIndexs.ContainsKey(index))
return null;
if (!m_Contents.ContainsKey(m_ContentIndexs[index]))
return null;
TItem item = m_Contents[m_ContentIndexs[index]];
return item;
}
public void SetItem(int index, WrapVo vo)
{
var item = GetItem(index);
if( item == null )
return;
item.SetItem(vo);
}
}