Tanoda
PropertiesHelper.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using GILES;
4using GILES.Interface;
5using NaughtyAttributes;
6using UnityEngine;
7using UnityEngine.UI;
8
9public class PropertiesHelper : pb_MonoBehaviourSingleton<PropertiesHelper>
10{
13 public Text fileName, fileType;
14 internal pb_PrefabBrowserItemButton cached;
15
16 internal void OpenAsset(pb_PrefabBrowserItemButton sender)
17 {
18#if UNITY_WEBGL
19
20 var cg = propertiesWindow.GetComponent<CanvasGroup>();
21
22 cg.alpha = 1;
23 cg.blocksRaycasts = cg.alpha == 1f;
24 cg.interactable = cg.alpha == 1f;
25#else
26 propertiesWindow.SetActive(true);
27#endif
28 if (cached)
29 cached.forceRender = false;
30 cached = sender;
31 preview.asset = sender.asset;
32 //preview.Initialize(true);
33
34 if (sender.previewComponent)
35 preview.previewImg.texture = sender.previewComponent.texture;
36 preview.previewImg.gameObject.SetActive(preview.previewImg.texture != null);
37
38 fileName.text = sender.asset.name;
39 fileType.text = sender.asset.name.Remove(0, sender.asset.name.LastIndexOf(".") + 1);
40 CleanTagCloud();
41
42 var tc = preview.asset.GetComponent<TagCloud>();
43 tc?.TryLoad();
44 if (tc != null)
45 foreach (var tcTag in tc.tags)
46 {
47 AddTag(tcTag);
48 }
49#if UNITY_WEBGL
50 makeGlobalButton.SetActive(true);
51 if (cached.asset.GetComponent<UploadedFile>().global)
52 {
53 Debug.Log("File is already global, hiding button!");
54 makeGlobalButton.SetActive(false);
55 }
56#endif
57 }
58
59 public void Close()
60 {
61 cached.forceRender = false;
62
63#if UNITY_WEBGL
64
65 var cg = propertiesWindow.GetComponent<CanvasGroup>();
66
67 cg.alpha = 0;
68 cg.blocksRaycasts = cg.alpha == 1f;
69 cg.interactable = cg.alpha == 1f;
70#endif
71 }
72
73 private void CleanTagCloud()
74 {
75 for (int i = 0; i < tagCloud.transform.childCount - 2; i++)
76 {
77 Destroy(tagCloud.transform.GetChild(i + 1).gameObject);
78 }
79 }
80
81 internal float GetTextWidth(Text t)
82 {
83 TextGenerator textGen = new TextGenerator();
84 TextGenerationSettings generationSettings = t.GetGenerationSettings(t.rectTransform.rect.size);
85 float width = textGen.GetPreferredWidth(t.text, generationSettings);
86 //float height = textGen.GetPreferredHeight(t.text, generationSettings);
87 return width;
88 }
89
90 internal void AddTag(string tagName, bool isNew = false)
91 {
92 if (string.IsNullOrEmpty(tagName) || string.IsNullOrWhiteSpace(tagName))
93 return;
94
95 var newTag = Instantiate(tagPrefab, tagCloud.transform);
96 newTag.name = tagName;
97 var text = newTag.GetComponentInChildren<Text>();
98 text.text = tagName;
99 var layoutElement = newTag.GetComponent<LayoutElement>();
100 layoutElement.preferredHeight = 30;
101 layoutElement.preferredWidth = GetTextWidth(text) + 10 + 25;
102 if (isNew)
103 {
104 var tc = preview.asset.GetComponent<TagCloud>();
105 tc.tags.Add(tagName);
106 tc.Save();
107 }
108
109 addnewButton.transform.SetAsLastSibling();
110 }
111
112 public void RemoveTag(string tagName)
113 {
114 var tc = preview.asset.GetComponent<TagCloud>();
115 tc.tags.Remove(tagName);
116 tc.Save();
117 }
118
119 public void AddNewTag()
120 {
121 var tagInput = Instantiate(inputPrefab, tagCloud.transform);
122 addnewButton.transform.SetAsLastSibling();
123
124 }
125}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
GameObject makeGlobalButton
GameObject propertiesWindow
void RemoveTag(string tagName)
GameObject addnewButton
GameObject inputPrefab
pb_PrefabBrowserItemButton preview
GameObject tagPrefab
void TryLoad()
Definition: TagCloud.cs:48
List< string > tags
Definition: TagCloud.cs:10