Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using System.IO;
- public class MapEditor : MonoBehaviour
- {
- #region Singleton
- public static MapEditor Instance;
- private void Awake()
- {
- Instance = this;
- }
- #endregion
- private static byte MaxConsoleMessages = 9;
- public enum ToolMode { None, Placement, Move, Rotate, Scale };
- public enum ConsoleMessageType { Normal, Warning, Error };
- [Header("References")]
- public MapEditorCamera editorCamera;
- public Material ghostMaterial;
- public GameObject nodeGenericVisualRepresentationPrefab;
- [Header("References/Tools")]
- public Transform cursorSelectionHolder;
- public Color placementColor;
- [Header("References/UI")]
- public GameObject ui_escapeMenu;
- [Header("Information")]
- [SerializeField] private Vector3 cursorWorldPosition = Vector3.zero;
- [SerializeField] private Vector3 nearestGridPoint = Vector3.zero;
- [SerializeField] private ToolMode toolMode = ToolMode.Placement;
- public AssetDirectories selectedDirectory;
- public AssetGroup selectedGroup;
- public List<string> consoleMessages = new List<string>();
- [SerializeField] private MapEditorAsset selectedAssetForPlacement;
- [SerializeField] private PlacedNode.NodeType selectedNodeTypeForPlacement;
- [SerializeField] private PlacedObject selectedObject;
- [SerializeField] private bool snapToGrid = false;
- [SerializeField] private float realTime;
- [SerializeField] private List<string> consoleMessagesSentThisSession = new List<string>();
- private void Start()
- {
- WriteToConsole(System.DateTime.Now.ToString());
- StartNewProject();
- }
- private void OnDisable()
- {
- // Unselect whatever selection has been selected to undo any potential changes
- if (selectedObject != null)
- UnSelectCurrentSelection();
- }
- private void OnApplicationQuit()
- {
- // Write all the map editor logs to a file
- WriteToConsole("Closing Application...");
- WriteToConsole(System.DateTime.Now.ToString());
- File.WriteAllLines(UGI.LogsPath + "/LastLog_MapEditor.txt", consoleMessagesSentThisSession.ToArray());
- }
- private void StartNewProject ()
- {
- // Inform the data handler to scrub/delete existing data
- MapEditorDataHandler.Instance.ClearAllData();
- // Default the map editor to having nothing selected
- SetTool(ToolMode.None);
- // Default us to having the "GW" group selected
- SetGroup(AssetGroup.GW);
- // Default us to having the "All" directory selected which will make all assets display
- SetDirectory(AssetDirectories.All);
- // Update the nodes to display all available nodes
- UI_MapEditor.Instance.UpdateNodesUI();
- // And then select to display the assets menu
- UI_MapEditor.Instance.OnClick_Menu_Assets();
- // Then write to the console
- WriteToConsole("Started new project");
- }
- private void Update()
- {
- realTime = Time.realtimeSinceStartup;
- // Toggle the escape menu
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- ui_escapeMenu.SetActive(!ui_escapeMenu.activeInHierarchy);
- }
- // Find the closest point to the cursor and cache it
- cursorWorldPosition = GameCursor.GetClosestPoint(editorCamera.gameObject, true, GameCursor.IgnoreMapEditorTags);
- // Then round it to find the nearest grid point
- nearestGridPoint = Vector3Int.RoundToInt(cursorWorldPosition);
- // If we're snapping to grid
- snapToGrid = Input.GetKey(KeyCode.LeftControl);
- // Do null checks
- OnUpdate_CheckForNullReferences();
- // Handle logic of tools differently
- switch (toolMode)
- {
- case ToolMode.None:
- OnUpdate_NoneTool();
- break;
- case ToolMode.Placement:
- OnUpdate_PlacementTool();
- break;
- }
- }
- private void OnUpdate_CheckForNullReferences ()
- {
- // If the object we had selected becomes null, unselect it
- if (selectedObject == null)
- UnSelectCurrentSelection();
- }
- private void OnUpdate_NoneTool ()
- {
- // If our cursor is over any UI, don't do anything
- if (GameCursor.IsCursorOverUI())
- return;
- // If we left click
- if (Input.GetMouseButtonDown(0))
- {
- // Find the closest object we hit
- GameObject hitObject = GameCursor.FindClosestObject(editorCamera.gameObject, true, GameCursor.IgnoreMapEditorTags);
- // If we hit nothing
- if(hitObject == null)
- {
- UnSelectCurrentSelection();
- return;
- }
- // If it has a selectable component, then select it
- PlacedObject placedObject = hitObject.GetComponent<PlacedObject>();
- if(placedObject != null)
- {
- SelectObject(placedObject);
- return;
- }
- else
- {
- // If what we clicked on is not selectable, then we've selected nothing
- // and should unselect whatever we do have selected
- UnSelectCurrentSelection();
- }
- }
- }
- private void OnUpdate_PlacementTool ()
- {
- if (snapToGrid)
- {
- SetCursorSelectionVisualToNearestGridPoint();
- } else
- {
- SetCursorSelectVisualToCursor();
- }
- // If we don't have an asset selected for placement,
- // AKA we're not placing anything, then we can't be
- // in placement mode
- if (selectedAssetForPlacement == null && selectedNodeTypeForPlacement == PlacedNode.NodeType.None)
- {
- WriteToConsole("Nothing selected yet tool was caught in placement mode - Switching to none", ConsoleMessageType.Warning);
- SetTool(ToolMode.None);
- return;
- }
- // If our cursor isn't over any UI
- if(GameCursor.IsCursorOverUI() == false)
- {
- // If we left click
- if (Input.GetMouseButtonDown(0))
- {
- if(selectedAssetForPlacement != null)
- {
- // Place the asset we're holding, keep placing if we're holding shift
- PlaceAssetAtCursor(Input.GetKey(KeyCode.LeftShift));
- }
- else if (selectedNodeTypeForPlacement != PlacedNode.NodeType.None)
- {
- // Place the node we're holding, keep placing if we're holding shift
- PlaceNodeAtCursor(Input.GetKey(KeyCode.LeftShift));
- }
- }
- else if (Input.GetMouseButtonDown(1))
- { // If we right click
- if (selectedAssetForPlacement != null)
- {
- // Cancel what we were doing
- CancelPlacingAsset();
- }
- else if (selectedNodeTypeForPlacement != PlacedNode.NodeType.None)
- {
- // Cancel what we were doing
- CancelPlacingNode();
- }
- }
- }
- }
- private void PlaceAssetAtCursor (bool isHoldingToPlaceMultiple)
- {
- MEObjectSpawner.Instance.PlaceAsset
- (
- selectedAssetForPlacement,
- snapToGrid ? nearestGridPoint : cursorWorldPosition,
- Vector3.zero,
- Vector3.one
- );
- if(isHoldingToPlaceMultiple == false)
- {
- CancelPlacingAsset();
- }
- }
- private void PlaceNodeAtCursor (bool isHoldingToPlaceMultiple)
- {
- MEObjectSpawner.Instance.PlaceNode
- (
- selectedNodeTypeForPlacement,
- snapToGrid ? nearestGridPoint : cursorWorldPosition,
- Vector3.zero,
- Vector3.one
- );
- if (isHoldingToPlaceMultiple == false)
- CancelPlacingNode();
- }
- private void CancelPlacingAsset ()
- {
- SetTool(ToolMode.None);
- selectedAssetForPlacement = null;
- }
- private void CancelPlacingNode ()
- {
- SetTool(ToolMode.None);
- selectedNodeTypeForPlacement = PlacedNode.NodeType.None;
- }
- private void SetCursorSelectVisualToCursor ()
- {
- // Set the position of the cursor selection to the cursor
- cursorSelectionHolder.position = cursorWorldPosition;
- }
- private void SetCursorSelectionVisualToNearestGridPoint ()
- {
- // Set the position of the cursor selection to the nearest grid point
- cursorSelectionHolder.position = nearestGridPoint;
- }
- public void SelectAssetFromAssetBrowser(MapEditorAsset asset)
- {
- // Set the tool to none in case we had something else selected
- SetTool(ToolMode.None);
- // Cache the selected asset
- selectedAssetForPlacement = asset;
- // Instantiate a copy of the asset to the cursor selection holder so the player
- // can see what they're placing down
- GameObject visualAsset = Instantiate(asset.prefab, cursorSelectionHolder);
- // Then iterate through it and make it look like a ghost
- foreach(MeshRenderer renderer in visualAsset.GetComponentsInChildren<MeshRenderer>())
- {
- renderer.material = ghostMaterial;
- }
- // And iterate through it and get rid of any colliders so while placing it can't
- // collide with raycasts or other objects
- foreach(Collider col in visualAsset.GetComponentsInChildren<Collider>())
- {
- Destroy(col);
- }
- // Finally, set the map editor tool to placement
- SetTool(ToolMode.Placement);
- }
- public void SelectNodeFromNodeBrowser (PlacedNode.NodeType type)
- {
- // Set the tool to none in case we had something else selected
- SetTool(ToolMode.None);
- // Then cache the selected node
- selectedNodeTypeForPlacement = type;
- // Create a visual to represent where the node will go & parent it to the cursor
- Instantiate(nodeGenericVisualRepresentationPrefab, cursorSelectionHolder);
- // Then set the map editor tool to placement
- SetTool(ToolMode.Placement);
- }
- public void SetTool(ToolMode tool)
- {
- toolMode = tool;
- // Change the placement visual depending on the selected tool
- switch (tool)
- {
- case ToolMode.None:
- // Get rid of any references to things we may have been placing
- selectedAssetForPlacement = null;
- selectedNodeTypeForPlacement = PlacedNode.NodeType.None;
- // Make the object no longer visible
- cursorSelectionHolder.gameObject.SetActive(false);
- // Set it to a position the player will never see
- cursorSelectionHolder.position = new Vector3(-999f, -999f, -999f);
- // And get rid of any children since they're likely ghosts of whatever object
- // the player was placing
- foreach(Transform child in cursorSelectionHolder)
- {
- Destroy(child.gameObject);
- }
- break;
- case ToolMode.Placement:
- cursorSelectionHolder.gameObject.SetActive(true);
- break;
- }
- }
- public void SetGroup (AssetGroup group)
- {
- selectedGroup = group;
- UI_MapEditor.Instance.UpdateGroupsUI();
- UI_MapEditor.Instance.UpdateAssetsUI();
- }
- public void SetDirectory (AssetDirectories directory)
- {
- selectedDirectory = directory;
- UI_MapEditor.Instance.UpdateDirectoriesUI();
- UI_MapEditor.Instance.UpdateAssetsUI();
- }
- public void WriteToConsole (string text, ConsoleMessageType messageType = ConsoleMessageType.Normal)
- {
- string changedText = "";
- switch(messageType)
- {
- case ConsoleMessageType.Warning:
- changedText = "> <color=yellow>(Warning) " + text + "</color>";
- Debug.LogWarning("(Map Editor) " + text);
- break;
- case ConsoleMessageType.Error:
- changedText = "> <color=red>(Error) " + text + "</color>";
- Debug.LogError("(Map Editor) " + text);
- break;
- default:
- changedText = "> (Message) " + text;
- Debug.Log("(Map Editor) " + text);
- break;
- }
- consoleMessages.Add(changedText);
- consoleMessagesSentThisSession.Add("[" + realTime.ToString("N3") + "] " + changedText);
- if (consoleMessages.Count > MaxConsoleMessages)
- consoleMessages.RemoveAt(0);
- UI_MapEditor.Instance.UpdateConsoleText();
- }
- public void SelectObject (PlacedObject placedObject)
- {
- // Unselect whatever we currently have selected
- UnSelectCurrentSelection();
- Debug.Log("Selected: " + placedObject.name);
- // Cache our selection
- selectedObject = placedObject;
- // Update the inspector
- UI_MapEditor.Instance.UpdateInspectorUI(selectedObject);
- }
- public void UnSelectCurrentSelection ()
- {
- // Make the reference null
- selectedObject = null;
- // Then update the inspector
- UI_MapEditor.Instance.UpdateInspectorUI(null);
- }
- /*
- * On Value Changed Functions
- */
- #region Inspector Transform Changes
- public void OnValueChanged_Inspector_PositionX()
- {
- // Get the value the player entered
- string text = UI_MapEditor.Instance.inspector_transform_positionDisplay_x.text;
- // Then convert it to a float
- float value = text == "" ? 0f : float.Parse(text);
- // Cache the object's relative transform
- Vector3 position = selectedObject.transform.position;
- // And update the object's transform accordingly
- position.x = value;
- selectedObject.transform.position = position;
- }
- public void OnValueChanged_Inspector_PositionY()
- {
- // Get the value the player entered
- string text = UI_MapEditor.Instance.inspector_transform_positionDisplay_y.text;
- // Then convert it to a float
- float value = text == "" ? 0f : float.Parse(text);
- // Cache the object's relative transform
- Vector3 position = selectedObject.transform.position;
- // And update the object's transform accordingly
- position.y = value;
- selectedObject.transform.position = position;
- }
- public void OnValueChanged_Inspector_PositionZ()
- {
- // Get the value the player entered
- string text = UI_MapEditor.Instance.inspector_transform_positionDisplay_z.text;
- // Then convert it to a float
- float value = text == "" ? 0f : float.Parse(text);
- // Cache the object's relative transform
- Vector3 position = selectedObject.transform.position;
- // And update the object's transform accordingly
- position.z = value;
- selectedObject.transform.position = position;
- }
- public void OnValueChanged_Inspector_RotationX()
- {
- // Get the value the player entered
- string text = UI_MapEditor.Instance.inspector_transform_rotationDisplay_x.text;
- // Then convert it to a float
- float value = text == "" ? 0f : float.Parse(text);
- // Cache the object's relative transform
- Vector3 rotation = selectedObject.transform.eulerAngles;
- // And update the object's transform accordingly
- rotation.x = value;
- selectedObject.transform.eulerAngles = rotation;
- }
- public void OnValueChanged_Inspector_RotationY()
- {
- // Get the value the player entered
- string text = UI_MapEditor.Instance.inspector_transform_rotationDisplay_y.text;
- // Then convert it to a float
- float value = text == "" ? 0f : float.Parse(text);
- // Cache the object's relative transform
- Vector3 rotation = selectedObject.transform.eulerAngles;
- // And update the object's transform accordingly
- rotation.y = value;
- selectedObject.transform.eulerAngles = rotation;
- }
- public void OnValueChanged_Inspector_RotationZ()
- {
- // Get the value the player entered
- string text = UI_MapEditor.Instance.inspector_transform_rotationDisplay_z.text;
- // Then convert it to a float
- float value = text == "" ? 0f : float.Parse(text);
- // Cache the object's relative transform
- Vector3 rotation = selectedObject.transform.eulerAngles;
- // And update the object's transform accordingly
- rotation.z = value;
- selectedObject.transform.eulerAngles = rotation;
- }
- public void OnValueChanged_Inspector_ScaleX()
- {
- // Get the value the player entered
- string text = UI_MapEditor.Instance.inspector_transform_scaleDisplay_x.text;
- // Then convert it to a float
- float value = text == "" ? 0f : float.Parse(text);
- // Cache the object's relative transform
- Vector3 scale = selectedObject.transform.localScale;
- // And update the object's transform accordingly
- scale.x = value;
- selectedObject.transform.localScale = scale;
- }
- public void OnValueChanged_Inspector_ScaleY()
- {
- // Get the value the player entered
- string text = UI_MapEditor.Instance.inspector_transform_scaleDisplay_y.text;
- // Then convert it to a float
- float value = text == "" ? 0f : float.Parse(text);
- // Cache the object's relative transform
- Vector3 scale = selectedObject.transform.localScale;
- // And update the object's transform accordingly
- scale.y = value;
- selectedObject.transform.localScale = scale;
- }
- public void OnValueChanged_Inspector_ScaleZ()
- {
- // Get the value the player entered
- string text = UI_MapEditor.Instance.inspector_transform_scaleDisplay_z.text;
- // Then convert it to a float
- float value = text == "" ? 0f : float.Parse(text);
- // Cache the object's relative transform
- Vector3 scale = selectedObject.transform.localScale;
- // And update the object's transform accordingly
- scale.z = value;
- selectedObject.transform.localScale = scale;
- }
- #endregion
- /*
- * On Click Functions
- */
- public void OnClick_DeleteSelectedObject ()
- {
- MEObjectSpawner.Instance.DestroyPlacedObject(selectedObject);
- UnSelectCurrentSelection();
- }
- public void OnClick_Save ()
- {
- UI_MapEditor.Instance.OpenSaveMenu();
- }
- public void OnClick_Load ()
- {
- UI_MapEditor.Instance.OpenLoadMenu();
- }
- public void OnClick_AssetInHierarchy (PlacedAsset asset)
- {
- SelectObject(asset);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement