Tanoda
ShowHideAction.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Runtime.Serialization;
4using GILES;
5using JetBrains.Annotations;
6using NaughtyAttributes;
7using UnityEngine;
9using UnityEngine.UI;
10
11public class ShowHideAction : ActionObject, ISerializable
12{
13 public Text title;
15 public string canvasSelectorName = "Canvas_selector";
16 public string selectedHash = "";
17 internal bool Show; //pont fordĂ­tva, mint ami logikus lenne
18 public Toggle showHideToggle;
19
20 private bool originalState = true;
21 private bool _setupOk;
22
23 [SerializeField] internal GameObject selectedGO;
24 private GameObject canvasSelector;
25 private GameObject canvasHierarchy;
26 private GameObject canvas;
27
28 public void OnValueChanged(bool value)
29 {
30 Show = value;
31 title.text = !Show ? Macro.T("Show") : Macro.T("Hide");
32 }
33
34 public override void Triggered(string id)
35 {
36#if !UNITY_WEBGL
37 statusImg.color = Color.green;
38#endif
39 GetGameObject()?.SetActive(!Show);
40 }
41
42 public void SetGameObject(GameObject go)
43 {
44 if (go == null) return;
45 var reinit = gop.asset != null;
46 gop.asset = go;
47
48 if (!gop.enabled)
49 gop.enabled = true;
50
51 if (reinit)
53 else
55 selectedGO = go;
56
57 gop.OnAssetNull = new UnityEvent();
58 gop.OnAssetNull.AddListener(OnAssetIsNullCallback);
59
60 GetComponentInChildren<ObjectDroppedEvent>().NameText.text = go.name;
61 }
62
64 {
65 if (!_setupOk) return;
66 try
67 {
68 selectedGO?.SetActive(originalState);
69 }
70 catch (Exception)
71 {
73 selectedGO?.SetActive(originalState);
74 }
75 }
76
77 public void SetActualState()
78 {
79 if (selectedGO)
80 {
81 originalState = selectedGO.activeSelf;
82 _setupOk = true;
83 }
84 else
85 {
87 }
88
89 selectedGO?.SetActive(!Show);
90 }
91
92 public IEnumerator SetGameObjectJob(string hash)
93 {
94 yield return new WaitForEndOfFrame();
95 SetGameObject(hash);
96 title.text = !Show ? Macro.T("Show") : Macro.T("Hide");
97 showHideToggle.isOn = Show;
98 }
99
100 public override void Deactivate()
101 {
102 selectedGO = null;
103 }
104
105 public void SetGameObject(string hash)
106 {
107 if (hash == "") return;
108 var reinit = gop.asset != null;
109 gop.asset = HashingManager.instance.GetGOFromHash(hash);
110 if (gop.asset == null) return;
111
112 if (!gop.enabled)
113 gop.enabled = true;
114
115 if (reinit)
117 else
118 gop.Initialize();
119 selectedGO = gop.asset;
120
121 gop.OnAssetNull = new UnityEvent();
122 gop.OnAssetNull.AddListener(OnAssetIsNullCallback);
123
124 GetComponentInChildren<ObjectDroppedEvent>().NameText.text = gop.asset.name;
125 }
126
127 private void OnAssetIsNullCallback()
128 {
130 }
131
132 public void SetSelectedGO()
133 {
135 canvasSelector.GetComponent<Canvas>().enabled = false;
136 GetComponentInParent<Canvas>().enabled = true;
137 canvasSelector.transform.GetChild(0).GetComponent<Button>().onClick.RemoveListener(SetSelectedGO);
138 canvasSelector.transform.GetChild(1).GetComponent<Button>().onClick.RemoveListener(CancelSelection);
139
140 pb_SelectionHandle.instance.SetTool(Tool.Position);
142
143 canvasHierarchy.transform.SetParent(canvas.transform, false);
144 }
145
146 public void CancelSelection()
147 {
148 canvasSelector.GetComponent<Canvas>().enabled = false;
149 GetComponentInParent<Canvas>().enabled = true;
150 canvasSelector.transform.GetChild(0).GetComponent<Button>().onClick.RemoveListener(SetSelectedGO);
151 canvasSelector.transform.GetChild(1).GetComponent<Button>().onClick.RemoveListener(CancelSelection);
152
154
155 pb_SelectionHandle.instance.SetTool(Tool.Position);
156
157 canvasHierarchy.transform.SetParent(canvas.transform, false);
158 }
159
160 public void TriggerSelection()
161 {
162 canvas = GameObject.Find("Canvas");
163 if (canvas == null)
164 {
165 Debug.LogError("canvas not found!");
166 return;
167 }
168
169 if (!canvasSelector)
170 canvasSelector = GameObject.Find(canvasSelectorName);
171
172 if (!canvasSelector)
173 {
174 Debug.LogError(canvasSelectorName + " not found!");
175 return;
176 }
177
178
179 canvasHierarchy = GameObject.Find("Hierarchy");
180 if (canvasHierarchy == null)
181 {
182 Debug.LogError("Hierarchy not found!");
183 return;
184 }
185
186 canvasHierarchy.transform.SetParent(canvasSelector.transform, false);
187
188 GetComponentInParent<Canvas>().enabled = false;
189 canvasSelector.transform.GetChild(0).GetComponent<Button>().onClick.AddListener(SetSelectedGO);
190 canvasSelector.transform.GetChild(1).GetComponent<Button>().onClick.AddListener(CancelSelection);
191 canvasSelector.GetComponent<Canvas>().enabled = true;
192 pb_SelectionHandle.instance.SetTool(Tool.None);
193 }
194
195 [CanBeNull]
196 public GameObject GetGameObject()
197 {
198 if (selectedGO == null) SetGameObject(selectedHash);
199 return selectedGO;
200 }
201
202 [Button]
203 private void ReSetGO()
204 {
206 }
207
208 public new void GetObjectData(SerializationInfo info, StreamingContext context)
209 {
210 base.GetObjectData(info, context);
211 if (!selectedGO) SetGameObject(selectedHash);
212 info.AddValue("selectedGO", selectedGO, typeof(GameObject));
213 info.AddValue("Show", Show, typeof(bool));
214 try
215 {
216 info.AddValue("selectedGOHash", selectedGO.GetComponent<HashHolder>().Hash, typeof(string));
217 }
218 catch (Exception)
219 {
220 info.AddValue("selectedGOHash", selectedHash, typeof(string));
221 // ignored
222 }
223 }
224
225 public ShowHideAction(SerializationInfo info, StreamingContext context) : base(info, context)
226 {
227 selectedGO = (GameObject) info.GetValue("selectedGO", typeof(GameObject));
228 Show = info.GetBoolean("Show");
229 foreach (var e in info)
230 if (e.Name == "selectedGOHash")
231 {
232 selectedHash = info.GetString("selectedGOHash");
233 break;
234 }
235 }
236}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
Image statusImg
Definition: ActionObject.cs:21
static void Clear()
Definition: pb_Selection.cs:61
static GameObject activeGameObject
Definition: pb_Selection.cs:82
void Initialize(bool reinitialize=false)
string Hash
Definition: HashHolder.cs:18
Definition: Macro.cs:12
static string T(string key)
Definition: Macro.cs:19
GameObject GetGameObject()
void SetGameObject(string hash)
void SetActualState()
void CancelSelection()
override void Triggered(string id)
new void GetObjectData(SerializationInfo info, StreamingContext context)
override void Deactivate()
ShowHideAction(SerializationInfo info, StreamingContext context)
void OnValueChanged(bool value)
string canvasSelectorName
void TriggerSelection()
void SetGameObject(GameObject go)
IEnumerator SetGameObjectJob(string hash)
GameObjectPreview gop
void RestoreOriginalState()
Toggle showHideToggle
Tool
Definition: pb_Enum.cs:24