EditorController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using UnityEngine.UI;
  9. /// <summary>
  10. /// Редактор
  11. /// </summary>
  12. public class EditorController : MonoBehaviour
  13. {
  14. public static EditorController instance;
  15. public InputField Name;
  16. public InputField Image;
  17. public InputField SizeX;
  18. public InputField SizeY;
  19. public GameObject BeaconsContent;
  20. public GameObject PanelBeaconEdit;
  21. public GameObject Player;
  22. public GameObject ButtonMode;
  23. public GameObject EditorTools;
  24. public Toggle ToggleScalePanel;
  25. public Dropdown DropdownLocation;
  26. public Transform Locations;
  27. public Button ButtonAddBeacon;
  28. public GameObject Dialog;
  29. public GameObject NewLocation;
  30. public GameObject plane;
  31. public Location newLocation;
  32. public Dictionary<uint, Dictionary<uint, Beacon>> Beacons = new Dictionary<uint, Dictionary<uint, Beacon>>(); // маяки локации
  33. CompanyController company;
  34. ModeController mode;
  35. ToolsController tools;
  36. public Location EditLocation;
  37. private void Awake()
  38. {
  39. instance = this;
  40. }
  41. // Start is called before the first frame update
  42. void Start()
  43. {
  44. company = CompanyController.instance;
  45. mode = ButtonMode.GetComponent<ModeController>();
  46. tools = EditorTools.GetComponent<ToolsController>();
  47. ButtonAddBeacon.onClick.AddListener(() => {
  48. AddBeacon(company);
  49. });
  50. var random = new System.Random();
  51. int number = random.Next(2000000000, int.MaxValue);
  52. uint uintNumber = (uint)(number + (uint)int.MaxValue);
  53. if (NewLocation == null)
  54. {
  55. NewLocation = new GameObject();
  56. NewLocation.transform.SetParent(Locations);
  57. NewLocation.name = "NewLocation";
  58. NewLocation.transform.position = Vector3.zero;
  59. NewLocation.SetActive(false);
  60. plane = Instantiate(Resources.Load("GameObjects/Plane", typeof(GameObject))) as GameObject;
  61. plane.transform.SetParent(NewLocation.transform);
  62. plane.transform.position = new Vector3(plane.transform.localScale.x * 5, 0, plane.transform.localScale.z * 5);
  63. foreach (var g in Location.contents)
  64. {
  65. var go = new GameObject();
  66. go.name = g;
  67. go.transform.SetParent(NewLocation.transform);
  68. }
  69. newLocation = new Location
  70. {
  71. id = uintNumber,
  72. company_id=(uint)company.active_company,
  73. location = NewLocation,
  74. walls = new List<Wall>(),
  75. zones = new List<Zone>(),
  76. beacons = new Dictionary<uint, Beacon>(),
  77. plane = plane,
  78. texture_url = ""
  79. };
  80. company.locations[newLocation.id] = newLocation;
  81. company.locations_index.Add(newLocation.id);
  82. DropdownLocation.options.Add(new Dropdown.OptionData("Новая локация"));
  83. DropdownLocation.RefreshShownValue();
  84. }
  85. }
  86. // Update is called once per frame
  87. void Update()
  88. {
  89. //if (company.active_location >= 0 && company.locations != null)
  90. //{
  91. // var l = company.locations[company.locations_index[company.active_location]];
  92. // l.plane.GetComponent<TouchScript>().Resize = ToggleScalePanel.isOn;
  93. //}
  94. }
  95. /// <summary>
  96. /// Изменение размеров панели
  97. /// Загрузка текстуры
  98. /// </summary>
  99. public void UpdatePanel()
  100. {
  101. var p = company.locations[company.locations_index[company.active_location]].plane;
  102. //if (!Image.text.Equals(texture_url))
  103. //{
  104. StartCoroutine(SendingFormController.LoadImage(Image.text, p, Vector3.zero, Vector3.zero, LoadTexture));
  105. //texture_url = Image.text;
  106. //}
  107. }
  108. public void UpdatePanelSize()
  109. {
  110. var p = company.locations[company.locations_index[company.active_location]].plane;
  111. var x = 1f;
  112. if (!SizeX.text.Equals(""))
  113. x = float.Parse(SizeX.text, CultureInfo.InvariantCulture);
  114. var y = 1f;
  115. if (!SizeY.text.Equals(""))
  116. y = float.Parse(SizeY.text, CultureInfo.InvariantCulture);
  117. p.transform.localScale = new Vector3(x, 1, y);
  118. var scale = p.transform.localScale;
  119. p.transform.position = new Vector3(scale.x * 5, 0, scale.z * 5);
  120. }
  121. //public void LoadTexture(Texture2D texture)
  122. //{
  123. // var p = company.locations[company.locations_index[company.active_location]].plane;
  124. // Renderer renderer = p.GetComponent<Renderer>();
  125. // renderer.material.mainTexture = texture;
  126. //}
  127. public void LoadTexture(float x, float y)
  128. {
  129. SizeX.text = x.ToString();
  130. SizeY.text = y.ToString();
  131. }
  132. public void Save()
  133. {
  134. //SceneManager.LoadScene("Location");
  135. tools.CursorOff();
  136. if (!Name.text.Equals(""))
  137. {
  138. //var location = new Location { id = 0, name = Name.text, beacons = Beacons, zones = WallCreate.Zones, walls = WallCreate.Walls, texture_url = Image.text, plane = plane, location = NewLocation};
  139. Location location;
  140. if (EditLocation.id != 0) location = EditLocation;
  141. else location = new Location { id = Convert.ToUInt32(4000000000), plane = plane, location = NewLocation, company_id = (uint)company.active_company };
  142. location.name = Name.text;
  143. location.texture_url = Image.text;
  144. if (WallCreate.Walls.ContainsKey(EditLocation.id)) location.walls.AddRange(WallCreate.Walls[EditLocation.id]);
  145. //if (WallCreate.Zones.ContainsKey(EditLocation.id)) location.zones.AddRange(WallCreate.Zones[EditLocation.id]);
  146. if (WallCreate.Zones.ContainsKey(EditLocation.id)) location.zones = WallCreate.Zones[EditLocation.id];
  147. if (Beacons.ContainsKey(EditLocation.id))
  148. {
  149. foreach(var b in Beacons[EditLocation.id].Values)
  150. {
  151. var go = b.beacon;
  152. b.x = go.transform.position.x;
  153. b.y = go.transform.position.z;
  154. b.z = go.transform.position.y;
  155. if (location.beacons.ContainsKey(b.id))
  156. location.beacons[b.id] = b;
  157. }
  158. //location.beacons.Add(Beacons[EditLocation.id]);
  159. }
  160. company.DropdownLocation.options.Last().text = location.name;
  161. location.location.name = location.name;
  162. location.Save();
  163. location.SaveContents();
  164. mode.Switch();
  165. }
  166. else
  167. {
  168. Dialog.SetActive(true);
  169. Dialog.transform.GetChild(0).GetComponent<Text>().text = "Введите название локации";
  170. Dialog.transform.GetChild(2).GetComponent<Button>().onClick.AddListener(() =>
  171. {
  172. Dialog.SetActive(false);
  173. });
  174. }
  175. }
  176. public void Back()
  177. {
  178. //SceneManager.LoadScene("Location");
  179. mode.Switch();
  180. }
  181. public void AddBeacon(CompanyController company, Beacon b = null)
  182. {
  183. //PanelBeaconEdit.SetActive(true);
  184. var location = company.locations[company.locations_index[company.active_location]];
  185. var beacon = Instantiate(Resources.Load("GameObjects/Beacon", typeof(GameObject))) as GameObject;
  186. if (ModeController.editor) beacon.GetComponent<BeaconController>().mode = BeaconController.Mode.Editor;
  187. beacon.name = $"Beacon_{b?.id.ToString() ?? Beacons.Count.ToString()}";
  188. if (b != null)
  189. {
  190. //beacon.transform.position = new Vector3(b.x / 100, 0.5f, b.y / 100);
  191. beacon.transform.position = new Vector3(b.x, 0.5f, b.y);
  192. //beacon.transform.GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>().text
  193. beacon.GetComponent<BeaconController>().text_info = $"{b.id}\n{b.vagrant_label}\nuuid={b.uuid}\nmajor={b.major}\nminor={b.minor}\nmac={b.mac}";
  194. beacon.GetComponent<BeaconController>().id = $"{b.id}";
  195. if (b.enabled == false) beacon.GetComponent<Renderer>().material.color = Color.grey;
  196. }
  197. beacon.transform.SetParent(location.location.transform.GetChild(3).transform);
  198. var name_beacon = $"Маяк {b?.id.ToString() ?? Beacons.Count.ToString()}";
  199. PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = name_beacon;
  200. var panel_button = Instantiate(Resources.Load("GameObjects/Panel_Beacon", typeof(GameObject))) as GameObject;
  201. panel_button.transform.SetParent(BeaconsContent.transform);
  202. panel_button.name = $"Beacon_{b?.id.ToString() ?? Beacons.Count.ToString()}";
  203. var button = panel_button.transform.GetChild(0).GetComponent<Button>();
  204. button.name = $"BeaconButton_{Beacons.Count}";
  205. button.transform.GetChild(0).GetComponent<Text>().text = name_beacon;
  206. //var b = new Beacon { };
  207. var shader1 = Shader.Find("Outlined/Silhouetted Bumped Diffuse");
  208. var shader2 = Shader.Find("Standard");
  209. //var text = PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = "";
  210. //var uuid = PanelBeaconEdit.transform.GetChild(1).GetComponent<InputField>().text = "";
  211. if (b == null)
  212. {
  213. b = new Beacon();
  214. b.location_id = location.id;
  215. b.id = Convert.ToUInt32(4000000000 + Beacons[location.id].Count);
  216. b.enabled = true;
  217. BeaconEdit(b, name_beacon);
  218. }
  219. button.onClick.AddListener(() =>
  220. {
  221. BeaconEdit(b, name_beacon);
  222. //b.beacon.GetComponent<Renderer>().material.shader = shader1;
  223. //edit_beacon = b.beacon;
  224. });
  225. var button_ok = PanelBeaconEdit.transform.GetChild(7).transform.GetChild(0).GetComponent<Button>();
  226. button_ok.onClick.RemoveAllListeners();
  227. button_ok.onClick.AddListener(() =>
  228. {
  229. b.uuid = PanelBeaconEdit.transform.GetChild(2).GetComponent<InputField>().text;
  230. b.minor = ushort.Parse(PanelBeaconEdit.transform.GetChild(3).GetComponent<InputField>().text);
  231. b.major = ushort.Parse(PanelBeaconEdit.transform.GetChild(4).GetComponent<InputField>().text);
  232. b.mac = PanelBeaconEdit.transform.GetChild(5).GetComponent<InputField>().text;
  233. b.enabled = PanelBeaconEdit.transform.GetChild(6).GetComponent<Toggle>().isOn;
  234. PanelBeaconEdit.SetActive(false);
  235. //b.beacon.GetComponent<Renderer>().material.shader = shader2;
  236. });
  237. //var button_close = PanelBeaconEdit.transform.GetChild(7).transform.GetChild(1).GetComponent<Button>();
  238. //button_close.onClick.AddListener(() =>
  239. //{
  240. // PanelBeaconEdit.SetActive(false);
  241. // b.beacon.GetComponent<Renderer>().material.shader = shader1;
  242. //});
  243. var button_del = panel_button.transform.GetChild(2).GetComponent<Button>();
  244. button_del.onClick.AddListener(() =>
  245. {
  246. Destroy(beacon);
  247. Destroy(panel_button);
  248. Beacons[location.id].Remove(b.id);
  249. if (PanelBeaconEdit.activeSelf) PanelBeaconEdit.SetActive(false);
  250. });
  251. //b.id = (uint)Beacons.Count;
  252. b.beacon = beacon;
  253. b.panel_button = panel_button;
  254. Beacons[location.id][b.id]=b;
  255. }
  256. void BeaconEdit(Beacon b, string name_beacon)
  257. {
  258. PanelBeaconEdit.SetActive(true);
  259. PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = name_beacon;
  260. if (b != null)
  261. {
  262. PanelBeaconEdit.transform.GetChild(1).GetComponent<InputField>().text = b.uuid;
  263. PanelBeaconEdit.transform.GetChild(2).GetComponent<InputField>().text = b.vagrant_label.ToString();
  264. PanelBeaconEdit.transform.GetChild(3).GetComponent<InputField>().text = $"{b.major}";
  265. PanelBeaconEdit.transform.GetChild(4).GetComponent<InputField>().text = $"{b.major}";
  266. PanelBeaconEdit.transform.GetChild(5).GetComponent<InputField>().text = b.mac;
  267. PanelBeaconEdit.transform.GetChild(6).GetComponent<Toggle>().isOn = b.enabled;
  268. }
  269. }
  270. //GameObject edit_beacon;
  271. public void CloseBeaconParameters()
  272. {
  273. PanelBeaconEdit.SetActive(false);
  274. //var shader2 = Shader.Find("Standard");
  275. //edit_beacon.GetComponent<Renderer>().material.shader = shader2;
  276. }
  277. }