SnapSettingsWindow.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Copyright (c) 2021 Omar Duarte
  3. Unauthorized copying of this file, via any medium is strictly prohibited.
  4. Writen by Omar Duarte, 2021.
  5. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  6. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  7. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  8. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  9. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  10. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  11. THE SOFTWARE.
  12. */
  13. using UnityEngine;
  14. namespace PluginMaster
  15. {
  16. public class SnapSettingsWindow : UnityEditor.EditorWindow
  17. {
  18. private static readonly string[] _gridTypeOptions = { "Rectangular", "Radial" };
  19. private static SnapSettingsWindow _instance = null;
  20. [UnityEditor.MenuItem("Tools/Plugin Master/Prefab World Builder/Grid and Snapping Settings...", false, 1150)]
  21. public static void ShowWindow() => _instance = GetWindow<SnapSettingsWindow>("Grid and Snapping Settings");
  22. private GameObject _activeGameObject = null;
  23. public static void RepaintWindow()
  24. {
  25. if (_instance != null) _instance.Repaint();
  26. }
  27. private void OnEnable()
  28. {
  29. _activeGameObject = UnityEditor.Selection.activeGameObject;
  30. }
  31. private void OnGUI()
  32. {
  33. minSize = new Vector2(350, SnapManager.settings.radialGridEnabled ? 290 : 310);
  34. using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
  35. {
  36. using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
  37. {
  38. if (SnapManager.settings.radialGridEnabled)
  39. {
  40. SnapManager.settings.radialStep = UnityEditor.EditorGUILayout.FloatField("Radial Snap Value:",
  41. SnapManager.settings.radialStep);
  42. }
  43. else
  44. {
  45. SnapManager.settings.step = UnityEditor.EditorGUILayout.Vector3Field("Snap Value:",
  46. SnapManager.settings.step);
  47. SnapManager.settings.midpointSnapping = UnityEditor.EditorGUILayout.ToggleLeft("Midpoint snapping",
  48. SnapManager.settings.midpointSnapping);
  49. }
  50. if (check.changed) UnityEditor.SceneView.RepaintAll();
  51. }
  52. if (!SnapManager.settings.radialGridEnabled)
  53. {
  54. using (new UnityEditor.EditorGUI.DisabledGroupScope(_activeGameObject == null))
  55. {
  56. if (GUILayout.Button("Set the snap value to the size of the active gameobject"))
  57. {
  58. var bounds = BoundsUtils.GetBounds(_activeGameObject.transform);
  59. SnapManager.settings.step = bounds.size;
  60. UnityEditor.SceneView.RepaintAll();
  61. }
  62. }
  63. }
  64. }
  65. if (SnapManager.settings.radialGridEnabled)
  66. {
  67. using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
  68. {
  69. using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
  70. {
  71. SnapManager.settings.radialSectors = UnityEditor.EditorGUILayout.IntField("Radial Sectors:",
  72. SnapManager.settings.radialSectors);
  73. if (check.changed) UnityEditor.SceneView.RepaintAll();
  74. }
  75. }
  76. }
  77. using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
  78. {
  79. using (new GUILayout.HorizontalScope(UnityEditor.EditorStyles.helpBox))
  80. {
  81. int originIndex = 0;
  82. UnityEditor.EditorGUIUtility.labelWidth = 70;
  83. using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
  84. {
  85. originIndex = UnityEditor.EditorGUILayout.Popup("Grid origin:",
  86. SnapManager.settings.GetIndexOfSelectedOrigin(), SnapManager.settings.GetOriginNames());
  87. if (check.changed)
  88. {
  89. SnapManager.settings.SelectOrigin(originIndex);
  90. UnityEditor.SceneView.RepaintAll();
  91. }
  92. }
  93. if (GUILayout.Button("Reset"))
  94. {
  95. SnapManager.settings.ResetOrigin();
  96. }
  97. if (GUILayout.Button("Save..."))
  98. {
  99. RenameWindow.ShowWindow(position.position + Event.current.mousePosition,
  100. SnapManager.settings.SaveGridOrigin, "Save Origin", SnapManager.settings.selectedOrigin);
  101. }
  102. using (new UnityEditor.EditorGUI.DisabledGroupScope(originIndex == 0))
  103. {
  104. if (GUILayout.Button("Delete"))
  105. {
  106. SnapManager.settings.DeleteSelectedOrigin();
  107. }
  108. }
  109. }
  110. using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
  111. {
  112. using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
  113. {
  114. SnapManager.settings.origin = UnityEditor.EditorGUILayout.Vector3Field("Origin position:",
  115. SnapManager.settings.origin);
  116. if (check.changed) UnityEditor.SceneView.RepaintAll();
  117. }
  118. using (new UnityEditor.EditorGUI.DisabledGroupScope(_activeGameObject == null))
  119. {
  120. if (GUILayout.Button("Set the origin to the active gameobject position"))
  121. {
  122. SnapManager.settings.origin = _activeGameObject.transform.position;
  123. UnityEditor.SceneView.RepaintAll();
  124. }
  125. }
  126. }
  127. using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
  128. {
  129. using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
  130. {
  131. var euler = SnapManager.settings.rotation.eulerAngles;
  132. euler = new Vector3(Mathf.RoundToInt(euler.x * 100000) / 100000f,
  133. Mathf.RoundToInt(euler.y * 100000) / 100000f,
  134. Mathf.RoundToInt(euler.z * 100000) / 100000f);
  135. SnapManager.settings.rotation
  136. = Quaternion.Euler(UnityEditor.EditorGUILayout.Vector3Field("Rotation", euler));
  137. if (check.changed) UnityEditor.SceneView.RepaintAll();
  138. }
  139. using (new UnityEditor.EditorGUI.DisabledGroupScope(_activeGameObject == null))
  140. {
  141. if (GUILayout.Button("Set the rotation to the active gameobject rotation"))
  142. {
  143. SnapManager.settings.rotation = _activeGameObject.transform.rotation;
  144. UnityEditor.SceneView.RepaintAll();
  145. }
  146. }
  147. }
  148. }
  149. if (!SnapManager.settings.radialGridEnabled)
  150. {
  151. using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
  152. {
  153. using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
  154. {
  155. SnapManager.settings.majorLinesGap
  156. = UnityEditor.EditorGUILayout.Vector3IntField("Major lines every Nth grid line:",
  157. SnapManager.settings.majorLinesGap);
  158. if (check.changed) UnityEditor.SceneView.RepaintAll();
  159. }
  160. }
  161. }
  162. using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
  163. {
  164. using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
  165. {
  166. var idx = SnapManager.settings.radialGridEnabled ? 1 : 0;
  167. idx = UnityEditor.EditorGUILayout.Popup("Grid type:", idx, _gridTypeOptions);
  168. if (check.changed)
  169. {
  170. SnapManager.settings.radialGridEnabled = idx == 0 ? false : true;
  171. PWBToolbar.RepaintWindow();
  172. }
  173. }
  174. using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
  175. {
  176. SnapManager.settings.lockedGrid = UnityEditor.EditorGUILayout.ToggleLeft("Lock the grid origin in place",
  177. SnapManager.settings.lockedGrid);
  178. if (check.changed) PWBToolbar.RepaintWindow();
  179. }
  180. using (new UnityEditor.EditorGUI.DisabledGroupScope(!SnapManager.settings.lockedGrid))
  181. {
  182. using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
  183. {
  184. var showPositionHandle = UnityEditor.EditorGUILayout.ToggleLeft("Show position handle",
  185. SnapManager.settings.showPositionHandle);
  186. if (check.changed) SnapManager.settings.showPositionHandle = showPositionHandle;
  187. }
  188. using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
  189. {
  190. var showRotationHandle = UnityEditor.EditorGUILayout.ToggleLeft("Show rotation handle",
  191. SnapManager.settings.showRotationHandle);
  192. if (check.changed) SnapManager.settings.showRotationHandle = showRotationHandle;
  193. }
  194. using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
  195. {
  196. var showScaleHandle = UnityEditor.EditorGUILayout.ToggleLeft("Show spacing handle",
  197. SnapManager.settings.showScaleHandle);
  198. if (check.changed) SnapManager.settings.showScaleHandle = showScaleHandle;
  199. }
  200. }
  201. }
  202. }
  203. private void OnSelectionChange() => _activeGameObject = UnityEditor.Selection.activeGameObject;
  204. }
  205. }