WallCreate.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. public class WallCreate : MonoBehaviour
  9. {
  10. public static WallCreate instance;
  11. public static float WallHeight = 1f;
  12. public float WallThickness = 0.05f;
  13. public int Rounding = 1;
  14. public bool move = false;
  15. public bool BoxColliderEnabled = true;
  16. public Dialog Dialog;
  17. public GameObject ZonesScroll;
  18. public GameObject Player;
  19. public Button redo;
  20. public Button undo; // отмена
  21. public List<History> histories = new List<History>();
  22. public enum Mode { Wall, Room, Zone, Pole, Cursor };
  23. public static Mode mode = Mode.Cursor;
  24. GameObject wall;
  25. Wall currentWall;
  26. //public Dictionary<uint, GameObject> WallObjects = new Dictionary<uint, GameObject>();
  27. public static Dictionary<uint, List<Wall>> Walls = new Dictionary<uint, List<Wall>>();
  28. //public static List<Wall> Rooms = new List<Wall>();
  29. public static Dictionary<uint, List<Zone>> Zones = new Dictionary<uint, List<Zone>>();
  30. private Vector3 screenPoint;
  31. private Vector3 offset;
  32. private Vector3 curScreenPoint;
  33. private Vector3 curPosition;
  34. private Vector3 start_pos;
  35. Vector3 Point;
  36. GameObject canvas;
  37. Text text;
  38. CompanyController company;
  39. private void Awake()
  40. {
  41. instance = this;
  42. }
  43. // Start is called before the first frame update
  44. void Start()
  45. {
  46. // gameObject.GetComponent<BoxCollider>().enabled = BoxColliderEnabled;
  47. //cursor_end = transform.GetChild(6).gameObject;
  48. Point = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  49. canvas = transform.GetChild(0).gameObject;
  50. text = transform.GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>();
  51. company = CompanyController.instance;
  52. undo.onClick.AddListener(Undo);
  53. }
  54. // Update is called once per frame
  55. void Update()
  56. {
  57. // if (gameObject.GetComponent<BoxCollider>().enabled != BoxColliderEnabled) gameObject.GetComponent<BoxCollider>().enabled = BoxColliderEnabled;
  58. if (!Input.GetMouseButtonDown(0))
  59. {
  60. transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Point.z));
  61. if (transform.position.y != 0.5f) transform.position = new Vector3(transform.position.x, 0.5f, transform.position.z);
  62. }
  63. }
  64. void OnMouseDown()
  65. {
  66. if (!EventSystem.current.IsPointerOverGameObject())
  67. {
  68. //Debug.Log(StagesEditorController.indexCursor);
  69. screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  70. //s_point = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  71. start_pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  72. offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  73. switch (mode)
  74. {
  75. case Mode.Wall:
  76. wall = AddWall(company, Vector3.zero);
  77. EventHistory(0, wall, null);
  78. break;
  79. case Mode.Room:
  80. wall = AddRoom();
  81. break;
  82. case Mode.Zone:
  83. wall = AddZone(Dialog, company, ZonesScroll);
  84. var location_id = company.locations_index[company.active_location];
  85. EventHistory(0, wall, Zones[location_id].Last().buttons);
  86. break;
  87. }
  88. }
  89. canvas.SetActive(true);
  90. }
  91. float scale_x;
  92. float scale_z;
  93. void OnMouseDrag()
  94. {
  95. if (!EventSystem.current.IsPointerOverGameObject())
  96. {
  97. curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  98. // e_point = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  99. curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
  100. var end_pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  101. //Debug.Log("start_pos.x "+start_pos.x);
  102. scale_x = (float)Math.Round(end_pos.x - start_pos.x, Rounding);
  103. scale_z = (float)Math.Round(end_pos.z - start_pos.z, Rounding);
  104. text.text = $"x={scale_x} y={scale_z}";
  105. switch (mode)
  106. {
  107. case Mode.Wall:
  108. if (Math.Abs(scale_x) > Math.Abs(scale_z)/* && x_mod >= 0.4f*/)
  109. {
  110. wall.transform.localScale = new Vector3(scale_x, WallHeight, WallThickness);
  111. wall.transform.position = new Vector3(scale_x / 2 + start_pos.x, WallHeight / 2, start_pos.z);
  112. }
  113. if (Math.Abs(scale_z) > Math.Abs(scale_x)/* && z_mod >= 0.4f*/)
  114. {
  115. wall.transform.localScale = new Vector3(WallThickness, WallHeight, scale_z);
  116. wall.transform.position = new Vector3(start_pos.x, WallHeight / 2, scale_z / 2 + start_pos.z);
  117. }
  118. break;
  119. case Mode.Room:
  120. /*if (scale_x > 0 && scale_z > 0) */
  121. LocationController.Cube(wall, new Vector3(scale_x, scale_z, WallHeight),
  122. new Vector2((float)Math.Round(start_pos.x, Rounding), (float)Math.Round(start_pos.z, Rounding)), WallThickness);
  123. break;
  124. //case Mode.Pole:
  125. // AddWall(pos);
  126. // break;
  127. case Mode.Zone:
  128. wall.transform.localScale = new Vector3(scale_x/10, 1, scale_z/10);
  129. wall.transform.position = new Vector3(scale_x / 2 + start_pos.x, 0.05f, scale_z / 2 + start_pos.z);
  130. break;
  131. }
  132. float offsetPosY = transform.position.y + 1.5f;
  133. // Final position of marker above GO in world space
  134. canvas.transform.GetChild(0).transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y+20, screenPoint.z);
  135. }
  136. }
  137. private void OnMouseUp()
  138. {
  139. //var end_pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  140. //Debug.Log($"end_pos = {end_pos}");
  141. var location_id = company.locations_index[company.active_location];
  142. if (!Walls.ContainsKey(location_id))
  143. Walls[location_id] = new List<Wall>();
  144. if (mode == Mode.Room)
  145. {
  146. for (int i = 0; i < wall.transform.childCount; i++)
  147. {
  148. var w = wall.transform.GetChild(i);
  149. if(w.gameObject.activeSelf == true)
  150. {
  151. currentWall = new Wall { id = Convert.ToUInt32(4000000000 + Walls[location_id].Count),location_id = location_id, start_width = w.position.x, start_height = w.position.z, end_width = w.localScale.x, end_height = w.localScale.z, go = wall };
  152. Walls[location_id].Add(currentWall);
  153. }
  154. }
  155. }
  156. //if (mode == Mode.Room) Rooms.Add(currentWall);
  157. if (mode == Mode.Wall)
  158. {
  159. currentWall = new Wall { id =Convert.ToUInt32(4000000000 + Walls[location_id].Count), start_width = wall.transform.position.x, start_height = wall.transform.position.z, end_width = wall.transform.localScale.x, end_height = wall.transform.localScale.z, go = wall, location_id=location_id };
  160. Debug.Log($"new wall save id {currentWall.id} x {currentWall.start_width} z {currentWall.start_height} x2 {currentWall.end_width} z2 {currentWall.end_height} location_id {currentWall.location_id}");
  161. Walls[location_id].Add(currentWall);
  162. }
  163. /*if (mode == Mode.Wall || mode == Mode.Room)*/ /*Walls.Add(currentWall);*/
  164. canvas.SetActive(false);
  165. if (mode == Mode.Zone && !EventSystem.current.IsPointerOverGameObject())
  166. {
  167. Dialog.GetComponent<Dialog>().Text.gameObject.SetActive(false);
  168. Dialog.GetComponent<Dialog>().InputField.gameObject.SetActive(true);
  169. Dialog.GetComponent<Dialog>().Button.onClick.AddListener(() =>
  170. {
  171. wall.GetComponent<ZoneController>().TextMesh.text=Dialog.GetComponent<Dialog>().InputField.text;
  172. });
  173. var z = Zones[location_id][last_zone];
  174. z.start_width = wall.transform.position.x;
  175. z.start_height = wall.transform.position.z;
  176. z.end_width = wall.transform.localScale.x;
  177. z.end_height = wall.transform.localScale.z;
  178. Debug.Log($"z {z.id} z.start_width {z.start_width} z.start_height {z.start_height}");
  179. //Zones[location_id].Insert(last_zone, z);
  180. }
  181. }
  182. public static GameObject AddWall(CompanyController company, Vector3 pos, Vector3 scale = default)
  183. {
  184. var wall = Instantiate(Resources.Load("GameObjects/Wall", typeof(GameObject))) as GameObject;
  185. wall.transform.position = pos;
  186. if (scale != null) wall.transform.localScale = scale;
  187. var location = company.locations[company.locations_index[company.active_location]].location;
  188. wall.transform.SetParent(location.transform.GetChild(1).transform);
  189. //WallObjects.Add(wall);
  190. return wall;
  191. }
  192. GameObject AddRoom()
  193. {
  194. var room = Instantiate(Resources.Load("GameObjects/Cube", typeof(GameObject))) as GameObject;
  195. var location = company.locations[company.locations_index[company.active_location]].location;
  196. room.transform.SetParent(location.transform.GetChild(1).transform);
  197. LocationController.Cube(room, new Vector3(0.1f, 0.1f, 1));
  198. //for (var i = 0; i < room.transform.childCount; i++)
  199. // if (room.transform.GetChild(i).gameObject.activeSelf == true)
  200. // WallObjects.Add(room.transform.GetChild(i).gameObject);
  201. return room;
  202. }
  203. static int last_zone;
  204. public static GameObject AddZone(Dialog Dialog, CompanyController company, GameObject ZonesScroll, Zone z = null)
  205. {
  206. var location = company.locations[company.locations_index[company.active_location]].location;
  207. var location_id = company.locations_index[company.active_location];
  208. if (!Zones.ContainsKey(location_id)) Zones[location_id] = new List<Zone>();
  209. var zone = Instantiate(Resources.Load("GameObjects/Zone", typeof(GameObject))) as GameObject;
  210. zone.name = $"Zone_{Zones[location_id].Count + 1}";
  211. var zname = $"Зона {Zones[location_id].Count + 1}";
  212. if (z != null) zname = z.name;
  213. //zone.transform.position = pos;
  214. zone.transform.SetParent(location.transform.GetChild(2).transform);
  215. var panel_button = Instantiate(Resources.Load("GameObjects/Panel_Beacon", typeof(GameObject))) as GameObject;
  216. panel_button.transform.SetParent(ZonesScroll.transform);
  217. var button = panel_button.transform.GetChild(0).GetComponent<Button>();
  218. button.transform.GetChild(0).GetComponent<Text>().text = zname;
  219. var tm = zone.GetComponent<ZoneController>().TextMesh;
  220. tm.text = zname;
  221. //tm.transform.parent = null;
  222. //tm.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
  223. //tm.transform.parent=zone.transform;
  224. var active_zone = Zones[location_id].Count;
  225. panel_button.name = $"Zone_{active_zone}";
  226. if(z == null) z = new Zone { id= Convert.ToUInt32(4000000000 + Zones[location_id].Count), location_id= location_id };
  227. else
  228. {
  229. zone.transform.position = new Vector3(Mathf.Abs(z.start_width), 0.05f, Mathf.Abs(z.start_height));
  230. zone.transform.localScale = new Vector3(Mathf.Abs(z.end_width), 1, Mathf.Abs(z.end_height));
  231. zone.name = $"Zone_{z.id}";
  232. company.LocationZones.AddZone(z);
  233. }
  234. z.buttons = panel_button;
  235. z.go = zone;
  236. var button_ok = Dialog.transform.GetChild(2).GetComponent<Button>();
  237. button.onClick.AddListener(() =>
  238. {
  239. Dialog.gameObject.SetActive(true);
  240. //active_zone = Zones[location_id].Count;
  241. button_ok.onClick.RemoveAllListeners();
  242. Dialog.Text.gameObject.SetActive(false);
  243. Dialog.InputField.gameObject.SetActive(true);
  244. Dialog.InputField.text = !z.name.Equals("") ? z.name : $"Зона {active_zone + 1}";
  245. button_ok.onClick.AddListener(() =>
  246. {
  247. var name = !Dialog.GetComponent<Dialog>().InputField.text.Equals("") ? Dialog.GetComponent<Dialog>().InputField.text : $"Зона {active_zone + 1}";
  248. button.transform.GetChild(0).GetComponent<Text>().text = name;
  249. zone.GetComponent<ZoneController>().TextMesh.text = name;
  250. Dialog.gameObject.SetActive(false);
  251. z.name = name;
  252. //Zones[location_id].Insert(last_zone, z);
  253. });
  254. });
  255. var button_del = panel_button.transform.GetChild(2).GetComponent<Button>();
  256. button_del.onClick.AddListener(() =>
  257. {
  258. Destroy(zone);
  259. Destroy(panel_button);
  260. Zones[location_id].Remove(z);
  261. });
  262. Zones[location_id].Add(z);
  263. last_zone = Zones[location_id].Count - 1;
  264. return zone;
  265. }
  266. //public void SaveZones()
  267. //{
  268. // foreach(var z in Zones)
  269. // {
  270. // z.Save();
  271. // }
  272. //}
  273. static uint WallGetInnerID(uint location_id, int num)
  274. {
  275. var wall = Walls[location_id][num];
  276. return wall.id;
  277. }
  278. static uint ZoneGetInnerID(uint location_id, int num)
  279. {
  280. var zone = Zones[location_id][num];
  281. return zone.id;
  282. }
  283. void Undo()
  284. {
  285. var last = histories.Last();
  286. last.obj.SetActive(false);
  287. if(last.ButtonPanel != null) last.ButtonPanel.SetActive(false);
  288. last.status = false;
  289. }
  290. public void EventHistory(uint id, GameObject gameObject, GameObject panel)
  291. {
  292. histories.Add(new History { id = id, obj = gameObject, ButtonPanel = panel, status = true });
  293. }
  294. }