ToolsController.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. using SimpleFileBrowser;
  9. public class ToolsController : MonoBehaviour
  10. {
  11. public GameObject Cursor;
  12. public int Rounding = 1;
  13. public Button Mouse;
  14. public Button Hammer;
  15. public Button Undo;
  16. public Button Redo;
  17. public Button ButtonGrid;
  18. public Button ButtonBeacon;
  19. public Button ButtonZone;
  20. public Button ButtonBuild;
  21. public GameObject GroundSettings;
  22. public GameObject Grid;
  23. public GameObject BuildTools;
  24. public GameObject ZonesScroll;
  25. public GameObject BeaconsScroll;
  26. Button old_btn_tools;
  27. Button old_btn_build;
  28. GameObject old_panel;
  29. bool cursor_active;
  30. bool ground_active;
  31. enum Tools { Wall, Room, Del, Cursor};
  32. // Tools tools = Tools.Cursor;
  33. // Start is called before the first frame update
  34. void Start()
  35. {
  36. }
  37. // Update is called once per frame
  38. void Update()
  39. {
  40. if (EventSystem.current.IsPointerOverGameObject())
  41. {
  42. if (cursor_active) Cursor.SetActive(!cursor_active);
  43. }
  44. else if (cursor_active) Cursor.SetActive(cursor_active);
  45. if (ground_active != GroundSettings.activeSelf) GroundSettings.SetActive(ground_active);
  46. }
  47. public void CursorOff()
  48. {
  49. cursor_active=false;
  50. WallCreate.mode = WallCreate.Mode.Cursor;
  51. }
  52. public void WallEdit()
  53. {
  54. cursor_active = true;
  55. WallCreate.mode = WallCreate.Mode.Wall;
  56. ground_active = false;
  57. }
  58. public void RoomEdit()
  59. {
  60. cursor_active = true;
  61. WallCreate.mode = WallCreate.Mode.Room;
  62. ground_active = false;
  63. }
  64. public void BuildEdit()
  65. {
  66. SelectButton(ButtonBuild, old_btn_build);
  67. if (old_panel != null) old_panel.SetActive(false);
  68. BuildTools.SetActive(true);
  69. old_panel = BuildTools;
  70. old_btn_build = ButtonBuild;
  71. }
  72. public void GroundEdit()
  73. {
  74. if (cursor_active)
  75. {
  76. Cursor.SetActive(false);
  77. cursor_active = false;
  78. }
  79. ground_active = true;
  80. }
  81. public void ZoneEdit()
  82. {
  83. cursor_active = true;
  84. SelectButton(ButtonZone, old_btn_build);
  85. WallCreate.mode = WallCreate.Mode.Zone;
  86. if (old_panel != null) old_panel.SetActive(false);
  87. ZonesScroll.SetActive(true);
  88. old_panel = ZonesScroll;
  89. old_btn_build = ButtonZone;
  90. ground_active = false;
  91. }
  92. public void BeaconEdit()
  93. {
  94. if (cursor_active)
  95. {
  96. Cursor.SetActive(false);
  97. cursor_active = false;
  98. }
  99. SelectButton(ButtonBeacon,old_btn_build);
  100. if (old_panel != null) old_panel.SetActive(false);
  101. BeaconsScroll.SetActive(true);
  102. old_panel = BeaconsScroll;
  103. old_btn_build = ButtonBeacon;
  104. ground_active = false;
  105. }
  106. bool grid_active = false;
  107. public void GridOnOff()
  108. {
  109. grid_active = !grid_active;
  110. Grid.SetActive(grid_active);
  111. if(grid_active) ButtonGrid.GetComponent<Image>().color = Color.gray;
  112. else ButtonGrid.GetComponent<Image>().color = Color.white;
  113. }
  114. public void Delete()
  115. {
  116. //tools = Tools.Del;
  117. }
  118. public void SelectButton(GameObject button)
  119. {
  120. GameObject old_button = null;
  121. switch (WallCreate.mode)
  122. {
  123. case WallCreate.Mode.Cursor:
  124. old_button = transform.GetChild(0).gameObject;
  125. break;
  126. case WallCreate.Mode.Room:
  127. old_button = transform.GetChild(1).gameObject;
  128. break;
  129. case WallCreate.Mode.Wall:
  130. old_button = transform.GetChild(2).gameObject;
  131. break;
  132. case WallCreate.Mode.Zone:
  133. old_button = transform.GetChild(3).gameObject;
  134. break;
  135. }
  136. if (old_button != null) old_button.GetComponent<Image>().color = Color.white;
  137. button.GetComponent<Image>().color = Color.gray;
  138. }
  139. public static void SelectButton(Button button, Button old_button)
  140. {
  141. var color = new Color();
  142. ColorUtility.TryParseHtmlString("#C8C8C8", out color);
  143. if (old_button != null) old_button.GetComponent<Image>().color = Color.white;
  144. button.GetComponent<Image>().color = color;
  145. }
  146. public void mouse()
  147. {
  148. SelectButton(Mouse, old_btn_tools);
  149. old_btn_tools = Mouse;
  150. }
  151. public void hammer()
  152. {
  153. SelectButton(Hammer, old_btn_tools);
  154. old_btn_tools = Hammer;
  155. }
  156. public void undo()
  157. {
  158. SelectButton(Undo, old_btn_tools);
  159. old_btn_tools = Undo;
  160. }
  161. public void redo()
  162. {
  163. SelectButton(Redo, old_btn_tools);
  164. old_btn_tools = Redo;
  165. }
  166. public void OpenFileBrowser()
  167. {
  168. StartCoroutine(ShowLoadDialogCoroutine($"Выбор карты"));
  169. }
  170. IEnumerator ShowLoadDialogCoroutine(string name_view)
  171. {
  172. FileBrowser.SetFilters(true, new FileBrowser.Filter("Images", ".jpg", ".png"));
  173. FileBrowser.SetDefaultFilter(".jpg");
  174. //FileBrowser.AddQuickLink("Users", "C:\\Users", null);
  175. // Show a load file dialog and wait for a response from user
  176. // Load file/folder: file, Initial path: default (Documents), Title: "Load File", submit button text: "Load"
  177. yield return FileBrowser.WaitForLoadDialog(false, null, name_view, "Load");
  178. // Dialog is closed
  179. // Print whether a file is chosen (FileBrowser.Success)
  180. // and the path to the selected file (FileBrowser.Result) (null, if FileBrowser.Success is false)
  181. Debug.Log(FileBrowser.Success + " " + FileBrowser.Result);
  182. if (FileBrowser.Success)
  183. {
  184. var go = GameObject.Find("InputField_Image");
  185. if (go != null)
  186. {
  187. var ifield = go.GetComponent<InputField>();
  188. if (ifield != null)
  189. {
  190. ifield.text = FileBrowser.Result;
  191. }
  192. }
  193. // If a file was chosen, read its bytes via FileBrowserHelpers
  194. // Contrary to File.ReadAllBytes, this function works on Android 10+, as well
  195. byte[] bytes = FileBrowserHelpers.ReadBytesFromFile(FileBrowser.Result);
  196. var loc = CompanyController.instance.GetActiveLocation();
  197. if (loc != null)
  198. {
  199. loc.SaveTexture(bytes);
  200. }
  201. }
  202. }
  203. }