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
|
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryGrid : MonoBehaviour
{
private class Element
{
public Vector2i m_pos;
public GameObject m_go;
public Image m_icon;
public Text m_amount;
public Text m_quality;
public Image m_equiped;
public Image m_queued;
public GameObject m_selected;
public Image m_noteleport;
public UITooltip m_tooltip;
public GuiBar m_durability;
public bool m_used;
}
public enum Modifier
{
Select,
Split,
Move
}
public Action<InventoryGrid, ItemDrop.ItemData, Vector2i, Modifier> m_onSelected;
public Action<InventoryGrid, ItemDrop.ItemData, Vector2i> m_onRightClick;
public GameObject m_elementPrefab;
public RectTransform m_gridRoot;
public Scrollbar m_scrollbar;
public UIGroupHandler m_uiGroup;
public float m_elementSpace = 10f;
private int m_width = 4;
private int m_height = 4;
private Vector2i m_selected = new Vector2i(0, 0);
private Inventory m_inventory;
private List<Element> m_elements = new List<Element>();
protected void Awake()
{
}
public void ResetView()
{
RectTransform rectTransform = base.transform as RectTransform;
if (m_gridRoot.rect.height > rectTransform.rect.height)
{
m_gridRoot.pivot = new Vector2(m_gridRoot.pivot.x, 1f);
}
else
{
m_gridRoot.pivot = new Vector2(m_gridRoot.pivot.x, 0.5f);
}
m_gridRoot.anchoredPosition = new Vector2(0f, 0f);
}
public void UpdateInventory(Inventory inventory, Player player, ItemDrop.ItemData dragItem)
{
m_inventory = inventory;
UpdateGamepad();
UpdateGui(player, dragItem);
}
private void UpdateGamepad()
{
if (!m_uiGroup.IsActive())
{
return;
}
if (ZInput.GetButtonDown("JoyDPadLeft") || ZInput.GetButtonDown("JoyLStickLeft"))
{
m_selected.x = Mathf.Max(0, m_selected.x - 1);
}
if (ZInput.GetButtonDown("JoyDPadRight") || ZInput.GetButtonDown("JoyLStickRight"))
{
m_selected.x = Mathf.Min(m_width - 1, m_selected.x + 1);
}
if (ZInput.GetButtonDown("JoyDPadUp") || ZInput.GetButtonDown("JoyLStickUp"))
{
m_selected.y = Mathf.Max(0, m_selected.y - 1);
}
if (ZInput.GetButtonDown("JoyDPadDown") || ZInput.GetButtonDown("JoyLStickDown"))
{
m_selected.y = Mathf.Min(m_width - 1, m_selected.y + 1);
}
if (ZInput.GetButtonDown("JoyButtonA"))
{
Modifier arg = Modifier.Select;
if (ZInput.GetButton("JoyLTrigger"))
{
arg = Modifier.Split;
}
if (ZInput.GetButton("JoyRTrigger"))
{
arg = Modifier.Move;
}
ItemDrop.ItemData gamepadSelectedItem = GetGamepadSelectedItem();
m_onSelected(this, gamepadSelectedItem, m_selected, arg);
}
if (ZInput.GetButtonDown("JoyButtonX"))
{
ItemDrop.ItemData gamepadSelectedItem2 = GetGamepadSelectedItem();
m_onRightClick(this, gamepadSelectedItem2, m_selected);
}
}
private void UpdateGui(Player player, ItemDrop.ItemData dragItem)
{
RectTransform rectTransform = base.transform as RectTransform;
int width = m_inventory.GetWidth();
int height = m_inventory.GetHeight();
if (m_selected.x >= width - 1)
{
m_selected.x = width - 1;
}
if (m_selected.y >= height - 1)
{
m_selected.y = height - 1;
}
if (m_width != width || m_height != height)
{
m_width = width;
m_height = height;
foreach (Element element3 in m_elements)
{
UnityEngine.Object.Destroy(element3.m_go);
}
m_elements.Clear();
Vector2 widgetSize = GetWidgetSize();
Vector2 vector = new Vector2(rectTransform.rect.width / 2f, 0f) - new Vector2(widgetSize.x, 0f) * 0.5f;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
Vector2 vector2 = new Vector3((float)j * m_elementSpace, (float)i * (0f - m_elementSpace));
GameObject gameObject = UnityEngine.Object.Instantiate(m_elementPrefab, m_gridRoot);
(gameObject.transform as RectTransform).anchoredPosition = vector + vector2;
UIInputHandler componentInChildren = gameObject.GetComponentInChildren<UIInputHandler>();
componentInChildren.m_onRightDown = (Action<UIInputHandler>)Delegate.Combine(componentInChildren.m_onRightDown, new Action<UIInputHandler>(OnRightClick));
componentInChildren.m_onLeftDown = (Action<UIInputHandler>)Delegate.Combine(componentInChildren.m_onLeftDown, new Action<UIInputHandler>(OnLeftClick));
Text component = gameObject.transform.Find("binding").GetComponent<Text>();
if ((bool)player && i == 0)
{
component.text = (j + 1).ToString();
}
else
{
component.enabled = false;
}
Element element = new Element();
element.m_pos = new Vector2i(j, i);
element.m_go = gameObject;
element.m_icon = gameObject.transform.Find("icon").GetComponent<Image>();
element.m_amount = gameObject.transform.Find("amount").GetComponent<Text>();
element.m_quality = gameObject.transform.Find("quality").GetComponent<Text>();
element.m_equiped = gameObject.transform.Find("equiped").GetComponent<Image>();
element.m_queued = gameObject.transform.Find("queued").GetComponent<Image>();
element.m_noteleport = gameObject.transform.Find("noteleport").GetComponent<Image>();
element.m_selected = gameObject.transform.Find("selected").gameObject;
element.m_tooltip = gameObject.GetComponent<UITooltip>();
element.m_durability = gameObject.transform.Find("durability").GetComponent<GuiBar>();
m_elements.Add(element);
}
}
}
foreach (Element element4 in m_elements)
{
element4.m_used = false;
}
bool flag = m_uiGroup.IsActive() && ZInput.IsGamepadActive();
foreach (ItemDrop.ItemData allItem in m_inventory.GetAllItems())
{
Element element2 = GetElement(allItem.m_gridPos.x, allItem.m_gridPos.y, width);
element2.m_used = true;
element2.m_icon.enabled = true;
element2.m_icon.sprite = allItem.GetIcon();
element2.m_icon.color = ((allItem == dragItem) ? Color.grey : Color.white);
element2.m_durability.gameObject.SetActive(allItem.m_shared.m_useDurability);
if (allItem.m_shared.m_useDurability)
{
if (allItem.m_durability <= 0f)
{
element2.m_durability.SetValue(1f);
element2.m_durability.SetColor((Mathf.Sin(Time.time * 10f) > 0f) ? Color.red : new Color(0f, 0f, 0f, 0f));
}
else
{
element2.m_durability.SetValue(allItem.GetDurabilityPercentage());
element2.m_durability.ResetColor();
}
}
element2.m_equiped.enabled = (bool)player && allItem.m_equiped;
element2.m_queued.enabled = (bool)player && player.IsItemQueued(allItem);
element2.m_noteleport.enabled = !allItem.m_shared.m_teleportable;
if (dragItem == null)
{
CreateItemTooltip(allItem, element2.m_tooltip);
}
element2.m_quality.enabled = allItem.m_shared.m_maxQuality > 1;
if (allItem.m_shared.m_maxQuality > 1)
{
element2.m_quality.text = allItem.m_quality.ToString();
}
element2.m_amount.enabled = allItem.m_shared.m_maxStackSize > 1;
if (allItem.m_shared.m_maxStackSize > 1)
{
element2.m_amount.text = allItem.m_stack + "/" + allItem.m_shared.m_maxStackSize;
}
}
foreach (Element element5 in m_elements)
{
element5.m_selected.SetActive(flag && element5.m_pos == m_selected);
if (!element5.m_used)
{
element5.m_durability.gameObject.SetActive(value: false);
element5.m_icon.enabled = false;
element5.m_amount.enabled = false;
element5.m_quality.enabled = false;
element5.m_equiped.enabled = false;
element5.m_queued.enabled = false;
element5.m_noteleport.enabled = false;
element5.m_tooltip.m_text = "";
element5.m_tooltip.m_topic = "";
}
}
float size = (float)height * m_elementSpace;
m_gridRoot.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size);
}
private void CreateItemTooltip(ItemDrop.ItemData item, UITooltip tooltip)
{
tooltip.Set(item.m_shared.m_name, item.GetTooltip());
}
public Vector2 GetWidgetSize()
{
return new Vector2((float)m_width * m_elementSpace, (float)m_height * m_elementSpace);
}
private void OnRightClick(UIInputHandler element)
{
GameObject go = element.gameObject;
Vector2i buttonPos = GetButtonPos(go);
ItemDrop.ItemData itemAt = m_inventory.GetItemAt(buttonPos.x, buttonPos.y);
if (m_onRightClick != null)
{
m_onRightClick(this, itemAt, buttonPos);
}
}
private void OnLeftClick(UIInputHandler clickHandler)
{
GameObject go = clickHandler.gameObject;
Vector2i buttonPos = GetButtonPos(go);
ItemDrop.ItemData itemAt = m_inventory.GetItemAt(buttonPos.x, buttonPos.y);
Modifier arg = Modifier.Select;
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
arg = Modifier.Split;
}
if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
{
arg = Modifier.Move;
}
if (m_onSelected != null)
{
m_onSelected(this, itemAt, buttonPos, arg);
}
}
private Element GetElement(int x, int y, int width)
{
int index = y * width + x;
return m_elements[index];
}
private Vector2i GetButtonPos(GameObject go)
{
for (int i = 0; i < m_elements.Count; i++)
{
if (m_elements[i].m_go == go)
{
int num = i / m_width;
return new Vector2i(i - num * m_width, num);
}
}
return new Vector2i(-1, -1);
}
public bool DropItem(Inventory fromInventory, ItemDrop.ItemData item, int amount, Vector2i pos)
{
ItemDrop.ItemData itemAt = m_inventory.GetItemAt(pos.x, pos.y);
if (itemAt == item)
{
return true;
}
if (itemAt != null && (itemAt.m_shared.m_name != item.m_shared.m_name || (item.m_shared.m_maxQuality > 1 && itemAt.m_quality != item.m_quality) || itemAt.m_shared.m_maxStackSize == 1) && item.m_stack == amount)
{
fromInventory.RemoveItem(item);
fromInventory.MoveItemToThis(m_inventory, itemAt, itemAt.m_stack, item.m_gridPos.x, item.m_gridPos.y);
m_inventory.MoveItemToThis(fromInventory, item, amount, pos.x, pos.y);
return true;
}
return m_inventory.MoveItemToThis(fromInventory, item, amount, pos.x, pos.y);
}
public ItemDrop.ItemData GetItem(Vector2i cursorPosition)
{
foreach (Element element in m_elements)
{
if (RectTransformUtility.RectangleContainsScreenPoint(element.m_go.transform as RectTransform, cursorPosition.ToVector2()))
{
Vector2i buttonPos = GetButtonPos(element.m_go);
return m_inventory.GetItemAt(buttonPos.x, buttonPos.y);
}
}
return null;
}
public Inventory GetInventory()
{
return m_inventory;
}
public void SetSelection(Vector2i pos)
{
m_selected = pos;
}
public ItemDrop.ItemData GetGamepadSelectedItem()
{
if (!m_uiGroup.IsActive())
{
return null;
}
return m_inventory.GetItemAt(m_selected.x, m_selected.y);
}
public RectTransform GetGamepadSelectedElement()
{
if (!m_uiGroup.IsActive())
{
return null;
}
if (m_selected.x < 0 || m_selected.x >= m_width || m_selected.y < 0 || m_selected.y >= m_height)
{
return null;
}
return GetElement(m_selected.x, m_selected.y, m_width).m_go.transform as RectTransform;
}
}
|