private void AddScripts(List <ScriptItem> items) { var actions = new List <IUndoAction>(4); for (int i = 0; i < items.Count; i++) { var item = items[i]; var scriptName = item.ScriptName; var scriptType = ScriptsBuilder.FindScript(scriptName); if (scriptType == null) { Editor.LogError("Invalid script type " + scriptName); return; } var actors = ScriptsEditor.ParentEditor.Values; for (int j = 0; j < actors.Count; j++) { var actor = (Actor)actors[j]; actions.Add(AddRemoveScript.Add(actor, scriptType)); } } if (actions.Count == 0) { Editor.LogWarning("Failed to spawn scripts"); return; } var multiAction = new MultiUndoAction(actions); multiAction.Do(); Editor.Instance.Undo.AddAction(multiAction); }
/// <summary> /// Deletes selected objects. /// </summary> public void Delete() { // Peek things that can be removed var objects = Selection.Where(x => x.CanDelete && x != Graph.Main).ToList().BuildAllNodes().Where(x => x.CanDelete).ToList(); if (objects.Count == 0) { return; } // Change selection var action1 = new SelectionChangeAction(Selection.ToArray(), new SceneGraphNode[0], OnSelectionUndo); // Delete objects var action2 = new CustomDeleteActorsAction(objects); // Merge actions and perform them var action = new MultiUndoAction(new IUndoAction[] { action1, action2 }, action2.ActionString); action.Do(); Undo.AddAction(action); }
private void AddScripts(List <ScriptType> items) { var actions = new List <IUndoAction>(4); for (int i = 0; i < items.Count; i++) { var scriptType = items[i]; var actors = ScriptsEditor.ParentEditor.Values; for (int j = 0; j < actors.Count; j++) { var actor = (Actor)actors[j]; actions.Add(AddRemoveScript.Add(actor, scriptType)); } } if (actions.Count == 0) { Editor.LogWarning("Failed to spawn scripts"); return; } var multiAction = new MultiUndoAction(actions); multiAction.Do(); ScriptsEditor.Presenter?.Undo.AddAction(multiAction); }
/// <summary> /// Deletes the selected objects. Supports undo/redo. /// </summary> public void Delete() { // Peek things that can be removed var objects = Selection.Where(x => x.CanDelete).ToList().BuildAllNodes().Where(x => x.CanDelete).ToList(); if (objects.Count == 0) { return; } // Change selection var action1 = new SelectionChangeAction(Selection.ToArray(), new SceneGraphNode[0]); // Delete objects var action2 = new DeleteActorsAction(objects); // Merge two actions and perform them var action = new MultiUndoAction(new IUndoAction[] { action1, action2 }, action2.ActionString); action.Do(); Undo.AddAction(action); // Auto CSG mesh rebuild foreach (var obj in objects) { if (obj is ActorNode node && node.Actor is BoxBrush) { node.Actor.Scene.BuildCSG(); } } }
/// <inheritdoc /> public override DragDropEffect OnDragDrop(ref Vector2 location, DragData data) { var result = base.OnDragDrop(ref location, data); if (_dragScripts.HasValidDrag) { result = _dragScripts.Effect; var actions = new List <IUndoAction>(4); for (int i = 0; i < _dragScripts.Objects.Count; i++) { var item = _dragScripts.Objects[i]; var scriptName = item.ScriptName; var scriptType = ScriptsBuilder.FindScript(scriptName); var actors = ScriptsEditor.ParentEditor.Values; for (int j = 0; j < actors.Count; j++) { var actor = (Actor)actors[j]; actions.Add(AddRemoveScript.Add(actor, scriptType)); } } var multiAction = new MultiUndoAction(actions); multiAction.Do(); Editor.Instance.Undo.AddAction(multiAction); } _dragScripts.OnDragDrop(); return(result); }
/// <summary> /// Deletes the selected objects. Supports undo/redo. /// </summary> public void Delete() { // Peek things that can be removed var objects = Selection.Where(x => x.CanDelete).ToList().BuildAllNodes().Where(x => x.CanDelete).ToList(); if (objects.Count == 0) { return; } bool isPlayMode = Editor.StateMachine.IsPlayMode; SelectionDeleteBegin?.Invoke(); // Change selection var action1 = new SelectionChangeAction(Selection.ToArray(), new SceneGraphNode[0], OnSelectionUndo); // Delete objects var action2 = new DeleteActorsAction(objects); // Merge two actions and perform them var action = new MultiUndoAction(new IUndoAction[] { action1, action2 }, action2.ActionString); action.Do(); Undo.AddAction(action); SelectionDeleteEnd?.Invoke(); var options = Editor.Options.Options; // Auto CSG mesh rebuild if (!isPlayMode && options.General.AutoRebuildCSG) { foreach (var obj in objects) { if (obj is ActorNode node && node.Actor is BoxBrush) { node.Actor.Scene.BuildCSG(options.General.AutoRebuildCSGTimeoutMs); } } } // Auto NavMesh rebuild if (!isPlayMode && options.General.AutoRebuildNavMesh) { foreach (var obj in objects) { if (obj is ActorNode node && node.Actor.Scene && (node.Actor.StaticFlags & StaticFlags.Navigation) == StaticFlags.Navigation) { var bounds = node.Actor.BoxWithChildren; node.Actor.Scene.BuildNavMesh(bounds, options.General.AutoRebuildNavMeshTimeoutMs); } } } }
/// <summary> /// Spawns the specified actor to the game (with undo). /// </summary> /// <param name="actor">The actor.</param> /// <param name="parent">The parent actor. Set null as default.</param> /// <param name="autoSelect">True if automatically select the spawned actor, otherwise false.</param> public void Spawn(Actor actor, Actor parent = null, bool autoSelect = true) { bool isPlayMode = Editor.StateMachine.IsPlayMode; if (Level.IsAnySceneLoaded == false) { throw new InvalidOperationException("Cannot spawn actor when no scene is loaded."); } SpawnBegin?.Invoke(); // Add it Level.SpawnActor(actor, parent); // Peek spawned node var actorNode = Editor.Instance.Scene.GetActorNode(actor); if (actorNode == null) { throw new InvalidOperationException("Failed to create scene node for the spawned actor."); } // During play in editor mode spawned actors should be dynamic (user can move them) if (isPlayMode) { actor.StaticFlags = StaticFlags.None; } // Call post spawn action (can possibly setup custom default values) actorNode.PostSpawn(); // Create undo action IUndoAction action = new DeleteActorsAction(new List <SceneGraphNode>(1) { actorNode }, true); if (autoSelect) { var before = Selection.ToArray(); Selection.Clear(); Selection.Add(actorNode); OnSelectionChanged(); action = new MultiUndoAction(action, new SelectionChangeAction(before, Selection.ToArray(), OnSelectionUndo)); } Undo.AddAction(action); // Mark scene as dirty Editor.Scene.MarkSceneEdited(actor.Scene); SpawnEnd?.Invoke(); OnDirty(actorNode); }
/// <summary> /// Deletes the selected objects. Supports undo/redo. /// </summary> public void Delete() { // Peek things that can be removed var objects = Selection.Where(x => x.CanDelete).ToList().BuildAllNodes().Where(x => x.CanDelete).ToList(); if (objects.Count == 0) { return; } var isSceneTreeFocus = Editor.Windows.SceneWin.ContainsFocus; SelectionDeleteBegin?.Invoke(); // Change selection var action1 = new SelectionChangeAction(Selection.ToArray(), new SceneGraphNode[0], OnSelectionUndo); // Delete objects var action2 = new DeleteActorsAction(objects); // Merge two actions and perform them var action = new MultiUndoAction(new IUndoAction[] { action1, action2 }, action2.ActionString); action.Do(); Undo.AddAction(action); SelectionDeleteEnd?.Invoke(); OnDirty(objects); if (isSceneTreeFocus) { Editor.Windows.SceneWin.Focus(); } }
/// <summary> /// Converts the selected actor to another type. /// </summary> /// <param name="to">The type to convert in.</param> public void Convert(Type to) { if (!Editor.SceneEditing.HasSthSelected || !(Editor.SceneEditing.Selection[0] is ActorNode)) return; if (Level.IsAnySceneLoaded == false) throw new InvalidOperationException("Cannot spawn actor when no scene is loaded."); var actionList = new IUndoAction[4]; Actor old = ((ActorNode)Editor.SceneEditing.Selection[0]).Actor; Actor actor = (Actor)FlaxEngine.Object.New(to); var parent = old.Parent; var orderInParent = old.OrderInParent; SelectionDeleteBegin?.Invoke(); actionList[0] = new SelectionChangeAction(Selection.ToArray(), new SceneGraphNode[0], OnSelectionUndo); actionList[0].Do(); actionList[1] = new DeleteActorsAction(new List<SceneGraphNode> { Editor.Instance.Scene.GetActorNode(old) }); actionList[1].Do(); SelectionDeleteEnd?.Invoke(); SpawnBegin?.Invoke(); // Copy properties actor.Transform = old.Transform; actor.StaticFlags = old.StaticFlags; actor.HideFlags = old.HideFlags; actor.Layer = old.Layer; actor.Tag = old.Tag; actor.Name = old.Name; actor.IsActive = old.IsActive; // Spawn actor Level.SpawnActor(actor, parent); if (parent != null) actor.OrderInParent = orderInParent; if (Editor.StateMachine.IsPlayMode) actor.StaticFlags = StaticFlags.None; // Move children for (var i = old.ScriptsCount - 1; i >= 0; i--) { var script = old.Scripts[i]; script.Actor = actor; Guid newid = Guid.NewGuid(); FlaxEngine.Object.Internal_ChangeID(FlaxEngine.Object.GetUnmanagedPtr(script), ref newid); } for (var i = old.Children.Length - 1; i >= 0; i--) { old.Children[i].Parent = actor; } var actorNode = Editor.Instance.Scene.GetActorNode(actor); if (actorNode == null) throw new InvalidOperationException("Failed to create scene node for the spawned actor."); actorNode.PostSpawn(); Editor.Scene.MarkSceneEdited(actor.Scene); actionList[2] = new DeleteActorsAction(new List<SceneGraphNode> { actorNode }, true); actionList[3] = new SelectionChangeAction(new SceneGraphNode[0], new SceneGraphNode[] { actorNode }, OnSelectionUndo); actionList[3].Do(); var actions = new MultiUndoAction(actionList); Undo.AddAction(actions); SpawnEnd?.Invoke(); OnDirty(actorNode); }