internal static string GenerateUXML(this VisualTreeAsset vta, string vtaPath, bool writingToFile = false) { string result = null; try { result = VisualTreeAssetToUXML.GenerateUXML(vta, vtaPath, writingToFile); } catch (Exception ex) { if (!vta.name.Contains(BuilderConstants.InvalidUXMLOrUSSAssetNameSuffix)) { var message = string.Format(BuilderConstants.InvalidUXMLDialogMessage, vta.name); BuilderDialogsUtility.DisplayDialog(BuilderConstants.InvalidUXMLDialogTitle, message); vta.name = vta.name + BuilderConstants.InvalidUXMLOrUSSAssetNameSuffix; } else { var name = vta.name.Replace(BuilderConstants.InvalidUXMLOrUSSAssetNameSuffix, string.Empty); var message = string.Format(BuilderConstants.InvalidUXMLDialogMessage, name); Builder.ShowWarning(message); } Debug.LogError(ex.Message + "\n" + ex.StackTrace); } return(result); }
public void CopyElement(VisualElement element) { var vea = element.GetVisualElementAsset(); if (vea != null) { BuilderEditorUtility.SystemCopyBuffer = VisualTreeAssetToUXML.GenerateUXML(m_PaneWindow.document.visualTreeAsset, null, vea); return; } var selector = element.GetStyleComplexSelector(); if (selector != null) { BuilderEditorUtility.SystemCopyBuffer = StyleSheetToUss.ToUssString(m_PaneWindow.document.mainStyleSheet, selector); return; } }
public void CreateTemplateFromHierarchy(VisualElement ve, VisualTreeAsset vta, string path = "") { var veas = new List <VisualElementAsset>(); var vea = ve.GetVisualElementAsset(); veas.Add(vea); if (string.IsNullOrEmpty(path)) { path = BuilderDialogsUtility.DisplaySaveFileDialog("Save UXML", null, ve.name, "uxml"); if (string.IsNullOrEmpty(path)) { // Save dialog cancelled return; } } if (path == m_PaneWindow.document.activeOpenUXMLFile.uxmlPath) { // Path is the same as the active open uxml file. Abort! BuilderDialogsUtility.DisplayDialog( BuilderConstants.InvalidCreateTemplatePathTitle, BuilderConstants.InvalidCreateTemplatePathMessage, BuilderConstants.DialogOkOption); return; } var uxml = VisualTreeAssetToUXML.GenerateUXML(vta, null, veas); if (!m_PaneWindow.document.SaveNewTemplateFileFromHierarchy(path, uxml)) { // New template wasn't saved return; } var parent = ve.parent; var parentVEA = parent.GetVisualElementAsset(); var index = parent.IndexOf(ve); // Delete old element BuilderAssetUtilities.DeleteElementFromAsset(m_PaneWindow.document, ve); ve.RemoveFromHierarchy(); // Replace with new template var newTemplateVTA = EditorGUIUtility.Load(path) as VisualTreeAsset; var newTemplateContainer = newTemplateVTA.CloneTree(); newTemplateContainer.SetProperty(BuilderConstants.LibraryItemLinkedTemplateContainerPathVEPropertyName, path); newTemplateContainer.name = newTemplateVTA.name; parent.Insert(index, newTemplateContainer); BuilderAssetUtilities.AddElementToAsset(m_PaneWindow.document, newTemplateContainer, (inVta, inParent, ve) => { var vea = inVta.AddTemplateInstance(inParent, path) as VisualElementAsset; vea.AddProperty("name", newTemplateVTA.name); ve.SetProperty(BuilderConstants.ElementLinkedInstancedVisualTreeAssetVEPropertyName, newTemplateVTA); return(vea); }, index); m_Selection.Select(null, newTemplateContainer); // Refresh m_Selection.NotifyOfHierarchyChange(); m_PaneWindow.OnEnableAfterAllSerialization(); }
internal static string GenerateUXML(this VisualTreeAsset vta, string vtaPath, bool writingToFile = false) { return(VisualTreeAssetToUXML.GenerateUXML(vta, vtaPath, writingToFile)); }
public bool CopySelection() { ClearCopyBuffer(); if (m_Selection.isEmpty) { return(false); } // UXML var veas = new List <VisualElementAsset>(); foreach (var element in m_Selection.selection) { var vea = element.GetVisualElementAsset(); if (vea == null) { veas.Clear(); break; // Mixed type selections are not supported. } // Check if current element is a child of another selected element. if (element.HasAnyAncestorInList(m_Selection.selection)) { continue; } veas.Add(vea); } if (veas.Count > 0) { BuilderEditorUtility.systemCopyBuffer = VisualTreeAssetToUXML.GenerateUXML(m_PaneWindow.document.visualTreeAsset, null, veas); return(true); } // USS var ussSnippetBuilder = new StringBuilder(); foreach (var element in m_Selection.selection) { var selector = element.GetStyleComplexSelector(); if (selector == null) { ussSnippetBuilder.Length = 0; break; // Mixed type selections are not supported. } // Check if current element is a child of another selected element. if (element.HasAnyAncestorInList(m_Selection.selection)) { continue; } var styleSheet = element.GetClosestStyleSheet(); StyleSheetToUss.ToUssString(styleSheet, selector, ussSnippetBuilder); } if (ussSnippetBuilder.Length > 0) { BuilderEditorUtility.systemCopyBuffer = ussSnippetBuilder.ToString(); return(true); } return(false); }