summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XMainClient/XBagWindow.cs
blob: 78a260fff99f0c958433e15876f91bdacad8518e (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
using System;
using System.Collections.Generic;
using UILib;
using UnityEngine;
using XUtliPoolLib;

namespace XMainClient
{
	internal class XBagWindow
	{
		public int COL_COUNT = 6;

		public int ROW_COUNT = 4;

		public GameObject PanelObject;

		private IXUIWrapContent m_WrapContent;

		private IXUIScrollView m_ScrollView;

		private XUIPool m_ItemPool = new XUIPool(XSingleton<XGameUI>.singleton.m_uiTool);

		public List<ulong> m_XItemIDList = new List<ulong>();

		public List<XItem> m_XItemList;

		private ItemUpdateHandler itemUpdateHandler = null;

		private GetItemHandler getItemHandler = null;

		public XBagWindow(GameObject PanelGo, ItemUpdateHandler updateHandler, GetItemHandler getHandler)
		{
			this.PanelObject = PanelGo;
			this.itemUpdateHandler = updateHandler;
			this.getItemHandler = getHandler;
		}

		public void Init()
		{
			Transform transform = this.PanelObject.transform.Find("Panel/WrapContent/ItemTpl");
			this.m_ScrollView = (this.PanelObject.transform.Find("Panel").GetComponent("XUIScrollView") as IXUIScrollView);
			this.m_WrapContent = (this.PanelObject.transform.Find("Panel/WrapContent").GetComponent("XUIWrapContent") as IXUIWrapContent);
			this.m_WrapContent.RegisterItemUpdateEventHandler(new WrapItemUpdateEventHandler(this.WrapContentItemUpdated));
			this.COL_COUNT = this.m_WrapContent.widthDimension;
			this.ROW_COUNT = this.m_WrapContent.heightDimensionMax;
		}

		private void WrapContentItemUpdated(Transform t, int index)
		{
			this.itemUpdateHandler(t, index);
		}

		public void ChangeData(ItemUpdateHandler updateHandler, GetItemHandler getHandler)
		{
			this.itemUpdateHandler = updateHandler;
			this.getItemHandler = getHandler;
		}

		public void OnShow()
		{
			this.UpdateBag();
			this.m_ScrollView.ResetPosition();
		}

		public void OnHide()
		{
			this.m_ItemPool.ReturnAll(true);
			this.m_XItemIDList.Clear();
		}

		protected void _RefreshBag()
		{
			int num = Math.Max(this.m_XItemList.Count, this.COL_COUNT * this.ROW_COUNT);
			this.m_WrapContent.SetContentCount(num, false);
		}

		public void UpdateBag()
		{
			this.GetItemData(this.getItemHandler());
			this.m_XItemIDList.Clear();
			for (int i = 0; i < this.m_XItemList.Count; i++)
			{
				bool flag = this.m_XItemList[i] != null;
				if (flag)
				{
					this.m_XItemIDList.Add(this.m_XItemList[i].uid);
				}
				else
				{
					this.m_XItemIDList.Add(0UL);
				}
			}
			this._RefreshBag();
		}

		private void GetItemData(List<XItem> lst)
		{
			bool flag = this.m_XItemList != null;
			if (flag)
			{
				this.m_XItemList.Clear();
			}
			else
			{
				this.m_XItemList = new List<XItem>();
			}
			for (int i = 0; i < lst.Count; i++)
			{
				this.m_XItemList.Add(lst[i]);
			}
		}

		public void RefreshWindow()
		{
			this.m_WrapContent.RefreshAllVisibleContents();
		}

		public void UpdateItem(XItem item)
		{
			for (int i = 0; i < this.m_XItemList.Count; i++)
			{
				bool flag = this.m_XItemIDList[i] == item.uid;
				if (flag)
				{
					this.m_XItemList[i] = item;
					break;
				}
			}
			this.RefreshWindow();
		}

		public void ReplaceItem(XItem from, XItem to)
		{
			for (int i = 0; i < this.m_XItemList.Count; i++)
			{
				bool flag = this.m_XItemIDList[i] == from.uid;
				if (flag)
				{
					this.m_XItemList[i] = to;
					this.m_XItemIDList[i] = to.uid;
					break;
				}
			}
			this.RefreshWindow();
		}

		public void ResetPosition()
		{
			this.m_ScrollView.ResetPosition();
		}

		public Transform FindChildByName(string name)
		{
			bool flag = this.m_WrapContent != null;
			Transform result;
			if (flag)
			{
				result = this.m_WrapContent.gameObject.transform.Find(name);
			}
			else
			{
				result = null;
			}
			return result;
		}
	}
}