-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInventorySlot.cs
292 lines (223 loc) · 10.8 KB
/
InventorySlot.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
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
#region Header
/* ============================================
* Author : Strix
* Initial Creation Date : 2020-01-30
* Summary :
* Template : For Unity Editor V1
============================================ */
#endregion Header
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System.Linq;
using UIFramework.InventorySlotLogic;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UIFramework
{
/// <summary>
///
/// </summary>
public class InventorySlot : UIWidgetObjectBase, IPointerEnterHandler, IPointerClickHandler
{
/* const & readonly declaration */
/* enum & struct declaration */
public enum EInventorySlot_StateEvent
{
None,
OnFillSlot,
OnEmptySlot,
OnSwapSlot,
OnSelectedSlot,
}
public enum EInventorySlot_CommandEvent
{
None,
OnHover,
OnClick,
OnDragBegin,
OnDrag,
OnDragEnd,
}
public struct OnChangeSlotData_Msg
{
public InventorySlot pSlot { get; private set; }
public IInventoryData pData_Prev { get; private set; }
public IInventoryData pData_Current { get; private set; }
public bool bSlot_IsEmpty { get; private set; }
public OnChangeSlotData_Msg(InventorySlot pSlot, IInventoryData pData_Prev, IInventoryData pData_Current)
{
this.pSlot = pSlot; this.pData_Prev = pData_Prev; this.pData_Current = pData_Current;
bSlot_IsEmpty = pData_Current == null;
}
}
/* public - Field declaration */
public event System.Action<InventorySlot, PointerEventData> OnClickedSlot;
public event System.Action<InventorySlot, PointerEventData> OnHoverSlot;
public event System.Action<Inventory.OnSwapSlot_Msg> OnSwapSlot;
public delegate void delOnChangeSlotData(OnChangeSlotData_Msg sMsg);
public event delOnChangeSlotData OnChange_SlotData;
public event System.Action<bool> OnChange_IsSelected;
public bool bIsSelected { get; private set; }
public bool bIsClone { get; private set; }
public IInventoryData pData { get; private set; }
public Inventory pInventory { get; private set; }
[Header("디버깅 유무")]
public bool bIsDebug = false;
[Header("선택 유무")]
public bool bIsSelectAble = true;
public int iSlotIndex;
/* protected & private - Field declaration */
protected static List<RaycastResult> g_listHit = new List<RaycastResult>();
Dictionary<EInventorySlot_StateEvent, List<InventorySlot_StateLogic>> _mapSlotLogic_State = new Dictionary<EInventorySlot_StateEvent, List<InventorySlot_StateLogic>>();
Dictionary<EInventorySlot_StateEvent, List<InventorySlot_StateLogic>> _mapSlotLogic_State_Undo = new Dictionary<EInventorySlot_StateEvent, List<InventorySlot_StateLogic>>();
Dictionary<EInventorySlot_CommandEvent, List<InventorySlot_CommandLogic>> _mapSlotLogic_Command = new Dictionary<EInventorySlot_CommandEvent, List<InventorySlot_CommandLogic>>();
Dictionary<EInventorySlot_CommandEvent, List<InventorySlot_CommandLogic>> _mapSlotLogic_Command_Undo = new Dictionary<EInventorySlot_CommandEvent, List<InventorySlot_CommandLogic>>();
// ========================================================================== //
/* public - [Do~Something] Function */
public void DoInit(Inventory pInventory)
{
this.pInventory = pInventory;
Event_SetSelected(false);
}
public void DoUpdateIndex_IsSibling()
{
iSlotIndex = transform.GetSiblingIndex();
}
public void DoInit_SlotStateLogic(InventorySlot_StateLogic[] arrSlotStateLogic)
{
_mapSlotLogic_State = arrSlotStateLogic.
GroupBy(p => p.eEvent).
ToDictionary(p => p.Key, p => p.ToList());
_mapSlotLogic_State_Undo = arrSlotStateLogic.
Where(p => p.eEvent_Undo != EInventorySlot_StateEvent.None).
GroupBy(p => p.eEvent_Undo).
ToDictionary(p => p.Key, p => p.ToList());
OnChange_SlotData -= ExecuteLogic_StateEvent_OnChangeData;
OnChange_SlotData += ExecuteLogic_StateEvent_OnChangeData;
OnChange_IsSelected -= ExecuteLogic_StateEvent_OnSelected;
OnChange_IsSelected += ExecuteLogic_StateEvent_OnSelected;
OnSwapSlot -= ExecuteLogic_StateEvent_OnSwap;
OnSwapSlot += ExecuteLogic_StateEvent_OnSwap;
}
public virtual void DoInit_SlotCommandLogic(InventorySlot_CommandLogic[] arrSlotCommandLogic)
{
_mapSlotLogic_Command = arrSlotCommandLogic.
GroupBy(p => p.eEvent).
ToDictionary(p => p.Key, p => p.ToList());
_mapSlotLogic_Command_Undo = arrSlotCommandLogic.
Where(p => p.eEvent_Undo != EInventorySlot_CommandEvent.None).
GroupBy(p => p.eEvent_Undo).
ToDictionary(p => p.Key, p => p.ToList());
OnHoverSlot -= ExecuteLogic_CommandEvent_OnHover;
OnHoverSlot += ExecuteLogic_CommandEvent_OnHover;
OnClickedSlot -= ExecuteLogic_CommandEvent_OnClick;
OnClickedSlot += ExecuteLogic_CommandEvent_OnClick;
}
public void DoSwapSlot(InventorySlot pSlotOrigin)
{
var pMyData = this.pData;
DoSetData(pSlotOrigin.pData);
pSlotOrigin.DoSetData(pMyData);
}
public void DoSetData(IInventoryData pData)
{
if (bIsDebug)
Debug.Log($"{name}-{iSlotIndex} {nameof(DoSetData)} - {pData}", this);
OnChange_SlotData?.Invoke(new OnChangeSlotData_Msg(this, this.pData, pData));
this.pData = pData;
}
public void DoClear()
{
if (bIsDebug)
Debug.Log($"{name}-{iSlotIndex} {nameof(DoClear)}", this);
OnChange_SlotData?.Invoke(new OnChangeSlotData_Msg(this, pData, null));
pData = null;
}
public void OnPointerClick(PointerEventData eventData) { OnClickedSlot?.Invoke(this, eventData); }
public void OnPointerEnter(PointerEventData eventData) { OnHoverSlot?.Invoke(this, eventData); }
public void Event_SetSelected(bool bIsSelected, bool bIsSwapData_OnOverlapSlot = true)
{
this.bIsSelected = bIsSelected;
if (bIsSwapData_OnOverlapSlot && bIsSelected && Inventory.g_setActiveInventory.Count >= 2)
{
foreach (Inventory pInventoryOther in Inventory.g_setActiveInventory)
{
if (pInventoryOther == pInventory)
continue;
if (pInventoryOther.pSlotSelected == null)
continue;
Event_OnSwapSlot(pInventoryOther, pInventoryOther.pSlotSelected, pInventory, this);
}
}
if (bIsSelectAble)
OnChange_IsSelected?.Invoke(bIsSelected);
}
protected void Event_OnSwapSlot(Inventory pInventory_OnDraging, InventorySlot pSlot_OnDraging, Inventory pInventory_Dest, InventorySlot pSlot_Dest)
{
if (bIsDebug)
Debug.Log($"{nameof(Event_OnSwapSlot)} - pSlot_OnDraging : {pInventory_OnDraging.name}-{pSlot_OnDraging.name} => pSlot_Dest : {pInventory_Dest.name}-{pSlot_Dest.name}", pSlot_OnDraging);
OnSwapSlot?.Invoke(new Inventory.OnSwapSlot_Msg(pInventory_OnDraging, pSlot_OnDraging, pInventory_Dest, pSlot_Dest));
}
public void Event_OnSetClone()
{
bIsClone = true;
}
// ========================================================================== //
/* protected - [Override & Unity API] */
/* protected - [abstract & virtual] */
// ========================================================================== //
#region Private
void ExecuteLogic_StateEvent_OnChangeData(OnChangeSlotData_Msg sMsg) { ExecuteLogic_State(sMsg.bSlot_IsEmpty ? EInventorySlot_StateEvent.OnEmptySlot : EInventorySlot_StateEvent.OnFillSlot); }
void ExecuteLogic_StateEvent_OnSelected(bool bSelected) { ExecuteLogic_State(EInventorySlot_StateEvent.OnSelectedSlot); }
void ExecuteLogic_StateEvent_OnSwap(Inventory.OnSwapSlot_Msg sMsg) { ExecuteLogic_State(EInventorySlot_StateEvent.OnSwapSlot); }
void ExecuteLogic_CommandEvent_OnHover(InventorySlot arg1, PointerEventData arg2) { ExecuteLogic_Command(EInventorySlot_CommandEvent.OnHover, arg2); }
void ExecuteLogic_CommandEvent_OnClick(InventorySlot arg1, PointerEventData arg2) { ExecuteLogic_Command(EInventorySlot_CommandEvent.OnClick, arg2); }
protected void ExecuteLogic_State(EInventorySlot_StateEvent eEvent)
{
if (_mapSlotLogic_State.TryGetValue(eEvent, out var listLogic))
{
for (int i = 0; i < listLogic.Count; i++)
listLogic[i].pLogic.IInventorySlot_StateLogic(this);
}
if (_mapSlotLogic_State_Undo.TryGetValue(eEvent, out listLogic))
{
for (int i = 0; i < listLogic.Count; i++)
listLogic[i].pLogic.IInventorySlot_StateLogic_Undo(this);
}
}
protected void ExecuteLogic_Command(EInventorySlot_CommandEvent eEvent, PointerEventData pPointerEventData)
{
if (_mapSlotLogic_Command.TryGetValue(eEvent, out var listLogic))
{
for (int i = 0; i < listLogic.Count; i++)
listLogic[i].pLogic.IInventorySlot_CommandLogic(this, pPointerEventData);
}
if (_mapSlotLogic_Command_Undo.TryGetValue(eEvent, out listLogic))
{
for (int i = 0; i < listLogic.Count; i++)
listLogic[i].pLogic.IInventorySlot_CommandLogic_Undo(this, pPointerEventData);
}
}
#endregion Private
}
#if UNITY_EDITOR
[CanEditMultipleObjects]
[CustomEditor(typeof(InventorySlot))]
public class InventorySlot_Inspector : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
InventorySlot pTargetSlot = target as InventorySlot;
IEnumerable<InventorySlot> arrTarget = targets.Cast<InventorySlot>();
if (GUILayout.Button("Update Slot Index - by Sibling"))
{
foreach (var pSlot in arrTarget)
pSlot.DoUpdateIndex_IsSibling();
}
}
}
#endif
}