public virtual void Finish(bool cancel) { if (!cancel) { //add to undo with deletion var document = DocumentWindow.Document; var action = new UndoActionComponentCreateDelete(document, new Component[] { CreatingObject }, true); document.UndoSystem.CommitAction(action); document.Modified = true; } else { CreatingObject?.Dispose(); documentWindow.SelectObjects(null); } DocumentWindow.ObjectCreationModeSet(null); }
public virtual bool TryDeleteObjects() { //!!!!!игнорить выделенные-вложенные. где еще так if (!CanDeleteObjects(out var objectsToDelete)) { return(false); } string text; if (objectsToDelete.Count == 1) { string template = EditorLocalization.Translate("DocumentWindow", "Are you sure you want to delete \'{0}\'?"); var name = objectsToDelete[0].ToString(); text = string.Format(template, name); } else { string template = EditorLocalization.Translate("DocumentWindow", "Are you sure you want to delete selected objects?"); text = string.Format(template, objectsToDelete.Count); } if (EditorMessageBox.ShowQuestion(text, EMessageBoxButtons.YesNo) == EDialogResult.No) { return(false); } //!!!!может сцену выбрать? везде так //clear selected objects SelectObjects(null); //add to undo with deletion var action = new UndoActionComponentCreateDelete(Document, objectsToDelete.Cast <Component>().ToArray(), false); Document.UndoSystem.CommitAction(action); Document.Modified = true; return(true); }
public virtual void TryCloneObjects() { //!!!!!игнорить выделенные-вложенные. где еще так if (!CanCloneObjects(out var objectsToClone)) { return; } List <Component> newObjects = new List <Component>(); foreach (var obj in objectsToClone) { var newObject = EditorUtility.CloneComponent(obj); newObjects.Add(newObject); } //select objects { var selectObjects = new List <object>(); //!!!!все выделить? selectObjects.AddRange(newObjects); SelectObjects(selectObjects); } if (newObjects.Count == 0) { return; } //add to undo with deletion var action = new UndoActionComponentCreateDelete(Document, newObjects, true); Document.UndoSystem.CommitAction(action); Document.Modified = true; //add screen message EditorUtility.ShowScreenNotificationObjectsCloned(newObjects.Count); }
private void ButtonAddEventHandler_Click(object sender, EventArgs e) { var items = new List <KryptonContextMenuItemBase>(); var subscribeTo = GetOneControlledObject <Component>(); if (subscribeTo == null) { return; } //add handler to C# class { bool enable = false; string className = null; string csharpFileName = null; var fileName = subscribeTo.ParentRoot.HierarchyController?.CreatedByResource?.Owner.Name; if (!string.IsNullOrEmpty(fileName)) { className = subscribeTo.ParentRoot.GetType().Name; try { //find by same class name if (string.Compare(Path.GetFileNameWithoutExtension(fileName), className, true) == 0) //if( Path.GetFileNameWithoutExtension( fileName ) == className ) { csharpFileName = VirtualPathUtility.GetRealPathByVirtual(Path.ChangeExtension(fileName, "cs")); if (File.Exists(csharpFileName)) { enable = true; } } //find by same file name if (!enable) { csharpFileName = VirtualPathUtility.GetRealPathByVirtual(Path.Combine(Path.GetDirectoryName(fileName), className + ".cs")); if (File.Exists(csharpFileName)) { enable = true; } } } catch { } } var item = new KryptonContextMenuItem(Translate("Add Handler to C# File"), null, delegate(object s, EventArgs e2) { AddHandlerToSharpClass(subscribeTo, csharpFileName, className); }); item.Enabled = enable; items.Add(item); } //add handler to C# script { var groupItem = new KryptonContextMenuItem(Translate("Add Handler to C# Script"), null); var childItems = new List <KryptonContextMenuItemBase>(); //create new C# script { var itemsData = new List <(string, Component)>(); itemsData.Add((Translate("Add C# Script in This Component"), subscribeTo)); if (subscribeTo != subscribeTo.ParentRoot) { itemsData.Add((Translate("Add C# Script in the Root Component"), subscribeTo.ParentRoot)); } foreach (var itemData in itemsData) { var childItem = new KryptonContextMenuItem(itemData.Item1, null, delegate(object s, EventArgs e2) { var parent = (Component)((KryptonContextMenuItem)s).Tag; var document = Owner.DocumentWindow.Document; //create script var script = parent.CreateComponent <Component_CSharpScript>(enabled: false); script.Name = script.BaseType.GetUserFriendlyNameForInstance(); script.Code = "class _Temp{\r\n}"; script.Enabled = true; //activate flow graph window var scriptDocumentWindow = EditorAPI.OpenDocumentWindowForObject(document, script) as Component_CSharpScript_DocumentWindow; Owner.DocumentWindow.SelectObjects(new object[] { script }); GetMethodInfo(subscribeTo, out var parameters, out var parametersSignature); var methodNameNotUnique = subscribeTo.Name.Replace(" ", "") + "_" + _event.Name; var methodName = methodNameNotUnique; var methodSignature = $"method:{methodName}({parametersSignature})"; if (!scriptDocumentWindow.ScriptEditorControl.AddMethod(methodName, parameters, out var error)) { Log.Warning("Unable to add a code of the method. " + error); //!!!! } //fix code try { scriptDocumentWindow.ScriptEditorControl.GetCode(out var code); code = code.Replace("class _Temp{", ""); var index = code.LastIndexOf('}'); code = code.Substring(0, index); string newCode = ""; var lines = code.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { newCode += line.Trim() + "\r\n"; } script.Code = newCode; } catch (Exception e3) { Log.Warning("Unable to fix code of the method. " + e3.Message); //!!!! } //create event handler { var handler = script.CreateComponent <Component_EventHandler>(enabled: false); handler.Name = handler.BaseType.GetUserFriendlyNameForInstance() + " " + _event.Name; handler.Event = ReferenceUtility.MakeReference <ReferenceValueType_Event>(null, ReferenceUtility.CalculateThisReference(handler, subscribeTo, _event.Signature)); handler.HandlerMethod = ReferenceUtility.MakeReference <ReferenceValueType_Method>(null, ReferenceUtility.CalculateThisReference(handler, script, methodSignature)); handler.Enabled = true; } //undo var action = new UndoActionComponentCreateDelete(document, new Component[] { script }, true); document.UndoSystem.CommitAction(action); document.Modified = true; }); childItem.Tag = itemData.Item2; childItems.Add(childItem); } } //add handler to one of already created C# scripts foreach (var script in subscribeTo.ParentRoot.GetComponents <Component_CSharpScript>(false, true)) { if (script.TypeSettingsIsPublic()) { var text = string.Format(Translate("Add Handler to \'{0}\'"), script.GetPathFromRoot(true)); var item = new KryptonContextMenuItem(text, null, delegate(object s, EventArgs e2) { var script2 = (Component_CSharpScript)((KryptonContextMenuItem)s).Tag; var document = Owner.DocumentWindow.Document; var oldCode = script2.Code; script2.Code = "class _Temp{\r\n}"; //activate flow graph window var scriptDocumentWindow = EditorAPI.OpenDocumentWindowForObject(document, script2) as Component_CSharpScript_DocumentWindow; Owner.DocumentWindow.SelectObjects(new object[] { script2 }); GetMethodInfo(subscribeTo, out var parameters, out var parametersSignature); //get unique method name string methodName; string methodSignature; { var methodNameNotUnique = subscribeTo.Name.Replace(" ", "") + "_" + _event.Name; for (int n = 1; ; n++) { methodName = methodNameNotUnique; if (n != 1) { methodName += n.ToString(); } methodSignature = $"method:{methodName}({parametersSignature})"; bool found = false; if (script2.CompiledScript != null) { foreach (var m in script2.CompiledScript.Methods) { if (m.Name == methodName) { found = true; } } } if (!found) { break; } //if( subscribeTo.ParentRoot.MetadataGetMemberBySignature( methodSignature ) == null ) // break; } } //GetMethodInfo( subscribeTo, out var parameters, out var methodName, out var methodSignature ); if (!scriptDocumentWindow.ScriptEditorControl.AddMethod(methodName, parameters, out var error)) { Log.Warning("Unable to add a code of the method. " + error); //!!!! } //set Code try { scriptDocumentWindow.ScriptEditorControl.GetCode(out var code); code = code.Replace("class _Temp{", ""); var index = code.LastIndexOf('}'); code = code.Substring(0, index); string newCode = ""; var lines = code.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { newCode += line.Trim() + "\r\n"; } script2.Code = oldCode + "\r\n" + newCode; } catch (Exception e3) { Log.Warning("Unable to fix code of the method. " + e3.Message); //!!!! } //create event handler Component_EventHandler handler; { handler = script2.CreateComponent <Component_EventHandler>(enabled: false); handler.Name = handler.BaseType.GetUserFriendlyNameForInstance() + " " + _event.Name; handler.Event = ReferenceUtility.MakeReference <ReferenceValueType_Event>(null, ReferenceUtility.CalculateThisReference(handler, subscribeTo, _event.Signature)); handler.HandlerMethod = ReferenceUtility.MakeReference <ReferenceValueType_Method>(null, ReferenceUtility.CalculateThisReference(handler, script2, methodSignature)); handler.Enabled = true; } //undo { var property = (Metadata.Property)script2.MetadataGetMemberBySignature("property:Code"); var undoItem = new UndoActionPropertiesChange.Item(script2, property, oldCode, new object[0]); var action1 = new UndoActionPropertiesChange(new UndoActionPropertiesChange.Item[] { undoItem }); var action2 = new UndoActionComponentCreateDelete(document, new Component[] { handler }, true); document.UndoSystem.CommitAction(new UndoMultiAction(new UndoSystem.Action[] { action1, action2 })); document.Modified = true; } }); item.Tag = script; childItems.Add(item); } } groupItem.Items.Add(new KryptonContextMenuItems(childItems.ToArray())); items.Add(groupItem); } //Add Handler to Flow Graph { var groupItem = new KryptonContextMenuItem(Translate("Add Handler to Flow Graph"), null); var childItems = new List <KryptonContextMenuItemBase>(); //create new flow graph { var itemsData = new List <(string, Component)>(); itemsData.Add((Translate("Add Flow Graph in This Component"), subscribeTo)); if (subscribeTo != subscribeTo.ParentRoot) { itemsData.Add((Translate("Add Flow Graph in the Root Component"), subscribeTo.ParentRoot)); } foreach (var itemData in itemsData) { var childItem = new KryptonContextMenuItem(itemData.Item1, null, delegate(object s, EventArgs e2) { var parent = (Component)((KryptonContextMenuItem)s).Tag; //create flow graph var graph = parent.CreateComponent <Component_FlowGraph>(enabled: false); graph.Name = graph.BaseType.GetUserFriendlyNameForInstance(); graph.Enabled = true; //create node with handler var node = AddFlowGraphNode(graph, subscribeTo); node.Position = new Vector2I(-20, -10); //undo var document = Owner.DocumentWindow.Document; var action = new UndoActionComponentCreateDelete(document, new Component[] { graph }, true); document.UndoSystem.CommitAction(action); document.Modified = true; //activate flow graph window EditorAPI.OpenDocumentWindowForObject(document, graph); }); childItem.Tag = itemData.Item2; childItems.Add(childItem); } } //add handler to one of already created flow graph foreach (var graph in subscribeTo.ParentRoot.GetComponents <Component_FlowGraph>(false, true)) { if (graph.TypeSettingsIsPublic()) { var text = string.Format(Translate("Add Handler to \'{0}\'"), graph.GetPathFromRoot(true)); var item = new KryptonContextMenuItem(text, null, delegate(object s, EventArgs e2) { var graph2 = (Component_FlowGraph)((KryptonContextMenuItem)s).Tag; //create node with handler var node = AddFlowGraphNode(graph2, subscribeTo); //undo var document = Owner.DocumentWindow.Document; var action = new UndoActionComponentCreateDelete(document, new Component[] { node }, true); document.UndoSystem.CommitAction(action); document.Modified = true; //activate flow graph window EditorAPI.OpenDocumentWindowForObject(document, graph2); }); item.Tag = graph; childItems.Add(item); } } groupItem.Items.Add(new KryptonContextMenuItems(childItems.ToArray())); items.Add(groupItem); } ////add handler (component only) //{ // var item = new KryptonContextMenuItem( Translate( "Add handler (component only)" ), null, delegate ( object s, EventArgs e2 ) // { // var newObjects = new List<Component>(); // var handler = subscribeTo.CreateComponent<Component_EventHandler>( enable: false ); // handler.Name = handler.BaseType.GetUserFriendlyNameForInstance() + " " + _event.Name; // handler.Event = ReferenceUtility.MakeReference<ReferenceValueType_Event>( null, // ReferenceUtility.CalculateThisReference( handler, subscribeTo, _event.Signature ) ); // handler.Enabled = true; // newObjects.Add( handler ); // var document = Owner.DocumentWindow.Document; // var action = new UndoActionComponentCreateDelete( document, newObjects, true ); // document.UndoSystem.CommitAction( action ); // document.Modified = true; // Owner.DocumentWindow.SelectObjects( newObjects.ToArray() ); // } ); // item.Enabled = GetOneControlledObject<Component>() != null; // items.Add( item ); //} EditorContextMenu.Show(items, Owner); }
void AddHandlerToSharpClass(Component subscribeTo, string csharpFileName, string className) { GetMethodInfo(subscribeTo, out var parameters, out var parametersSignature); //get unique method name string methodName; string methodSignature; { var methodNameNotUnique = subscribeTo.Name.Replace(" ", "") + "_" + _event.Name; for (int n = 1; ; n++) { methodName = methodNameNotUnique; if (n != 1) { methodName += n.ToString(); } methodSignature = $"method:{methodName}({parametersSignature})"; if (subscribeTo.ParentRoot.MetadataGetMemberBySignature(methodSignature) == null) { break; } } } var newObjects = new List <Component>(); //create handler var handler = subscribeTo.CreateComponent <Component_EventHandler>(enabled: false); handler.Name = handler.BaseType.GetUserFriendlyNameForInstance() + " " + _event.Name; handler.Event = ReferenceUtility.MakeReference <ReferenceValueType_Event>(null, ReferenceUtility.CalculateThisReference(handler, subscribeTo, _event.Signature)); handler.HandlerMethod = ReferenceUtility.MakeReference <ReferenceValueType_Method>(null, ReferenceUtility.CalculateThisReference(handler, subscribeTo.ParentRoot, methodSignature)); handler.Enabled = true; newObjects.Add(handler); //undo for handler var document = Owner.DocumentWindow.Document; var action = new UndoActionComponentCreateDelete(document, newObjects, true); document.UndoSystem.CommitAction(action); document.Modified = true; Owner.DocumentWindow.SelectObjects(newObjects.ToArray()); //open cs file and add method var documentWindow = EditorAPI.OpenFileAsDocument(csharpFileName, true, true) as CSharpDocumentWindow; if (documentWindow != null) { if (!documentWindow.ScriptEditorControl.AddMethod(methodName, parameters, out var error)) { Log.Warning("Unable to add a code of the method. " + error); //!!!! } } else { Log.Warning("Unable to add a code of the method. Document file is not opened."); //!!!! } }
//bool CanPaste( out Component destinationParent ) //{ // if( ClipboardManager.CheckAvailableInClipboard<ObjectCutCopyPasteData>() ) // { // destinationParent = ObjectOfWindow as Component; // if( destinationParent != null ) // return true; // } // destinationParent = null; // return false; //} public virtual bool Paste() { if (!CanPaste(out var destinationParent)) { return(false); } var data = ClipboardManager.GetFromClipboard <ObjectCutCopyPasteData>(); if (data != null) { var components = new List <Component>(); foreach (var obj in data.objects) { var c = obj as Component; if (c != null) { components.Add(c); } } //create new objects var newObjects = new List <Component>(); Vector3 addToPosition = Vector3.Zero; for (int n = 0; n < components.Count; n++) { var c = components[n]; var cloned = c.Clone(); if (destinationParent.GetComponent(c.Name) == null) { cloned.Name = c.Name; } else { cloned.Name = destinationParent.Components.GetUniqueName(c.Name, true, 2); } destinationParent.AddComponent(cloned); newObjects.Add(cloned); } if (data.cut) { //cut if (data.documentWindow.Document != Document) { //another document { var action = new UndoActionComponentCreateDelete(data.documentWindow.Document, components, false); data.documentWindow.Document.UndoSystem.CommitAction(action); data.documentWindow.Document.Modified = true; } { var action = new UndoActionComponentCreateDelete(Document, newObjects, true); Document.UndoSystem.CommitAction(action); Document.Modified = true; } } else { //same document var multiAction = new UndoMultiAction(); multiAction.AddAction(new UndoActionComponentCreateDelete(Document, components, false)); multiAction.AddAction(new UndoActionComponentCreateDelete(Document, newObjects, true)); Document.UndoSystem.CommitAction(multiAction); Document.Modified = true; } } else { //copy var action = new UndoActionComponentCreateDelete(Document, newObjects, true); Document.UndoSystem.CommitAction(action); Document.Modified = true; } } return(true); }
bool CreateObject() { creationData.ClearCreatedObjects(); //!!!!в окнах/окне делать активным после создания //creationData.selectedType = SelectedType; //creationData.replaceSelectedTypeFunction?.Invoke( this ); //!!!! //create objects { creationData.beforeCreateObjectsFunction?.Invoke(this, SelectedType); //default creation behaviour if (creationData.createdObjects == null) { creationData.createdObjects = new List <object>(); if (creationData.initParentObjects != null) { foreach (var parentObject in creationData.initParentObjects) { var parentComponent = parentObject as Component; object obj; if (parentComponent != null) { var insertIndex = EditorUtility.GetNewObjectInsertIndex(parentComponent, SelectedType); obj = parentComponent.CreateComponent(SelectedType, insertIndex, false); } else { obj = SelectedType.InvokeInstance(null); } creationData.createdObjects.Add(obj); creationData.createdObjectsToApplySettings.Add(obj); var c = obj as Component; if (c != null) { creationData.createdComponentsOnTopLevel.Add(c); } } } else { var obj = SelectedType.InvokeInstance(null); creationData.createdObjects.Add(obj); creationData.createdObjectsToApplySettings.Add(obj); var c = obj as Component; if (c != null) { creationData.createdComponentsOnTopLevel.Add(c); } } } } //!!!! //no created objects if (creationData.createdObjects.Count == 0) { //!!!! return(false); } string realFileName = ""; if (IsFileCreation()) { realFileName = VirtualPathUtility.GetRealPathByVirtual(textBoxName.Text); } //create folder for file creation if (IsFileCreation()) { var directoryName = Path.GetDirectoryName(realFileName); if (!Directory.Exists(directoryName)) { try { Directory.CreateDirectory(directoryName); } catch (Exception e) { Log.Warning(e.Message); return(false); } } } //init settings of objects bool disableFileCreation = false; foreach (var createdObject in creationData.createdObjectsToApplySettings) { if (!ApplyCreationSettingsToObject(createdObject, ref disableFileCreation)) { return(false); } } //action before enabled creationData.additionActionBeforeEnabled?.Invoke(this); //finalization of creation foreach (var component in creationData.createdComponentsOnTopLevel) { component.Enabled = true; } creationData.additionActionAfterEnabled?.Invoke(this); //foreach( var obj in createdObjects ) // creationData.additionActionAfterEnabled?.Invoke( this, obj, newObjectsFromAdditionActions ); //file creation. save to file if (IsFileCreation()) { //!!!!проверки //!!!!!!!overwrite if (creationData.createdComponentsOnTopLevel.Count == 1 && !disableFileCreation) { var createdComponent = creationData.createdComponentsOnTopLevel[0]; if (!ComponentUtility.SaveComponentToFile(createdComponent, realFileName, null, out string error)) { if (!string.IsNullOrEmpty(error)) { //!!!! Log.Warning(error); return(false); } } } //Dispose created objects for file creation mode foreach (var obj in creationData.createdObjects) { var d = obj as IDisposable; if (d != null) { d.Dispose(); } } } //update document if (!IsFileCreation()) { //update document //!!!!! var document = creationData.initDocumentWindow.Document; if (document != null) { //!!!!только компоненты? var action = new UndoActionComponentCreateDelete(document, creationData.createdComponentsOnTopLevel, true); //List<Component> created = new List<Component>(); //created.AddRange( createdComponents ); //foreach( var obj in newObjectsFromAdditionActions ) //{ // var component = obj as Component; // if( component != null ) // created.Add( component ); //} //var action = new UndoActionComponentCreateDelete( created, true ); document.UndoSystem.CommitAction(action); document.Modified = true; } else { //!!!!надо ли? Log.Warning("impl"); } } //select and open if (IsFileCreation()) { //!!!!не обязательно основное окно EditorAPI.GetRestartApplication(out var needRestart, out _); if (needRestart) { EditorSettingsSerialization.OpenFileAtStartup = realFileName; } else { //select new file in Resources window EditorAPI.SelectFilesOrDirectoriesInMainResourcesWindow(new string[] { realFileName }); //open file EditorAPI.OpenFileAsDocument(realFileName, true, true); } } else { //select created components if (creationData.createdFromContentBrowser != null) { var creator = creationData.createdFromContentBrowser; if (creator.IsHandleCreated && !creator.IsDisposed) { ContentBrowserUtility.SelectComponentItems(creator, creationData.createdComponentsOnTopLevel.ToArray()); } } else { EditorAPI.SelectComponentsInMainObjectsWindow(creationData.initDocumentWindow, creationData.createdComponentsOnTopLevel.ToArray()); } //open editor if (creationData.createdComponentsOnTopLevel.Count == 1) { var component = creationData.createdComponentsOnTopLevel[0]; if (!component.EditorReadOnlyInHierarchy) { //!!!!пока так if (component is Component_FlowGraph || component is Component_CSharpScript) { EditorAPI.OpenDocumentWindowForObject(creationData.initDocumentWindow.Document, component); } } //if( EditorAPI.IsDocumentObjectSupport( component ) && !component.EditorReadOnlyInHierarchy ) // EditorAPI.OpenDocumentWindowForObject( creationData.initDocumentWindow.Document, component ); } } //finish creation creationData.ClearCreatedObjects(); return(true); }