/// <summary> /// Shows the room context menu. /// </summary> /// <param name="roomNode"></param> private void ShowRoomContextMenu(RoomNode roomNode) { var genericMenu = new GenericMenu(); genericMenu.AddItem(new GUIContent("Delete room"), false, () => DeleteRoomNode(roomNode)); genericMenu.ShowAsContext(); }
/// <summary> /// Creates a connection between the two given room nodes. /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <returns></returns> public ConnectionBase CreateConnection(RoomNode from, RoomNode to) { // Do not create the connection if the from room is same as the to room if (from.Room == to.Room) { return(null); } // Do not create the connection if it already exists if (connectionNodes.Any(x => (x.From == from && x.To == to) || (x.To == from && x.From == to))) { return(null); } var type = FindType(LevelGraph.RoomType); var connectionType = type != null ? LevelGraph.ConnectionType : typeof(Connection).FullName; var connection = (ConnectionBase)CreateInstance(connectionType); connection.From = from.Room; connection.To = to.Room; LevelGraph.Connections.Add(connection); AssetDatabase.AddObjectToAsset(connection, LevelGraph); CreateConnectionNode(connection); EditorUtility.SetDirty(LevelGraph); return(connection); }
/// <summary> /// Creates a room node from a given room. /// </summary> /// <param name="room"></param> /// <returns></returns> private RoomNode CreateRoomNode(RoomBase room) { var roomNode = new RoomNode(room); roomNodes.Add(roomNode); return(roomNode); }
/// <summary> /// Deletes a given room node. /// </summary> /// <param name="roomNode"></param> private void DeleteRoomNode(RoomNode roomNode) { LevelGraph.Rooms.Remove(roomNode.Room); DestroyImmediate(roomNode.Room, true); roomNodes.Remove(roomNode); var connectionsToRemove = new List <ConnectionNode>(); foreach (var connectionNode in connectionNodes) { if (connectionNode.From == roomNode || connectionNode.To == roomNode) { connectionsToRemove.Add(connectionNode); } } foreach (var connectionNode in connectionsToRemove) { DeleteConnectionNode(connectionNode); } EditorUtility.SetDirty(LevelGraph); }
/// <summary> /// Initialize the window with a given level graph. /// </summary> /// <param name="levelGraph"></param> public void Initialize(LevelGraph levelGraph) { LevelGraph = levelGraph; CurrentState = State.Idle; zoom = LevelGraph.EditorData.Zoom; panOffset = LevelGraph.EditorData.PanOffset; snapToGrid = EditorPrefs.GetBool(EditorConstants.SnapToGridEditorPrefsKey, false); connectionStartNode = null; // Initialize room nodes roomNodes = new List <RoomNode>(); foreach (var room in LevelGraph.Rooms) { if (room != null) { CreateRoomNode(room); } else { Debug.LogError($"There is a null room in the level graph {levelGraph.name}. This should not happen."); } } // Initialize connection nodes connectionNodes = new List <ConnectionNode>(); foreach (var connection in LevelGraph.Connections) { if (connection != null) { CreateConnectionNode(connection); } else { Debug.LogError($"There is a null connection in the level graph {levelGraph.name}. This should not happen."); } } }
public ConnectionNode(ConnectionBase connection, RoomNode from, RoomNode to) { Connection = connection; From = from; To = to; }