Tanoda
GameObjectAction.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using System.Runtime.Serialization;
6using GILES;
7using JetBrains.Annotations;
8using UnityEngine;
11using UnityEngine.UI;
12
13public class GameObjectAction : ActionObject, ISerializable
14{
16 public string canvasSelectorName = "Canvas_selector";
17 public string selectedHash = "";
18
19 [SerializeField] internal GameObject selectedGO;
20 private GameObject canvasSelector;
21 private GameObject canvasHierarchy;
22 private Canvas canvas;
24 private Coroutine mouseOver;
25 private GameObject highlightObject;
26 public Material highlightMaterial;
27
28 private GameObject canvasGo;
29 private GameObject handPrefab, leftHandPrefab;
30 private GameObject handInstance;
31 private Transform attachPoint;
32 private bool editingLeft = false;
33 private Canvas canvasOffsetEditor;
34
35 public Vector3 Offset;
36 public Vector3 Rotation;
37 public Vector3 LeftOffset;
38 public Vector3 LeftRotation;
39
40 public override void Start()
41 {
42
43#if DANA
44 handPrefab = Resources.Load("OffsetEditorHand_new") as GameObject;
45 leftHandPrefab = Resources.Load("OffsetEditorLeftHand_new") as GameObject;
46#else
47 handPrefab = Resources.Load("OffsetEditorHand_WebGL") as GameObject;
48 leftHandPrefab = Resources.Load("OffsetEditorLeftHand_WebGL") as GameObject;
49#endif
50 base.Start();
51 }
52
53
54 void PrepareScene()
55 {
56 canvasGo = GameObject.Find("Canvas");
57 canvas = canvasGo.GetComponent<Canvas>();
58 canvas.enabled = false;
59
60 GameObject canvasOffsetGo = GameObject.Find("Canvas_OffsetEditor");
61 canvasOffsetEditor = canvasOffsetGo.GetComponent<Canvas>();
62 canvasOffsetEditor.enabled = true;
63
64 var saveBtn = canvasOffsetGo.GetComponentInChildren<Button>();
65 if (saveBtn == null)
66 {
67 Debug.Log("save btn not found");
68 }
69 saveBtn.onClick.RemoveAllListeners();
70
71 saveBtn.onClick.AddListener(ExitOffsetEditor);
73 pb_Selection.SetSelection(attachPoint.gameObject);
74 pb_Selection.AddOnRemovedFromSelectionListener(OnRemovedFromSelection);
75 }
76 void OnRemovedFromSelection(IEnumerable<GameObject> selection)
77 {
78 ExitOffsetEditor();
79 }
80 private new void OnDestroy()
81 {
82 Destroy(highlightObject);
83 base.OnDestroy();
84 }
85
86 void ExitOffsetEditor()
87 {
88 pb_Selection.instance.OnRemovedFromSelection -= OnRemovedFromSelection;
89
90 MiscLogicManager.instance.OffsetEditorHelper(attachPoint, GetGameObject(), this, canvas, canvasOffsetEditor, editingLeft);
91 }
92
93 public void EnterOffsetEditor()
94 {
95 if (handPrefab == null)
96 {
97 Debug.LogError("couldn't find glove, offset editor cannot be entered");
98 return;
99 }
100
101 var go = GetGameObject();
102 if (go == null) return;
103 Vector3 scale = go.transform.localScale;
104 if (scale.x == 0f || scale.y == 0f || scale.z == 0f)
105 {
106 Debug.LogError("object has at least one scale component set to 0, offset editor cannot be entered");
107 return;
108 }
109 StartCoroutine(SpawnHand());
110 }
111
113 {
114 if (leftHandPrefab == null)
115 {
116 Debug.LogError("couldn't find glove, offset editor cannot be entered");
117 return;
118 }
119 var go = GetGameObject();
120 if (go == null) return;
121 Vector3 scale = go.transform.localScale;
122 if (scale.x == 0f || scale.y == 0f || scale.z == 0f)
123 {
124 Debug.LogError("object has at least one scale component set to 0, offset editor cannot be entered");
125 return;
126 }
127 StartCoroutine(SpawnHand(true));
128 }
129
130 IEnumerator SpawnHand(bool left = false)
131 {
132 editingLeft = left;
133
134 handInstance = Instantiate(left ? leftHandPrefab : handPrefab, Vector3.zero, new Quaternion(), null);
135 yield return null;
136
137 attachPoint = handInstance.transform.Find("AttachPoint");
138 attachPoint.transform.SetParent(null, true);
139 yield return null;
140
141 handInstance.transform.SetParent(attachPoint, true);
142 yield return null;
143
144 attachPoint.transform.SetParent(GetGameObject().transform, true);
145 yield return null;
146
147 attachPoint.transform.localPosition = left ? LeftOffset : Offset;
148 attachPoint.transform.localRotation = left ? Quaternion.Euler(LeftRotation) : Quaternion.Euler(Rotation);
149 yield return null;
150
151 var scale = attachPoint.transform.localScale;
152
153 if (scale.x != scale.y || scale.x != scale.z)
154 {
155 attachPoint.transform.SetParent(null, true);
156 yield return null;
157#if !DANA
158 attachPoint.transform.localScale = Vector3.one;
159#else
160 attachPoint.transform.localScale = Vector3.one * 0.09f;
161#endif
162 yield return null;
163 }
164
165 PrepareScene();
166 }
167
168 public void IfTrigger(string id)
169 {
170 Triggered(id);
171 }
172
173 public void IfNotTrigger()
174 {
175 TriggerOutput("");
176 }
177
178 public void SetGameObject(GameObject go)
179 {
180 if (go == null) return;
181 var reinit = gop.asset != null;
182 gop.asset = go;
183
184 if (!gop.enabled)
185 gop.enabled = true;
186
187 if (reinit)
189 else
190 gop.Initialize();
191 selectedGO = go;
192
193 gop.OnAssetNull = new UnityEvent();
194 gop.OnAssetNull.AddListener(OnAssetIsNullCallback);
195
196 var goRo = go.GetComponent<RenameObject>();
197 var goName = goRo ? string.IsNullOrEmpty(goRo.NewName) ? go.name : goRo.NewName : go.name;
198
199 GetComponentInChildren<ObjectDroppedEvent>().NameText.text = goName;
200
201 //Triggered("");
202
203 //Controller.Instance.Trigger(GetOutputs()[0]);
204 }
205
206
207 public IEnumerator SetGameObjectJob(string hash)
208 {
209 if (selectedHash == "")
210 selectedHash = hash;
211
212 yield return new WaitForEndOfFrame();
213 while (LoadingManager.instance.isLoading) yield return new WaitForEndOfFrame();
214 SetGameObject(hash);
215 }
216
217 public void SetGameObject(string hash)
218 {
219 if (hash == "") return;
220 var reinit = gop.asset != null;
221 gop.asset = HashingManager.instance.GetGOFromHash(hash);
222 if (gop.asset == null) return;
223
224 if (!gop.enabled)
225 gop.enabled = true;
226
227 if (reinit)
229 else
230 gop.Initialize();
231 selectedGO = gop.asset;
232
233 gop.OnAssetNull = new UnityEvent();
234 gop.OnAssetNull.AddListener(OnAssetIsNullCallback);
235
236 var goRo = gop.asset.GetComponent<RenameObject>();
237 var goName = goRo ? string.IsNullOrEmpty(goRo.NewName) ? gop.asset.name : goRo.NewName : gop.asset.name;
238
239 GetComponentInChildren<ObjectDroppedEvent>().NameText.text = goName;
240
241 //Triggered("");
242
243 //Controller.Instance.Trigger(GetOutputs()[0]);
244 }
245
246 private void OnAssetIsNullCallback()
247 {
249 }
250
251 public void SetSelectedGO()
252 {
254 canvasSelector.GetComponent<Canvas>().enabled = false;
255 GetComponentInParent<Canvas>().enabled = true;
256 canvasSelector.transform.GetChild(0).GetComponent<Button>().onClick.RemoveListener(SetSelectedGO);
257 canvasSelector.transform.GetChild(1).GetComponent<Button>().onClick.RemoveListener(CancelSelection);
258
260
261 pb_SelectionHandle.instance.SetTool(Tool.Position);
262
263 canvasHierarchy.transform.SetParent(canvas.transform, false);
264 }
265
266 public void CancelSelection()
267 {
268 canvasSelector.GetComponent<Canvas>().enabled = false;
269 GetComponentInParent<Canvas>().enabled = true;
270 canvasSelector.transform.GetChild(0).GetComponent<Button>().onClick.RemoveListener(SetSelectedGO);
271 canvasSelector.transform.GetChild(1).GetComponent<Button>().onClick.RemoveListener(CancelSelection);
272
274
275 pb_SelectionHandle.instance.SetTool(Tool.Position);
276
277 canvasHierarchy.transform.SetParent(canvas.transform, false);
278 }
279
280 public void TriggerSelection()
281 {
282 if (!canvasSelector)
283 canvasSelector = GameObject.Find(canvasSelectorName);
284
285 if (!canvasSelector)
286 {
287 Debug.LogError(canvasSelectorName + " not found!");
288 return;
289 }
290
291 GetComponentInParent<Canvas>().enabled = false;
292 canvasSelector.transform.GetChild(0).GetComponent<Button>().onClick.AddListener(SetSelectedGO);
293 canvasSelector.transform.GetChild(1).GetComponent<Button>().onClick.AddListener(CancelSelection);
294 canvasSelector.GetComponent<Canvas>().enabled = true;
295 pb_SelectionHandle.instance.SetTool(Tool.None);
296
297 GetComponentInChildren<GameObjectPreview>().GetComponent<Image>().enabled = false;
298 var canvasGo = GameObject.Find("Canvas");
299 canvas = canvasGo.GetComponent<Canvas>();
300 if (canvas == null)
301 {
302 Debug.LogError("canvas not found!");
303 return;
304 }
305
306 canvasHierarchy = GameObject.Find("Hierarchy");
307 if (canvasHierarchy == null)
308 {
309 Debug.LogError("Hierarchy not found!");
310 return;
311 }
312
313 canvasHierarchy.transform.SetParent(canvasSelector.transform, false);
314 }
315
316 public string GetOutput()
317 {
318 try
319 {
320 return outIDs.Keys.ToList()[0];
321 }
322 catch (Exception)
323 {
324 return null;
325 }
326 }
327
328 internal GameObject GetOutputAction()
329 {
330 try
331 {
333 var action = Controller.Instance.GetActionByInOut(connection.toId);
334
335 return action.gameObject;
336 }
337 catch (Exception)
338 {
339 return null;
340 }
341 }
342
343 [CanBeNull]
344 public GameObject GetGameObject()
345 {
346 if (selectedGO == null)
348
349 return selectedGO;
350 }
351
352
353 public override void OnPointerEnter(PointerEventData eventData)
354 {
355 if (SavedUser.instance.isEditor)
356 mouseOver = StartCoroutine(ShowHighLight());
357 }
358 public override void OnPointerExit(PointerEventData eventData)
359 {
360 if (SavedUser.instance.isEditor)
361 {
362 if (mouseOver != null)
363 StopCoroutine(mouseOver);
364 Destroy(highlightObject);
365 }
366 }
367
368
369 private IEnumerator ShowHighLight()
370 {
371 if (highlightObject) Destroy(highlightObject);
372
373 if (selectedHash == "" && (gop == null || gop.asset == null)) yield break;
374
375 if (MiscLogicManager.instance.mode != MiscLogicManager.CurrentMode.Training && MiscLogicManager.instance.mode != MiscLogicManager.CurrentMode.Optimization) yield break;
376
377 if (!selectedGO)
378 selectedGO = HashingManager.instance.GetGOFromHash(selectedHash);
379
380 yield return new WaitForEndOfFrame();
381
382 GameObject cachedGO = GetGameObject();
383 var inflate = false;
384
385 highlightObject = Instantiate(cachedGO, cachedGO.transform.position, cachedGO.transform.rotation, null);
386 highlightObject.transform.position = cachedGO.transform.position;
387 highlightObject.transform.rotation = cachedGO.transform.rotation;
388 highlightObject.transform.localScale = cachedGO.transform.lossyScale;
389
390#if !UNITY_WEBGL
391 Macro.SetStatic(highlightObject);
392 Destroy(highlightObject.GetComponent<TorqueWrench>());
393 Destroy(highlightObject.GetComponent<SeegerHelper>());
394#endif
395
396 var allCollider = highlightObject.GetComponentsInChildren<Collider>();
397 var allRb = highlightObject.GetComponentsInChildren<Rigidbody>();
398
399 foreach (var c in allCollider)
400 Destroy(c);
401
402 foreach (var c in allRb)
403 Destroy(c);
404
405 Destroy(highlightObject.GetComponent<HashHolder>());
406
407 yield return new WaitForEndOfFrame();
408
409 if (inflate)
410 highlightObject.transform.localScale *= 1.001f;
411
412 highlightObject.SetActive(true);
413
414 MatChange(highlightObject);
415 }
416 private void MatChange(GameObject go)
417 {
418 var mrs = go.GetComponentsInChildren<MeshRenderer>();
419 foreach (var meshRenderer in mrs)
420 {
421 var list = new System.Collections.Generic.List<Material>();
422 for (var i = 0; i < meshRenderer.materials.Length; i++)
423 list.Add(highlightMaterial);
424 meshRenderer.materials = list.ToArray();
425 }
426 }
427
428
429 public new void GetObjectData(SerializationInfo info, StreamingContext context)
430 {
431 base.GetObjectData(info, context);
432 info.AddValue("selectedGO", GetGameObject(), typeof(GameObject));
433 try
434 {
435 if (string.IsNullOrEmpty(selectedHash))
436 info.AddValue("selectedGOHash", selectedGO.GetComponent<HashHolder>().Hash, typeof(string));
437 else
438 try
439 {
440 info.AddValue("selectedGOHash", selectedGO.GetComponent<HashHolder>().Hash, typeof(string));
441 }
442 catch (Exception)
443 {
444 info.AddValue("selectedGOHash", selectedHash, typeof(string));
445 }
446 }
447 catch (Exception e)
448 {
449 // ignored
450 }
451 info.AddValue("offset", (Vector3)Offset, typeof(Vector3));
452 info.AddValue("rotoffset", (Vector3)Rotation, typeof(Vector3));
453 info.AddValue("leftoffset", (Vector3)LeftOffset, typeof(Vector3));
454 info.AddValue("leftrotoffset", (Vector3)LeftRotation, typeof(Vector3));
455 }
456
457 public GameObjectAction(SerializationInfo info, StreamingContext context) : base(info, context)
458 {
459 selectedGO = (GameObject) info.GetValue("selectedGO", typeof(GameObject));
460
461 foreach (var e in info)
462 {
463 if (e.Name == "selectedGOHash")
464 {
465 selectedHash = info.GetString("selectedGOHash");
466 continue;
467 }
468 if (e.Name == "offset")
469 {
470 Offset = (Vector3)info.GetValue("offset", typeof(Vector3));
471 continue;
472 }
473 if (e.Name == "leftoffset")
474 {
475 LeftOffset = (Vector3)info.GetValue("leftoffset", typeof(Vector3));
476 continue;
477 }
478 if (e.Name == "rotoffset")
479 {
480 Rotation = (Vector3)info.GetValue("rotoffset", typeof(Vector3));
481 continue;
482 }
483 if (e.Name == "leftrotoffset")
484 {
485 LeftRotation = (Vector3)info.GetValue("leftrotoffset", typeof(Vector3));
486 continue;
487 }
488
489 }
490 }
491}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
System.Drawing.Image Image
Definition: TestScript.cs:37
void TriggerOutput(string id)
string action
Definition: ActionObject.cs:32
virtual void Triggered(string id)
Connection GetConnectionByEndPointId(string id)
Definition: Controller.cs:857
ActionObject GetActionByInOut(string id)
Definition: Controller.cs:925
static Controller Instance
Definition: Controller.cs:16
static void AddOnRemovedFromSelectionListener(Callback< IEnumerable< GameObject > > del)
Definition: pb_Selection.cs:45
static void SetSelection(IEnumerable< GameObject > selection)
Definition: pb_Selection.cs:92
static void Clear()
Definition: pb_Selection.cs:61
static GameObject activeGameObject
Definition: pb_Selection.cs:82
override void OnPointerEnter(PointerEventData eventData)
GameObject GetGameObject()
GameObjectPreview gop
void SetGameObject(string hash)
GameObjectAction(SerializationInfo info, StreamingContext context)
new void GetObjectData(SerializationInfo info, StreamingContext context)
override void OnPointerExit(PointerEventData eventData)
void IfTrigger(string id)
override void Start()
Material highlightMaterial
void SetGameObject(GameObject go)
IEnumerator SetGameObjectJob(string hash)
void Initialize(bool reinitialize=false)
string Hash
Definition: HashHolder.cs:18
Definition: Macro.cs:12
static void SetStatic(GameObject go)
Definition: Macro.cs:388
string NewName
Definition: RenameObject.cs:7
Tool
Definition: pb_Enum.cs:24