EditorController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 Toggle projection;
  26. public Dropdown DropdownLocation;
  27. public Transform Locations;
  28. public Button ButtonAddBeacon;
  29. public GameObject Dialog;
  30. public GameObject NewLocation;
  31. public GameObject plane;
  32. public Location newLocation;
  33. public Dictionary<uint, List<Beacon>> Beacons = new Dictionary<uint, List<Beacon>>();
  34. CompanyController company;
  35. ModeController mode;
  36. ToolsController tools;
  37. public Location EditLocation;
  38. private void Awake()
  39. {
  40. instance = this;
  41. }
  42. // Start is called before the first frame update
  43. void Start()
  44. {
  45. company = CompanyController.instance;
  46. mode = ButtonMode.GetComponent<ModeController>();
  47. tools = EditorTools.GetComponent<ToolsController>();
  48. ButtonAddBeacon.onClick.AddListener(() => {
  49. AddBeacon(company);
  50. });
  51. var random = new System.Random();
  52. int number = random.Next(2000000000, Int32.MaxValue);
  53. uint uintNumber = (uint)(number + (uint)Int32.MaxValue);
  54. if (NewLocation == null)
  55. {
  56. NewLocation = new GameObject();
  57. NewLocation.transform.SetParent(Locations);
  58. NewLocation.name = "NewLocation";
  59. NewLocation.transform.position = Vector3.zero;
  60. NewLocation.SetActive(false);
  61. plane = Instantiate(Resources.Load("GameObjects/Plane", typeof(GameObject))) as GameObject;
  62. plane.transform.SetParent(NewLocation.transform);
  63. plane.transform.position = new Vector3(plane.transform.localScale.x * 5, 0, plane.transform.localScale.z * 5);
  64. foreach (var g in Location.contents)
  65. {
  66. var go = new GameObject();
  67. go.name = g;
  68. go.transform.SetParent(NewLocation.transform);
  69. }
  70. newLocation = new Location
  71. {
  72. id = uintNumber,
  73. location = NewLocation,
  74. walls = new List<Wall>(),
  75. zones = new List<Zone>(),
  76. beacons = new List<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 (projection.isOn)
  90. {
  91. Camera.main.orthographic = true;
  92. }
  93. else
  94. {
  95. Camera.main.orthographic = false;
  96. }
  97. }
  98. string texture_url = "";
  99. /// <summary>
  100. /// Изменение размеров панели
  101. /// Загрузка текстуры
  102. /// </summary>
  103. public void UpdatePanel()
  104. {
  105. var p = company.locations[company.locations_index[company.active_location]].plane;
  106. if (!Image.text.Equals(texture_url))
  107. {
  108. StartCoroutine(SendingFormController.LoadImage(Image.text, p));
  109. texture_url = Image.text;
  110. }
  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 Save()
  122. {
  123. //SceneManager.LoadScene("Location");
  124. tools.CursorOff();
  125. if (!Name.text.Equals(""))
  126. {
  127. //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};
  128. Location location;
  129. if (EditLocation.id != 0) location = EditLocation;
  130. else location = new Location { id = Convert.ToUInt32(4000000000), plane = plane, location = NewLocation };
  131. location.name = Name.text;
  132. location.texture_url = Image.text;
  133. if (WallCreate.Walls.ContainsKey(EditLocation.id)) location.walls.AddRange(WallCreate.Walls[EditLocation.id]);
  134. //if (WallCreate.Zones.ContainsKey(EditLocation.id)) location.zones.AddRange(WallCreate.Zones[EditLocation.id]);
  135. if (WallCreate.Zones.ContainsKey(EditLocation.id)) location.zones = WallCreate.Zones[EditLocation.id];
  136. if (Beacons.ContainsKey(EditLocation.id))
  137. {
  138. foreach(var b in Beacons[EditLocation.id])
  139. {
  140. var go = b.beacon;
  141. b.x = go.transform.position.x;
  142. b.y = go.transform.position.z;
  143. b.z = go.transform.position.y;
  144. }
  145. location.beacons.AddRange(Beacons[EditLocation.id]);
  146. }
  147. company.DropdownLocation.options.Last().text = location.name;
  148. location.location.name = location.name;
  149. location.Save();
  150. location.SaveContents();
  151. mode.Switch();
  152. }
  153. else
  154. {
  155. Dialog.SetActive(true);
  156. Dialog.transform.GetChild(0).GetComponent<Text>().text = "Введите название локации";
  157. Dialog.transform.GetChild(2).GetComponent<Button>().onClick.AddListener(() =>
  158. {
  159. Dialog.SetActive(false);
  160. });
  161. }
  162. }
  163. public void Back()
  164. {
  165. //SceneManager.LoadScene("Location");
  166. mode.Switch();
  167. }
  168. public void AddBeacon(CompanyController company, Beacon b = null)
  169. {
  170. //PanelBeaconEdit.SetActive(true);
  171. var location = company.locations[company.locations_index[company.active_location]];
  172. var beacon = Instantiate(Resources.Load("GameObjects/Beacon", typeof(GameObject))) as GameObject;
  173. if (ModeController.editor) beacon.GetComponent<BeaconController>().mode = BeaconController.Mode.Editor;
  174. beacon.name = $"Beacon_{b?.id.ToString() ?? Beacons.Count.ToString()}";
  175. if (b != null)
  176. {
  177. //beacon.transform.position = new Vector3(b.x / 100, 0.5f, b.y / 100);
  178. beacon.transform.position = new Vector3(b.x, 0.5f, b.y);
  179. //beacon.transform.GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>().text
  180. beacon.GetComponent<BeaconController>().text_info = $"{b.id}\n{b.vagrant_label}\nuuid={b.uuid}\nmajor={b.major}\nminor={b.minor}\nmac={b.mac}";
  181. beacon.GetComponent<BeaconController>().id = $"{b.id}";
  182. if (b.enabled == false) beacon.GetComponent<Renderer>().material.color = Color.grey;
  183. }
  184. beacon.transform.SetParent(location.location.transform.GetChild(3).transform);
  185. var name_beacon = $"Маяк {b?.id.ToString() ?? Beacons.Count.ToString()}";
  186. PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = name_beacon;
  187. var panel_button = Instantiate(Resources.Load("GameObjects/Panel_Beacon", typeof(GameObject))) as GameObject;
  188. panel_button.transform.SetParent(BeaconsContent.transform);
  189. panel_button.name = $"Beacon_{b?.id.ToString() ?? Beacons.Count.ToString()}";
  190. var button = panel_button.transform.GetChild(0).GetComponent<Button>();
  191. button.name = $"BeaconButton_{Beacons.Count}";
  192. button.transform.GetChild(0).GetComponent<Text>().text = name_beacon;
  193. //var b = new Beacon { };
  194. var shader1 = Shader.Find("Outlined/Silhouetted Bumped Diffuse");
  195. var shader2 = Shader.Find("Standard");
  196. button.onClick.AddListener(() =>
  197. {
  198. BeaconEdit(b, name_beacon);
  199. b.beacon.GetComponent<Renderer>().material.shader = shader1;
  200. });
  201. //var text = PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = "";
  202. //var uuid = PanelBeaconEdit.transform.GetChild(1).GetComponent<InputField>().text = "";
  203. if (b == null)
  204. {
  205. b = new Beacon();
  206. b.location_id = location.id;
  207. b.id = Convert.ToUInt32(4000000000 + Beacons[location.id].Count);
  208. b.enabled = true;
  209. BeaconEdit(b, name_beacon);
  210. }
  211. var button_ok = PanelBeaconEdit.transform.GetChild(7).transform.GetChild(0).GetComponent<Button>();
  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. });
  221. var button_close = PanelBeaconEdit.transform.GetChild(7).transform.GetChild(1).GetComponent<Button>();
  222. button_close.onClick.AddListener(() =>
  223. {
  224. PanelBeaconEdit.SetActive(false);
  225. b.beacon.GetComponent<Renderer>().material.shader = shader2;
  226. });
  227. var button_del = panel_button.transform.GetChild(2).GetComponent<Button>();
  228. button_del.onClick.AddListener(() =>
  229. {
  230. Destroy(beacon);
  231. Destroy(panel_button);
  232. Beacons[location.id].Remove(b);
  233. if (PanelBeaconEdit.activeSelf) PanelBeaconEdit.SetActive(false);
  234. });
  235. //b.id = (uint)Beacons.Count;
  236. b.beacon = beacon;
  237. b.panel_button = panel_button;
  238. if (!Beacons.ContainsKey(location.id))
  239. Beacons[location.id] = new List<Beacon>();
  240. Beacons[location.id].Add(b);
  241. }
  242. void BeaconEdit(Beacon b, string name_beacon)
  243. {
  244. PanelBeaconEdit.SetActive(true);
  245. PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = name_beacon;
  246. if (b != null)
  247. {
  248. PanelBeaconEdit.transform.GetChild(1).GetComponent<InputField>().text = b.uuid;
  249. PanelBeaconEdit.transform.GetChild(2).GetComponent<InputField>().text = b.vagrant_label.ToString();
  250. PanelBeaconEdit.transform.GetChild(3).GetComponent<InputField>().text = $"{b.major}";
  251. PanelBeaconEdit.transform.GetChild(4).GetComponent<InputField>().text = $"{b.major}";
  252. PanelBeaconEdit.transform.GetChild(5).GetComponent<InputField>().text = b.mac;
  253. PanelBeaconEdit.transform.GetChild(6).GetComponent<Toggle>().isOn = b.enabled;
  254. }
  255. }
  256. }