123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- /*
- Copyright (c) 2021 Omar Duarte
- Unauthorized copying of this file, via any medium is strictly prohibited.
- Writen by Omar Duarte, 2021.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
- using UnityEngine;
- namespace PluginMaster
- {
- public class SnapSettingsWindow : UnityEditor.EditorWindow
- {
- private static readonly string[] _gridTypeOptions = { "Rectangular", "Radial" };
- private static SnapSettingsWindow _instance = null;
- [UnityEditor.MenuItem("Tools/Plugin Master/Prefab World Builder/Grid and Snapping Settings...", false, 1150)]
- public static void ShowWindow() => _instance = GetWindow<SnapSettingsWindow>("Grid and Snapping Settings");
- private GameObject _activeGameObject = null;
- public static void RepaintWindow()
- {
- if (_instance != null) _instance.Repaint();
- }
- private void OnEnable()
- {
- _activeGameObject = UnityEditor.Selection.activeGameObject;
- }
- private void OnGUI()
- {
- minSize = new Vector2(350, SnapManager.settings.radialGridEnabled ? 290 : 310);
- using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
- {
- using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
- {
- if (SnapManager.settings.radialGridEnabled)
- {
- SnapManager.settings.radialStep = UnityEditor.EditorGUILayout.FloatField("Radial Snap Value:",
- SnapManager.settings.radialStep);
- }
- else
- {
- SnapManager.settings.step = UnityEditor.EditorGUILayout.Vector3Field("Snap Value:",
- SnapManager.settings.step);
- SnapManager.settings.midpointSnapping = UnityEditor.EditorGUILayout.ToggleLeft("Midpoint snapping",
- SnapManager.settings.midpointSnapping);
- }
- if (check.changed) UnityEditor.SceneView.RepaintAll();
- }
- if (!SnapManager.settings.radialGridEnabled)
- {
- using (new UnityEditor.EditorGUI.DisabledGroupScope(_activeGameObject == null))
- {
- if (GUILayout.Button("Set the snap value to the size of the active gameobject"))
- {
- var bounds = BoundsUtils.GetBounds(_activeGameObject.transform);
- SnapManager.settings.step = bounds.size;
- UnityEditor.SceneView.RepaintAll();
- }
- }
- }
- }
- if (SnapManager.settings.radialGridEnabled)
- {
- using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
- {
- using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
- {
- SnapManager.settings.radialSectors = UnityEditor.EditorGUILayout.IntField("Radial Sectors:",
- SnapManager.settings.radialSectors);
- if (check.changed) UnityEditor.SceneView.RepaintAll();
- }
- }
- }
- using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
- {
- using (new GUILayout.HorizontalScope(UnityEditor.EditorStyles.helpBox))
- {
- int originIndex = 0;
- UnityEditor.EditorGUIUtility.labelWidth = 70;
- using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
- {
- originIndex = UnityEditor.EditorGUILayout.Popup("Grid origin:",
- SnapManager.settings.GetIndexOfSelectedOrigin(), SnapManager.settings.GetOriginNames());
- if (check.changed)
- {
- SnapManager.settings.SelectOrigin(originIndex);
- UnityEditor.SceneView.RepaintAll();
- }
- }
- if (GUILayout.Button("Reset"))
- {
- SnapManager.settings.ResetOrigin();
- }
- if (GUILayout.Button("Save..."))
- {
- RenameWindow.ShowWindow(position.position + Event.current.mousePosition,
- SnapManager.settings.SaveGridOrigin, "Save Origin", SnapManager.settings.selectedOrigin);
- }
- using (new UnityEditor.EditorGUI.DisabledGroupScope(originIndex == 0))
- {
- if (GUILayout.Button("Delete"))
- {
- SnapManager.settings.DeleteSelectedOrigin();
- }
- }
- }
- using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
- {
- using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
- {
- SnapManager.settings.origin = UnityEditor.EditorGUILayout.Vector3Field("Origin position:",
- SnapManager.settings.origin);
- if (check.changed) UnityEditor.SceneView.RepaintAll();
- }
- using (new UnityEditor.EditorGUI.DisabledGroupScope(_activeGameObject == null))
- {
- if (GUILayout.Button("Set the origin to the active gameobject position"))
- {
- SnapManager.settings.origin = _activeGameObject.transform.position;
- UnityEditor.SceneView.RepaintAll();
- }
- }
- }
- using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
- {
- using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
- {
- var euler = SnapManager.settings.rotation.eulerAngles;
- euler = new Vector3(Mathf.RoundToInt(euler.x * 100000) / 100000f,
- Mathf.RoundToInt(euler.y * 100000) / 100000f,
- Mathf.RoundToInt(euler.z * 100000) / 100000f);
- SnapManager.settings.rotation
- = Quaternion.Euler(UnityEditor.EditorGUILayout.Vector3Field("Rotation", euler));
- if (check.changed) UnityEditor.SceneView.RepaintAll();
- }
- using (new UnityEditor.EditorGUI.DisabledGroupScope(_activeGameObject == null))
- {
- if (GUILayout.Button("Set the rotation to the active gameobject rotation"))
- {
- SnapManager.settings.rotation = _activeGameObject.transform.rotation;
- UnityEditor.SceneView.RepaintAll();
- }
- }
- }
- }
- if (!SnapManager.settings.radialGridEnabled)
- {
- using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
- {
- using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
- {
- SnapManager.settings.majorLinesGap
- = UnityEditor.EditorGUILayout.Vector3IntField("Major lines every Nth grid line:",
- SnapManager.settings.majorLinesGap);
- if (check.changed) UnityEditor.SceneView.RepaintAll();
- }
- }
- }
- using (new GUILayout.VerticalScope(UnityEditor.EditorStyles.helpBox))
- {
- using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
- {
- var idx = SnapManager.settings.radialGridEnabled ? 1 : 0;
- idx = UnityEditor.EditorGUILayout.Popup("Grid type:", idx, _gridTypeOptions);
- if (check.changed)
- {
- SnapManager.settings.radialGridEnabled = idx == 0 ? false : true;
- PWBToolbar.RepaintWindow();
- }
- }
- using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
- {
- SnapManager.settings.lockedGrid = UnityEditor.EditorGUILayout.ToggleLeft("Lock the grid origin in place",
- SnapManager.settings.lockedGrid);
- if (check.changed) PWBToolbar.RepaintWindow();
- }
- using (new UnityEditor.EditorGUI.DisabledGroupScope(!SnapManager.settings.lockedGrid))
- {
- using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
- {
- var showPositionHandle = UnityEditor.EditorGUILayout.ToggleLeft("Show position handle",
- SnapManager.settings.showPositionHandle);
- if (check.changed) SnapManager.settings.showPositionHandle = showPositionHandle;
- }
- using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
- {
- var showRotationHandle = UnityEditor.EditorGUILayout.ToggleLeft("Show rotation handle",
- SnapManager.settings.showRotationHandle);
- if (check.changed) SnapManager.settings.showRotationHandle = showRotationHandle;
- }
- using (var check = new UnityEditor.EditorGUI.ChangeCheckScope())
- {
- var showScaleHandle = UnityEditor.EditorGUILayout.ToggleLeft("Show spacing handle",
- SnapManager.settings.showScaleHandle);
- if (check.changed) SnapManager.settings.showScaleHandle = showScaleHandle;
- }
- }
- }
- }
- private void OnSelectionChange() => _activeGameObject = UnityEditor.Selection.activeGameObject;
- }
- }
|