EditorController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. location = NewLocation,
  73. walls = new List<Wall>(),
  74. zones = new List<Zone>(),
  75. beacons = new Dictionary<uint, Beacon>(),
  76. plane = plane,
  77. texture_url = ""
  78. };
  79. company.locations[newLocation.id] = newLocation;
  80. company.locations_index.Add(newLocation.id);
  81. DropdownLocation.options.Add(new Dropdown.OptionData("Новая локация"));
  82. DropdownLocation.RefreshShownValue();
  83. }
  84. }
  85. // Update is called once per frame
  86. void Update()
  87. {
  88. if (company.active_location >= 0 && company.locations != null)
  89. {
  90. var l = company.locations[company.locations_index[company.active_location]];
  91. l.plane.GetComponent<TouchScript>().Resize = ToggleScalePanel.isOn;
  92. }
  93. }
  94. string texture_url = "";
  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));
  105. texture_url = Image.text;
  106. }
  107. var x = 1f;
  108. if (!SizeX.text.Equals(""))
  109. x = float.Parse(SizeX.text, CultureInfo.InvariantCulture);
  110. var y = 1f;
  111. if (!SizeY.text.Equals(""))
  112. y = float.Parse(SizeY.text, CultureInfo.InvariantCulture);
  113. p.transform.localScale = new Vector3(x, 1, y);
  114. var scale = p.transform.localScale;
  115. p.transform.position = new Vector3(scale.x * 5, 0, scale.z * 5);
  116. }
  117. public void Save()
  118. {
  119. //SceneManager.LoadScene("Location");
  120. tools.CursorOff();
  121. if (!Name.text.Equals(""))
  122. {
  123. //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};
  124. Location location;
  125. if (EditLocation.id != 0) location = EditLocation;
  126. else location = new Location { id = Convert.ToUInt32(4000000000), plane = plane, location = NewLocation };
  127. location.name = Name.text;
  128. location.texture_url = Image.text;
  129. if (WallCreate.Walls.ContainsKey(EditLocation.id)) location.walls.AddRange(WallCreate.Walls[EditLocation.id]);
  130. //if (WallCreate.Zones.ContainsKey(EditLocation.id)) location.zones.AddRange(WallCreate.Zones[EditLocation.id]);
  131. if (WallCreate.Zones.ContainsKey(EditLocation.id)) location.zones = WallCreate.Zones[EditLocation.id];
  132. if (Beacons.ContainsKey(EditLocation.id))
  133. {
  134. foreach(var b in Beacons[EditLocation.id].Values)
  135. {
  136. var go = b.beacon;
  137. b.x = go.transform.position.x;
  138. b.y = go.transform.position.z;
  139. b.z = go.transform.position.y;
  140. if (location.beacons.ContainsKey(b.id))
  141. location.beacons[b.id] = b;
  142. }
  143. //location.beacons.Add(Beacons[EditLocation.id]);
  144. }
  145. company.DropdownLocation.options.Last().text = location.name;
  146. location.location.name = location.name;
  147. location.Save();
  148. location.SaveContents();
  149. mode.Switch();
  150. }
  151. else
  152. {
  153. Dialog.SetActive(true);
  154. Dialog.transform.GetChild(0).GetComponent<Text>().text = "Введите название локации";
  155. Dialog.transform.GetChild(2).GetComponent<Button>().onClick.AddListener(() =>
  156. {
  157. Dialog.SetActive(false);
  158. });
  159. }
  160. }
  161. public void Back()
  162. {
  163. //SceneManager.LoadScene("Location");
  164. mode.Switch();
  165. }
  166. public void AddBeacon(CompanyController company, Beacon b = null)
  167. {
  168. //PanelBeaconEdit.SetActive(true);
  169. var location = company.locations[company.locations_index[company.active_location]];
  170. var beacon = Instantiate(Resources.Load("GameObjects/Beacon", typeof(GameObject))) as GameObject;
  171. if (ModeController.editor) beacon.GetComponent<BeaconController>().mode = BeaconController.Mode.Editor;
  172. beacon.name = $"Beacon_{b?.id.ToString() ?? Beacons.Count.ToString()}";
  173. if (b != null)
  174. {
  175. //beacon.transform.position = new Vector3(b.x / 100, 0.5f, b.y / 100);
  176. beacon.transform.position = new Vector3(b.x, 0.5f, b.y);
  177. //beacon.transform.GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>().text
  178. beacon.GetComponent<BeaconController>().text_info = $"{b.id}\n{b.vagrant_label}\nuuid={b.uuid}\nmajor={b.major}\nminor={b.minor}\nmac={b.mac}";
  179. beacon.GetComponent<BeaconController>().id = $"{b.id}";
  180. if (b.enabled == false) beacon.GetComponent<Renderer>().material.color = Color.grey;
  181. }
  182. beacon.transform.SetParent(location.location.transform.GetChild(3).transform);
  183. var name_beacon = $"Маяк {b?.id.ToString() ?? Beacons.Count.ToString()}";
  184. PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = name_beacon;
  185. var panel_button = Instantiate(Resources.Load("GameObjects/Panel_Beacon", typeof(GameObject))) as GameObject;
  186. panel_button.transform.SetParent(BeaconsContent.transform);
  187. panel_button.name = $"Beacon_{b?.id.ToString() ?? Beacons.Count.ToString()}";
  188. var button = panel_button.transform.GetChild(0).GetComponent<Button>();
  189. button.name = $"BeaconButton_{Beacons.Count}";
  190. button.transform.GetChild(0).GetComponent<Text>().text = name_beacon;
  191. //var b = new Beacon { };
  192. var shader1 = Shader.Find("Outlined/Silhouetted Bumped Diffuse");
  193. var shader2 = Shader.Find("Standard");
  194. //var text = PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = "";
  195. //var uuid = PanelBeaconEdit.transform.GetChild(1).GetComponent<InputField>().text = "";
  196. if (b == null)
  197. {
  198. b = new Beacon();
  199. b.location_id = location.id;
  200. b.id = Convert.ToUInt32(4000000000 + Beacons[location.id].Count);
  201. b.enabled = true;
  202. BeaconEdit(b, name_beacon);
  203. }
  204. button.onClick.AddListener(() =>
  205. {
  206. BeaconEdit(b, name_beacon);
  207. //b.beacon.GetComponent<Renderer>().material.shader = shader1;
  208. //edit_beacon = b.beacon;
  209. });
  210. var button_ok = PanelBeaconEdit.transform.GetChild(7).transform.GetChild(0).GetComponent<Button>();
  211. button_ok.onClick.RemoveAllListeners();
  212. button_ok.onClick.AddListener(() =>
  213. {
  214. b.uuid = PanelBeaconEdit.transform.GetChild(2).GetComponent<InputField>().text;
  215. b.minor = ushort.Parse(PanelBeaconEdit.transform.GetChild(3).GetComponent<InputField>().text);
  216. b.major = ushort.Parse(PanelBeaconEdit.transform.GetChild(4).GetComponent<InputField>().text);
  217. b.mac = PanelBeaconEdit.transform.GetChild(5).GetComponent<InputField>().text;
  218. b.enabled = PanelBeaconEdit.transform.GetChild(6).GetComponent<Toggle>().isOn;
  219. PanelBeaconEdit.SetActive(false);
  220. //b.beacon.GetComponent<Renderer>().material.shader = shader2;
  221. });
  222. //var button_close = PanelBeaconEdit.transform.GetChild(7).transform.GetChild(1).GetComponent<Button>();
  223. //button_close.onClick.AddListener(() =>
  224. //{
  225. // PanelBeaconEdit.SetActive(false);
  226. // b.beacon.GetComponent<Renderer>().material.shader = shader1;
  227. //});
  228. var button_del = panel_button.transform.GetChild(2).GetComponent<Button>();
  229. button_del.onClick.AddListener(() =>
  230. {
  231. Destroy(beacon);
  232. Destroy(panel_button);
  233. Beacons[location.id].Remove(b.id);
  234. if (PanelBeaconEdit.activeSelf) PanelBeaconEdit.SetActive(false);
  235. });
  236. //b.id = (uint)Beacons.Count;
  237. b.beacon = beacon;
  238. b.panel_button = panel_button;
  239. Beacons[location.id][b.id]=b;
  240. }
  241. void BeaconEdit(Beacon b, string name_beacon)
  242. {
  243. PanelBeaconEdit.SetActive(true);
  244. PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = name_beacon;
  245. if (b != null)
  246. {
  247. PanelBeaconEdit.transform.GetChild(1).GetComponent<InputField>().text = b.uuid;
  248. PanelBeaconEdit.transform.GetChild(2).GetComponent<InputField>().text = b.vagrant_label.ToString();
  249. PanelBeaconEdit.transform.GetChild(3).GetComponent<InputField>().text = $"{b.major}";
  250. PanelBeaconEdit.transform.GetChild(4).GetComponent<InputField>().text = $"{b.major}";
  251. PanelBeaconEdit.transform.GetChild(5).GetComponent<InputField>().text = b.mac;
  252. PanelBeaconEdit.transform.GetChild(6).GetComponent<Toggle>().isOn = b.enabled;
  253. }
  254. }
  255. //GameObject edit_beacon;
  256. public void CloseBeaconParameters()
  257. {
  258. PanelBeaconEdit.SetActive(false);
  259. //var shader2 = Shader.Find("Standard");
  260. //edit_beacon.GetComponent<Renderer>().material.shader = shader2;
  261. }
  262. }