Tanoda
pb_VisualOffsetEditor.cs
Go to the documentation of this file.
1using NaughtyAttributes;
2using System.Collections;
3using System.Collections.Generic;
4using UnityEngine;
5using UnityEngine.UI;
6
7
8namespace GILES.Interface
9{
11 {
12 private VisualOffset _vo;
13 private OffsetHolder offsetHolder;
14 private GameObject canvasGo;
15 private Canvas canvas;
16 private Canvas canvasOffsetEditor;
17 private Button saveBtn;
18 private GameObject handPrefab, leftHandPrefab;
19 private GameObject handInstance;
20 private GameObject objectToGrab;
21 private Transform attachPoint;
22 private bool editingLeft = false;
23
24 protected override void InitializeGUI()
25 {
26 _vo = ((VisualOffset) target);
27 pb_GUIUtility.AddVerticalLayoutGroup(gameObject);
28
29 var horizontalParent = new GameObject("HorizontalParent");
30 var hlg = horizontalParent.AddComponent<HorizontalLayoutGroup>();
31 var lec = horizontalParent.AddComponent<LayoutElement>();
32 lec.minHeight = 32;
33 horizontalParent.transform.SetParent(transform);
34
35 {
36 var buttonGO = new GameObject("ClickButton");
37 var image = buttonGO.AddComponent<Image>();
38 var button = buttonGO.AddComponent<Button>();
39 var textGO = new GameObject("LabelGO");
40 var text = textGO.AddComponent<Text>();
41 textGO.GetComponent<RectTransform>().sizeDelta = new Vector2(65, 14);
42 text.font = Font.CreateDynamicFontFromOSFont("Arial", 24);
43 text.alignment = TextAnchor.MiddleCenter;
44 text.text = Macro.T("Edit Left offset");
45 text.color = Color.white;
46 text.resizeTextForBestFit = true;
47 text.resizeTextMaxSize = 16;
48 textGO.transform.SetParent(buttonGO.transform);
49 image.sprite = GameObject.Find("SaveButton").GetComponent<Image>().sprite;
50 image.type = Image.Type.Sliced;
51 image.color = new Color(255, 255, 255, 40 / 255.0f);
52 buttonGO.transform.SetParent(horizontalParent.transform);
53 button.onClick.AddListener(EnterLeftOffsetEditor);
54 }
55 {
56 var buttonGO = new GameObject("ClickButton");
57 var image = buttonGO.AddComponent<Image>();
58 var button = buttonGO.AddComponent<Button>();
59 var textGO = new GameObject("LabelGO");
60 var text = textGO.AddComponent<Text>();
61 textGO.GetComponent<RectTransform>().sizeDelta = new Vector2(65, 14);
62 text.font = Font.CreateDynamicFontFromOSFont("Arial", 24);
63 text.alignment = TextAnchor.MiddleCenter;
64 text.text = Macro.T("Edit Right offset");
65 text.color = Color.white;
66 text.resizeTextForBestFit = true;
67 text.resizeTextMaxSize = 16;
68 textGO.transform.SetParent(buttonGO.transform);
69 image.sprite = GameObject.Find("SaveButton").GetComponent<Image>().sprite;
70 image.type = Image.Type.Sliced;
71 image.color = new Color(255,255,255, 40/255.0f);
72 buttonGO.transform.SetParent(horizontalParent.transform);
73 button.onClick.AddListener(EnterOffsetEditor);
74 }
75
76#if DANA
77 handPrefab = Resources.Load("OffsetEditorHand_new") as GameObject;
78 leftHandPrefab = Resources.Load("OffsetEditorLeftHand_new") as GameObject;
79#else
80 handPrefab = Resources.Load("OffsetEditorHand_WebGL") as GameObject;
81 leftHandPrefab = Resources.Load("OffsetEditorLeftHand_WebGL") as GameObject;
82#endif
83 }
84
85 void EnterOffsetEditor()
86 {
87 if (handPrefab == null){
88 Debug.LogError("couldn't find glove, offset editor cannot be entered");
89 return;
90 }
91
92 objectToGrab = pb_Selection.activeGameObject;
93 if(objectToGrab == null){
94 Debug.LogError("no selected object, offset editor cannot be entered");
95 return;
96 }
97
98 Vector3 scale = objectToGrab.transform.localScale;
99 if(scale.x == 0f || scale.y == 0f || scale.z == 0f) {
100 Debug.LogError("object has at least one scale component set to 0, offset editor cannot be entered");
101 return;
102 }
103
104 offsetHolder = objectToGrab.GetComponent<OffsetHolder>();
105 StartCoroutine(SpawnHand());
106 }
107
108 void EnterLeftOffsetEditor()
109 {
110 if (leftHandPrefab == null){
111 Debug.LogError("couldn't find glove, offset editor cannot be entered");
112 return;
113 }
114
115 objectToGrab = pb_Selection.activeGameObject;
116 if(objectToGrab == null){
117 Debug.LogError("no selected object, offset editor cannot be entered");
118 return;
119 }
120
121 Vector3 scale = objectToGrab.transform.localScale;
122 if(scale.x == 0f || scale.y == 0f || scale.z == 0f) {
123 Debug.LogError("object has at least one scale component set to 0, offset editor cannot be entered");
124 return;
125 }
126
127 offsetHolder = objectToGrab.GetComponent<OffsetHolder>();
128 StartCoroutine(SpawnHand(true));
129 }
130
131 void PrepareScene()
132 {
133 canvasGo = GameObject.Find("Canvas");
134 canvas = canvasGo.GetComponent<Canvas>();
135 canvas.enabled = false;
136
137 GameObject canvasOffsetGo = GameObject.Find("Canvas_OffsetEditor");
138 canvasOffsetEditor = canvasOffsetGo.GetComponent<Canvas>();
139 canvasOffsetEditor.enabled = true;
140
141 saveBtn = canvasOffsetGo.GetComponentInChildren<Button>();
142 if(saveBtn == null)
143 {
144 Debug.Log("save btn not found");
145 }
146
147 saveBtn.onClick.AddListener(ExitOffsetEditor);
148 pb_Selection.Clear();
149 pb_Selection.SetSelection(attachPoint.gameObject);
150 pb_Selection.AddOnRemovedFromSelectionListener(OnRemovedFromSelection);
151 }
152
153 IEnumerator SpawnHand(bool left = false)
154 {
155 editingLeft = left;
156
157 handInstance = Instantiate(left ? leftHandPrefab : handPrefab, Vector3.zero, new Quaternion(), null);
158 yield return null;
159
160 attachPoint = handInstance.transform.Find("AttachPoint");
161 attachPoint.transform.SetParent(null, true);
162 yield return null;
163
164 handInstance.transform.SetParent(attachPoint, true);
165 yield return null;
166
167 attachPoint.transform.SetParent(objectToGrab.transform, true);
168 yield return null;
169
170 attachPoint.transform.localPosition = left ? offsetHolder.LeftOffset : offsetHolder.Offset;
171 attachPoint.transform.localRotation = left ? Quaternion.Euler(offsetHolder.LeftRotation) : Quaternion.Euler(offsetHolder.Rotation);
172 yield return null;
173
174 var scale = attachPoint.transform.localScale;
175
176 if (scale.x != scale.y || scale.x != scale.z)
177 {
178 attachPoint.transform.SetParent(null, true);
179 yield return null;
180#if !DANA
181 attachPoint.transform.localScale = Vector3.one;
182#else
183 attachPoint.transform.localScale = Vector3.one * 0.09f;
184#endif
185 yield return null;
186 }
187
188 PrepareScene();
189 }
190
191 [Button]
192 private void slowspawn1()
193 {
194 objectToGrab = pb_Selection.activeGameObject;
195 offsetHolder = objectToGrab.GetComponent<OffsetHolder>();
196 handInstance = Instantiate(handPrefab, Vector3.zero, new Quaternion(), null);
197 }
198 [Button]
199 private void slowspawn2()
200 {
201 attachPoint = handInstance.transform.Find("AttachPoint");
202 attachPoint.transform.SetParent(null, true);
203 }
204 [Button]
205 private void slowspawn3()
206 {
207 handInstance.transform.SetParent(attachPoint, true);
208 }
209 [Button]
210 private void slowspawn4()
211 {
212 attachPoint.transform.SetParent(objectToGrab.transform, true);
213 }
214 [Button]
215 private void slowspawn5()
216 {
217 attachPoint.transform.localPosition = offsetHolder.Offset;
218 attachPoint.transform.localRotation = Quaternion.Euler(offsetHolder.Rotation);
219 }
220
221 void ExitOffsetEditor()
222 {
223 saveBtn.onClick.RemoveListener(ExitOffsetEditor);
224 pb_Selection.instance.OnRemovedFromSelection -= OnRemovedFromSelection;
225
226 MiscLogicManager.instance.OffsetEditorHelper(attachPoint, objectToGrab, offsetHolder, canvas, canvasOffsetEditor, editingLeft);
227
228 //StartCoroutine(ActualExitSave());
229
230 //if (editingLeft)
231 //{
232 //
233 // offsetHolder.LeftOffset = objectToGrab.transform.InverseTransformPoint(attachPoint.position);
234 // var localEuler = (Quaternion.Inverse(objectToGrab.transform.rotation) * attachPoint.rotation);
235 // offsetHolder.LeftRotation = localEuler.eulerAngles;
236 //}
237 //else
238 //{
239 // offsetHolder.Offset = objectToGrab.transform.InverseTransformPoint(attachPoint.position);
240 // var localEuler = (Quaternion.Inverse(objectToGrab.transform.rotation) * attachPoint.rotation);
241 // offsetHolder.Rotation = localEuler.eulerAngles;
242 //}
243 //
244 //if(attachPoint.gameObject != null) Destroy(attachPoint.gameObject);
245 }
246
247 IEnumerator ActualExitSave()
248 {
249 attachPoint.SetParent(objectToGrab.transform, true);
250 yield return null;
251
252 if (editingLeft)
253 {
254 offsetHolder.LeftOffset = attachPoint.localPosition;
255 offsetHolder.LeftRotation = attachPoint.localRotation.eulerAngles;
256 }
257 else
258 {
259 offsetHolder.Offset = attachPoint.localPosition;
260 offsetHolder.Rotation = attachPoint.localRotation.eulerAngles;
261 }
262
263 if (attachPoint.gameObject != null) Destroy(attachPoint.gameObject);
264
265 pb_Selection.Clear();
266
267 canvas.enabled = true;
268 canvasOffsetEditor.enabled = false;
269 }
270
271 void OnRemovedFromSelection(IEnumerable<GameObject> selection)
272 {
273 ExitOffsetEditor();
274 }
275 }
276}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
System.Drawing.Image Image
Definition: TestScript.cs:37
UnityEngine.Color Color
Definition: TestScript.cs:32
Component target
The UnityEngine.Component being edited.
static GameObject activeGameObject
Definition: pb_Selection.cs:82
Definition: Macro.cs:12
static string T(string key)
Definition: Macro.cs:19
Vector3 Offset
Definition: OffsetHolder.cs:7
Vector3 LeftRotation
Definition: OffsetHolder.cs:10
Vector3 Rotation
Definition: OffsetHolder.cs:8
Vector3 LeftOffset
Definition: OffsetHolder.cs:9