Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngineInternal;
- using UnityEditor;
- using UnityEditorInternal;
- using Sirenix.OdinInspector;
- using Sirenix.OdinInspector.Editor;
- using Sirenix.OdinInspector.Internal;
- using Sirenix.Utilities.Editor;
- public class LevelEditor : OdinEditorWindow
- {
- #region Nested
- public enum Mode { Creation, Edition, Entities, Puzzles, Decor, None }
- public enum Tool { None, Move, PlaceOrRemove, Select }
- #endregion
- #region Interface
- [SerializeField, OnValueChanged("ModeResolver")]
- private Mode currentMode;
- private void ModeResolver()
- {
- if (currentMode == Mode.Creation) currentRoom = null;
- else if (currentMode != Mode.Creation && currentRoom == null) currentMode = Mode.None;
- }
- [SerializeField, OnValueChanged("SwitchToEdit")]
- private Room currentRoom;
- private void SwitchToEdit()
- {
- if (currentRoom != null) currentMode = Mode.Edition;
- }
- [SerializeField, ValidateInput("RoomSizeIsValid"), EnableIf("InCreationMod")]
- private Vector2Int roomSize = new Vector2Int(1, 1);
- private bool RoomSizeIsValid(Vector2Int vector2Int)
- {
- return vector2Int != Vector2Int.zero;
- }
- private bool InCreationMod { get { return currentMode == Mode.Creation; } }
- [SerializeField]
- private GameObject objToPlace;
- #endregion
- #region Fields
- private Tool currentTool;
- private bool[] toolStates;
- private Grid grid;
- private bool isFilling;
- private bool additiveSelection;
- private bool supressiveSelection;
- private List<Bounds> allCellsSelected = new List<Bounds>();
- private List<Bounds> currentCellsSelected = new List<Bounds>();
- private (Bounds start, Bounds end) fillLimits;
- #endregion
- #region Window Methods
- [MenuItem("Tools/Level Editor")]
- public static void ShowWindow()
- {
- GetWindow<LevelEditor>("Level Editor");
- }
- protected new void OnEnable()
- {
- base.OnEnable();
- toolStates = new bool[Enum.GetNames(typeof(Tool)).Length - 1];
- var gridObj = new GameObject("Grid");
- grid = gridObj.AddComponent<Grid>();
- grid.cellSwizzle = GridLayout.CellSwizzle.XZY;
- grid.cellSize = new Vector3(1, 1.414214f, 1);
- gridObj.hideFlags = HideFlags.HideAndDontSave;
- }
- private void OnFocus()
- {
- SceneView.duringSceneGui -= this.OnSceneGUI;
- SceneView.duringSceneGui += this.OnSceneGUI;
- }
- protected new void OnDestroy()
- {
- base.OnDestroy();
- DestroyImmediate(grid.gameObject);
- SceneView.duringSceneGui -= this.OnSceneGUI;
- }
- #endregion
- private void OnSceneGUI(SceneView sceneView)
- {
- HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
- if (currentRoom == null && currentMode != Mode.Creation)
- {
- currentMode = Mode.None;
- }
- Selection.activeObject = grid;
- var currentEvt = Event.current;
- switch (currentMode)
- {
- case Mode.Creation:
- CreateRoom(currentEvt);
- break;
- case Mode.Edition:
- EditRoom(currentEvt);
- break;
- case Mode.Entities:
- break;
- case Mode.Puzzles:
- break;
- case Mode.Decor:
- break;
- }
- sceneView.Repaint();
- /*Handles.BeginGUI();
- var toolIcons = new Dictionary<Tool, EditorIcon>()
- {
- {Tool.Move, EditorIcons.Move },
- {Tool.Place, EditorIcons.Pen },
- {Tool.Select, EditorIcons.Ruler }
- };
- var toolNames = Enum.GetNames(typeof(Tool));
- for (int i = 0; i < toolNames.Length; i++)
- {
- var value = (Tool)Enum.Parse(typeof(Tool), toolNames[i]);
- var toolRect = new Rect(10 + (30 * i), 10, 30, 30);
- var buttonStyle = default(GUIStyle);
- if (i == 0) buttonStyle = EditorStyles.miniButtonLeft;
- else if (i == toolNames.Length - 1) buttonStyle = EditorStyles.miniButtonRight;
- else buttonStyle = EditorStyles.miniButtonMid;
- var toolTexture = toolStates[i] ? toolIcons[value].Highlighted : toolIcons[value].Active;
- toolStates[i] = GUI.Toggle(toolRect, toolStates[i], toolTexture, buttonStyle);
- if (toolStates[i])
- {
- currentTool = value;
- for (int ii = 0; ii < toolStates.Length; ii++)
- {
- if (ii != i) toolStates[ii] = false;
- }
- }
- }
- Handles.EndGUI();
- Selection.activeObject = grid;
- var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
- var plane = new Plane(Vector3.up, Vector3.zero);
- if(plane.Raycast(ray, out float rayDistance))
- {
- var point = ray.GetPoint(rayDistance);
- var bounds = grid.GetBoundsLocal(grid.WorldToCell(point));
- bounds = new Bounds(bounds.max, bounds.size);
- Handles.DrawSolidRectangleWithOutline(GetVerts(bounds), new Color(1, 1, 1, 0.1f), Color.black);
- if (Event.current.OnMouseDown(0) && !cells.Contains(bounds))
- {
- cells.Add(bounds);
- }
- foreach (var cell in cells)
- {
- Handles.DrawSolidRectangleWithOutline(GetVerts(cell), new Color(1, 0, 0, 0.1f), Color.yellow);
- }
- if (cells.Count >= 2)
- {
- var max = new Vector3(cells.Min(c => c.min.x), 0, cells.Min(c => c.min.z));
- var min = new Vector3(cells.Max(c => c.max.x), 0, cells.Max(c => c.max.z));
- var roomBounds = new Bounds();
- roomBounds.SetMinMax(min, max);
- Handles.DrawSolidRectangleWithOutline(GetVerts(roomBounds), new Color(0, 0, 1, 0.1f), Color.red);
- }
- if (Event.current.OnMouseDown(1) && cells.Contains(bounds))
- {
- Debug.Log("Removing");
- cells.Remove(bounds);
- }
- if (Event.current.OnKeyDown(KeyCode.F) && !isFilling)
- {
- isFilling = true;
- fillLimits = (bounds, bounds);
- }
- if (isFilling)
- {
- cellsForFill.Clear();
- fillLimits.end = bounds;
- int spacingX = Mathf.RoundToInt(Mathf.Abs(fillLimits.end.center.x - fillLimits.start.center.x));
- int spacingZ = Mathf.RoundToInt(Mathf.Abs(fillLimits.end.center.z - fillLimits.start.center.z) / 1.414214f);
- Vector3 startPoint = fillLimits.start.center;
- int signX = (fillLimits.start.center.x < fillLimits.end.center.x) ? 1 : -1;
- int signZ = (fillLimits.start.center.z < fillLimits.end.center.z) ? 1 : -1;
- for (int x = 0; x <= spacingX; x++)
- {
- for (int z = 0; z < spacingZ + 1; z++)
- {
- Vector3 cellPos = startPoint + (Vector3.right * x * signX) + (Vector3.forward * 1.414214f * z * signZ);
- Bounds cell = new Bounds(cellPos, new Vector3(1, 0, 1.414214f));
- cellsForFill.Add(cell);
- }
- }
- foreach (Bounds cells in cellsForFill)
- {
- Handles.DrawSolidRectangleWithOutline(GetVerts(cells), new Color(0, 1, 0, 0.25f), Color.clear);
- }
- }
- if (Event.current.OnKeyUp(KeyCode.F))
- {
- Debug.Log("acess");
- var cellsForFillCorrected = cellsForFill.Where(c => !cells.Contains(c));
- cells.AddRange(cellsForFillCorrected);
- isFilling = false;
- }
- }
- sceneView.Repaint();*/
- }
- #region Main Methods
- private void CreateRoom(Event currentEvt)
- {
- if(GetPointedBounds(out Bounds bounds))
- {
- if (currentEvt.OnMouseDown(0) && RoomSizeIsValid(roomSize))
- {
- var roomObj = new GameObject("Room");
- currentRoom = roomObj.AddComponent<Room>();
- currentRoom.Intialize(bounds, roomSize);
- currentMode = Mode.Edition;
- }
- }
- }
- private void EditRoom(Event currentEvt)
- {
- Handles.BeginGUI();
- var toolIcons = new Dictionary<Tool, EditorIcon>()
- {
- {Tool.Move, EditorIcons.Move },
- {Tool.PlaceOrRemove, EditorIcons.Pen },
- {Tool.Select, EditorIcons.Ruler }
- };
- var toolNames = Enum.GetNames(typeof(Tool)).ToList();
- toolNames.Remove("None");
- for (int i = 0; i < toolNames.Count; i++)
- {
- var value = (Tool)Enum.Parse(typeof(Tool), toolNames[i]);
- var toolRect = new Rect(10 + (30 * i), 10, 30, 30);
- var buttonStyle = default(GUIStyle);
- if (i == 0) buttonStyle = EditorStyles.miniButtonLeft;
- else if (i == toolNames.Count - 1) buttonStyle = EditorStyles.miniButtonRight;
- else buttonStyle = EditorStyles.miniButtonMid;
- var toolTexture = toolStates[i] ? toolIcons[value].Highlighted : toolIcons[value].Active;
- toolStates[i] = GUI.Toggle(toolRect, toolStates[i], toolTexture, buttonStyle);
- if (toolStates[i])
- {
- currentTool = value;
- for (int ii = 0; ii < toolStates.Length; ii++)
- {
- if (ii != i) toolStates[ii] = false;
- }
- }
- }
- if (toolStates.All(b => !b)) currentTool = Tool.None;
- Handles.EndGUI();
- var cells = currentRoom.cellInfos.ConvertAll(cf => cf.bounds);
- if (cells.Count >= 2)
- {
- var max = new Vector3(cells.Min(c => c.min.x), 0, cells.Min(c => c.min.z));
- var min = new Vector3(cells.Max(c => c.max.x), 0, cells.Max(c => c.max.z));
- var roomBounds = new Bounds();
- roomBounds.SetMinMax(min, max);
- Handles.DrawSolidRectangleWithOutline(GetVerts(roomBounds), new Color(0, 0, 1, 0.1f), Color.red);
- }
- else Handles.DrawSolidRectangleWithOutline(GetVerts(cells[0]), new Color(0, 0, 1, 0.1f), Color.red);
- switch (currentTool)
- {
- case Tool.PlaceOrRemove:
- PlaceOrRemove(currentEvt);
- break;
- case Tool.Select:
- SelectZone(currentEvt);
- break;
- }
- if(allCellsSelected.Count != 0)
- {
- foreach (var cell in allCellsSelected)
- {
- Handles.DrawSolidRectangleWithOutline(GetVerts(cell), new Color(0, 1, 0, 0.25f), Color.clear);
- }
- }
- }
- private void PlaceOrRemove(Event currentEvt)
- {
- if (GetPointedBounds(out Bounds boundsForPlacement) && currentEvt.OnMouseDown(0))
- {
- if (currentRoom.ContainsCell(boundsForPlacement))
- {
- Remove(boundsForPlacement);
- }
- currentRoom.AddCell(boundsForPlacement, objToPlace);
- }
- if (GetPointedBounds(out Bounds boundsForRemoval) && currentEvt.OnMouseDown(1) && currentRoom.ContainsCell(boundsForRemoval))
- {
- Remove(boundsForRemoval);
- }
- void Remove(Bounds boundsToRemove)
- {
- var cellToRemove = currentRoom.cellInfos.Find(cf => cf.bounds == boundsToRemove);
- var remove = cellToRemove.RemoveObj(LayerMask.NameToLayer("Default"));
- if (remove) currentRoom.cellInfos.Remove(cellToRemove);
- }
- }
- private void SelectZone(Event currentEvt)
- {
- if (GetPointedBounds(out Bounds bounds))
- {
- if (currentEvt.OnMouseDown(0) && !isFilling)
- {
- isFilling = true;
- fillLimits = (bounds, bounds);
- }
- if (isFilling)
- {
- currentCellsSelected.Clear();
- fillLimits.end = bounds;
- int spacingX = Mathf.RoundToInt(Mathf.Abs(fillLimits.end.center.x - fillLimits.start.center.x));
- int spacingZ = Mathf.RoundToInt(Mathf.Abs(fillLimits.end.center.z - fillLimits.start.center.z) / 1.414214f);
- Vector3 startPoint = fillLimits.start.center;
- int signX = (fillLimits.start.center.x < fillLimits.end.center.x) ? 1 : -1;
- int signZ = (fillLimits.start.center.z < fillLimits.end.center.z) ? 1 : -1;
- for (int x = 0; x <= spacingX; x++)
- {
- for (int z = 0; z < spacingZ + 1; z++)
- {
- Vector3 cellPos = startPoint + (Vector3.right * x * signX) + (Vector3.forward * 1.414214f * z * signZ);
- Bounds cell = new Bounds(cellPos, new Vector3(1, 0, 1.414214f));
- currentCellsSelected.Add(cell);
- }
- }
- foreach (Bounds cells in currentCellsSelected)
- {
- Handles.DrawSolidRectangleWithOutline(GetVerts(cells), new Color(1, 1, 0, 0.25f), Color.clear);
- }
- }
- if (currentEvt.shift) additiveSelection = true;
- else additiveSelection = false;
- if (currentEvt.control) supressiveSelection = true;
- else supressiveSelection = false;
- Debug.Log(allCellsSelected.Count);
- if (currentEvt.OnMouseUp(0) && isFilling == true)
- {
- isFilling = false;
- var correctSelection = new List<Bounds>();
- if (supressiveSelection)
- {
- var test = new Bounds[allCellsSelected.Count];
- allCellsSelected.CopyTo(test, 0);
- allCellsSelected.Clear();
- foreach (var cell in test)
- {
- if (!currentCellsSelected.Contains(cell)) allCellsSelected.Add(cell);
- }
- //allCellsSelected.RemoveAll(c => currentCellsSelected.Contains(c));
- }
- else
- {
- if (!additiveSelection)
- {
- allCellsSelected.Clear();
- correctSelection = currentCellsSelected;
- }
- else
- {
- foreach (var cell in currentCellsSelected)
- {
- if (!allCellsSelected.Exists(c => c.center == cell.center)) correctSelection.Add(cell);
- }
- }
- allCellsSelected.AddRange(correctSelection);
- }
- currentCellsSelected.Clear();
- }
- }
- }
- #endregion
- #region General Methods
- public bool GetPointedBounds(out Bounds bounds)
- {
- var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
- var plane = new Plane(Vector3.up, Vector3.zero);
- if (plane.Raycast(ray, out float rayDistance))
- {
- var point = ray.GetPoint(rayDistance);
- bounds = grid.GetBoundsLocal(grid.WorldToCell(point));
- bounds = new Bounds(new Vector3(bounds.max.x, 0, bounds.max.z), new Vector3(bounds.size.x, 0, bounds.size.z));
- Handles.DrawSolidRectangleWithOutline(GetVerts(bounds), new Color(1, 1, 1, 0.1f), Color.black);
- return true;
- }
- else
- {
- bounds = new Bounds();
- return false;
- }
- }
- private Vector3[] GetVerts(Bounds bounds)
- {
- Vector3[] verts = new Vector3[]
- {
- new Vector3(bounds.min.x, 0, bounds.min.z),
- new Vector3(bounds.min.x, 0, bounds.max.z),
- new Vector3(bounds.max.x, 0, bounds.max.z),
- new Vector3(bounds.max.x, 0, bounds.min.z),
- };
- return verts;
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement