-
Notifications
You must be signed in to change notification settings - Fork 2
/
GPActionObjectMapper.cs
455 lines (361 loc) · 12.3 KB
/
GPActionObjectMapper.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//
// GPActionObjectMapper.cs
//
// Author:
// Baptiste Dupy <baptiste.dupy@gmail.com>
//
// Copyright (c) 2014
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Utils;
namespace ActionTool
{
public class GPActionObjectMapper : MonoBehaviour , ISerializationCallbackReceiver
{
[System.Serializable]
public class GPActionObjectMap : DictionaryWrapper<EventHandler,GameObject>
{}
#region Private Members
/// <summary>
/// Maps EventHanlder to GameObject. Those game object are GPActionHolderObject
/// </summary>
[UnityEngine.SerializeField]
private GPActionObjectMap m_actionObjectMap;
#endregion
#region Constructors
/// <summary>
/// Default Constructor
/// </summary>
public GPActionObjectMapper()
{
m_actionObjectMap = new GPActionObjectMap();
}
#endregion
#region Public Interface
#region Context Menu
[ContextMenu("Show Children")]
private void ShowAllActions()
{
for (int i = 0; i < this.transform.childCount; ++i)
this.transform.GetChild(i).gameObject.hideFlags = HideFlags.None;
}
[ContextMenu("Hide Children")]
private void HideAllActions()
{
for (int i = 0; i < this.transform.childCount; ++i)
this.transform.GetChild(i).gameObject.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
}
#endregion
/// <summary>
/// Adds an EventHandler to the mapping.
/// </summary>
/// <param name="handler"></param>
public GameObject AddEventHandler(EventHandler handler)
{
GameObject holder = CreateGPActionHolderObject(handler);
m_actionObjectMap.Dictionary.Add(handler, holder);
CheckAndAddHolderComponent(holder, handler);
return holder;
}
/// <summary>
/// Add an action to the EventHandler
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="handler"></param>
/// <returns></returns>
public GPAction AddAction(EventHandler handler,System.Type actionType)
{
if (handler == null)
throw new System.ArgumentNullException();
if (!typeof(GPAction).IsAssignableFrom(actionType))
throw new System.ArgumentException("Type 'actionType' must inherit from GPAction");
GameObject holder;
if(!m_actionObjectMap.Dictionary.TryGetValue(handler, out holder))
holder = AddEventHandler(handler);
return (GPAction) holder.AddComponent(actionType);
}
public GPActionRelatedObject GetRelatedObject(EventHandler handler)
{
if (handler == null)
throw new System.ArgumentNullException();
if (!typeof(GPAction).IsAssignableFrom(typeof(GPActionRelatedObject)))
throw new System.ArgumentException("Type 'actionType' must inherit from GPAction");
GameObject holder;
if (!m_actionObjectMap.Dictionary.TryGetValue(handler, out holder))
holder = AddEventHandler(handler);
GPActionRelatedObject[] relObjs = holder.GetComponents<GPActionRelatedObject>();
GPActionRelatedObject relObj;
if(relObjs.Length > 0)
{
relObj = relObjs[0];
if (relObjs.Length >1)
for (int i = 1; i < relObjs.Length; i++)
{
#if UNITY_EDITOR
if(UnityEditor.EditorApplication.isPlaying)
Destroy(relObjs[i]);
else
DestroyImmediate(relObjs[i]);
#else
Destroy(relObjs[i]);
#endif
}
}
else
{
relObj = holder.AddComponent<GPActionRelatedObject>();
}
return relObj;
}
/// <summary>
/// Removes the event handler from the map.
/// </summary>
/// <param name="handler">Handler.</param>
public void RemoveEventHandler(EventHandler handler)
{
try
{
DestroyImmediate(m_actionObjectMap.Dictionary[handler]);
m_actionObjectMap.Dictionary.Remove(handler);
}
catch(KeyNotFoundException)
{
Debug.LogWarning("Try to remove unexisting handler");
}
}
public GPAction[] GetAllActions(EventHandler handler)
{
if (handler == null)
throw new System.ArgumentNullException();
GameObject holder;
if(m_actionObjectMap.Dictionary.TryGetValue(handler, out holder))
{
CheckAndAddHolderComponent(holder,handler);
return holder.GetComponents<GPAction>();
}
return new GPAction[0];
}
public void CheckAndAddHolderComponent(GameObject holder, EventHandler handler)
{
GPActionHolder[] holders = holder.GetComponents<GPActionHolder>();
if(holders.Length > 1)
{
for (int i = 1; i < holders.Length; i++)
{
#if UNITY_EDITOR
if (UnityEditor.EditorApplication.isPlaying)
DestroyImmediate(holders[i]);
else
DestroyImmediate(holders[i]);
#else
Destroy(holders[i]);
#endif
}
}
else
{
GPActionHolder holderComponent = holder.AddComponent<GPActionHolder>();
holderComponent._eventHandler = handler;
}
}
#if UNITY_EDITOR
public void CheckPrefabConnection(EventHandler handler)
{
GameObject obj;
try
{
obj = m_actionObjectMap.Dictionary[handler];
}
catch (KeyNotFoundException)
{
obj = AddEventHandler(handler);
}
Object prefab = PrefabUtility.GetPrefabParent(obj);
if(prefab == null)
handler.PrefabAction = null;
}
/// <summary>
/// Imports an action prefab for the specified EventHandler
/// </summary>
/// <param name="handler"></param>
/// <param name="prefab"></param>
public void ImportGPActionObjectHolderPrefab(EventHandler handler,Object prefab)
{
GameObject instPrefab = (GameObject) PrefabUtility.InstantiatePrefab(prefab);
try
{ m_actionObjectMap.Dictionary[handler] = instPrefab; }
catch (KeyNotFoundException)
{ m_actionObjectMap.Dictionary.Add(handler,instPrefab); }
handler.PrefabAction = prefab;
handler.Action = instPrefab.GetComponent<GPActionExport>()._rootAction;
InitGPActionHolderObject(instPrefab);
}
/// <summary>
/// Exports the current GPActionHolderObject as action prefab
/// </summary>
/// <param name="handler"></param>
/// <returns></returns>
public Object ExportGPActionObjectHolderPrefab(EventHandler handler)
{
GameObject obj;
try
{
obj = m_actionObjectMap.Dictionary[handler];
}
catch (KeyNotFoundException)
{
obj = AddEventHandler(handler);
}
obj.GetComponentOrCreate<GPActionExport>()._rootAction = handler.Action;
string str = EditorUtility.SaveFilePanel("Export Action","Assets","New Action","prefab");
if(string.IsNullOrEmpty(str))
return null;
str = "Assets/"+FileUtils.GetRelativePath(str,Application.dataPath);
handler.PrefabAction = PrefabUtility.CreatePrefab(str, obj, ReplacePrefabOptions.ConnectToPrefab);
return handler.PrefabAction;
}
/// <summary>
/// Applies the modification of GPActionHolderObject to its prefab if any.
/// </summary>
/// <param name="handler"></param>
public void ApplyGPActionObjectHolderToPrefab(EventHandler handler)
{
GameObject obj;
try
{
obj = m_actionObjectMap.Dictionary[handler];
}
catch (KeyNotFoundException)
{
obj = AddEventHandler(handler);
}
Object prefab = PrefabUtility.GetPrefabParent(obj);
if(prefab == null)
{
handler.PrefabAction = null;
return;
}
PrefabUtility.ReplacePrefab(obj, prefab,ReplacePrefabOptions.ConnectToPrefab);
}
/// <summary>
/// Resets all GPActionHolderObject modifications to the prefab values
/// </summary>
/// <param name="handler"></param>
public void RevertGPActionObjectHolderToPrefab(EventHandler handler)
{
GameObject obj;
try
{
obj = m_actionObjectMap.Dictionary[handler];
}
catch (KeyNotFoundException)
{
obj = AddEventHandler(handler);
}
PrefabUtility.RevertPrefabInstance(obj);
}
public void ResetGPActionObjectHolder(EventHandler handler)
{
try
{
DestroyImmediate(m_actionObjectMap.Dictionary[handler]);
m_actionObjectMap.Dictionary.Remove(handler);
}
catch (KeyNotFoundException)
{
}
AddEventHandler(handler);
handler.Action = null;
}
public void BreakGPActionObjectHolderFromPrefab(EventHandler handler)
{
GameObject obj;
try
{
obj = m_actionObjectMap.Dictionary[handler];
}
catch (KeyNotFoundException)
{
obj = AddEventHandler(handler);
}
PrefabUtility.DisconnectPrefabInstance(obj);
handler.PrefabAction = null;
}
#endif
#endregion
#region Conditions
public GPCondition AddCondition(System.Type type, EventHandler handler)
{
if (handler == null)
throw new System.ArgumentNullException();
if (!typeof(GPCondition).IsAssignableFrom(type))
throw new System.ArgumentException("Type "+type.FullName+" must inherit from GPCondition");
GameObject holder;
if (!m_actionObjectMap.Dictionary.TryGetValue(handler, out holder))
holder = AddEventHandler(handler);
GPCondition condition = (GPCondition)holder.AddComponent(type);
condition.SetHandler(handler);
return condition;
}
#endregion
#region Protected Interface
/// <summary>
/// Create a GPActionHolderObject in the hierarchy
/// </summary>
/// <param name="handler"></param>
/// <returns></returns>
protected GameObject CreateGPActionHolderObject(EventHandler handler)
{
GameObject holder = new GameObject();
InitGPActionHolderObject(holder);
return holder;
}
/// <summary>
/// Init a GPActionHolderObject properties.
/// </summary>
/// <param name="holder"></param>
protected void InitGPActionHolderObject(GameObject holder)
{
holder.name = "__Holder__";
holder.transform.parent = this.transform;
holder.hideFlags = HideFlags.HideInHierarchy;
}
#endregion
#region Serialization Callback
public void OnBeforeSerialize()
{
}
public void OnAfterDeserialize()
{
}
#endregion
}
public class GPActionHolderObjectNotFoundException : System.Exception
{
public GPActionHolderObjectNotFoundException()
: base("GPAction Holder object not found")
{}
}
}