void TryCreateInlineStyleSheet(VisualTreeAsset vta) { if (m_Context != null) { foreach (var e in m_Errors) { if (e.isWarning) { m_Context.LogImportWarning(e.ToString(), e.assetPath, e.line); } else { m_Context.LogImportError(e.ToString(), e.assetPath, e.line); } } } if (m_Errors.hasErrors) { // in case of errors preventing the creation of the inline stylesheet, // reset rule indices foreach (var asset in vta.visualElementAssets) { asset.ruleIndex = -1; } return; } StyleSheet inlineSheet = ScriptableObject.CreateInstance <StyleSheet>(); inlineSheet.name = "inlineStyle"; m_Builder.BuildTo(inlineSheet); vta.inlineSheet = inlineSheet; }
internal void ImportXmlFromString(string xml, out VisualTreeAsset vta) { vta = ScriptableObject.CreateInstance <VisualTreeAsset>(); vta.visualElementAssets = new List <VisualElementAsset>(); vta.templateAssets = new List <TemplateAsset>(); var h = new Hash128(); byte[] b = Encoding.UTF8.GetBytes(xml); HashUtilities.ComputeHash128(b, ref h); vta.contentHash = h.GetHashCode(); XDocument doc; try { doc = XDocument.Parse(xml, LoadOptions.SetLineInfo); } catch (Exception e) { logger.LogError(ImportErrorType.Syntax, ImportErrorCode.InvalidXml, e, Error.Level.Fatal, null); return; } LoadXmlRoot(doc, vta); StyleSheet inlineSheet = ScriptableObject.CreateInstance <StyleSheet>(); inlineSheet.name = "inlineStyle"; m_Builder.BuildTo(inlineSheet); vta.inlineSheet = inlineSheet; }
public void ChangeCanvasTheme(BuilderDocument.CanvasTheme theme, ThemeStyleSheet themeStyleSheet = null) { ApplyCanvasTheme(m_Viewport.sharedStylesAndDocumentElement, theme, themeStyleSheet); ApplyCanvasBackground(m_Viewport.canvas.defaultBackgroundElement, theme, themeStyleSheet); ApplyCanvasTheme(m_TooltipPreview, theme, themeStyleSheet); ApplyCanvasBackground(m_TooltipPreview, theme, themeStyleSheet); document.ChangeDocumentTheme(m_Viewport.documentRootElement, theme, themeStyleSheet); m_Inspector?.selection.NotifyOfStylingChange(null, null, BuilderStylingChangeType.RefreshOnly); }
public void Import(UnityStyleSheet asset, string contents) { ParserStyleSheet styleSheet = m_Parser.Parse(contents); ImportParserStyleSheet(asset, styleSheet); var h = new Hash128(); byte[] b = Encoding.UTF8.GetBytes(contents); if (b.Length > 0) { HashUtilities.ComputeHash128(b, ref h); } asset.contentHash = h.GetHashCode(); }
void ImportXml(string xmlPath, out VisualTreeAsset vta) { vta = ScriptableObject.CreateInstance <VisualTreeAsset>(); vta.visualElementAssets = new List <VisualElementAsset>(); vta.templateAssets = new List <TemplateAsset>(); var h = new Hash128(); using (var stream = File.OpenRead(xmlPath)) { int readCount = 0; byte[] b = new byte[1024 * 16]; while ((readCount = stream.Read(b, 0, b.Length)) > 0) { for (int i = readCount; i < b.Length; i++) { b[i] = 0; } Hash128 blockHash = new Hash128(); HashUtilities.ComputeHash128(b, ref blockHash); HashUtilities.AppendHash(ref blockHash, ref h); } } vta.contentHash = h.GetHashCode(); XDocument doc; try { doc = XDocument.Load(xmlPath, LoadOptions.SetLineInfo); } catch (Exception e) { logger.LogError(ImportErrorType.Syntax, ImportErrorCode.InvalidXml, e, Error.Level.Fatal, null); return; } LoadXmlRoot(doc, vta); StyleSheet inlineSheet = ScriptableObject.CreateInstance <StyleSheet>(); inlineSheet.name = "inlineStyle"; m_Builder.BuildTo(inlineSheet); vta.inlineSheet = inlineSheet; }
protected void ImportParserStyleSheet(UnityStyleSheet asset, ParserStyleSheet styleSheet) { m_Errors.assetPath = assetPath; if (styleSheet.Errors.Count > 0) { foreach (StylesheetParseError error in styleSheet.Errors) { m_Errors.AddSyntaxError(error.ToString()); } } else { try { VisitSheet(styleSheet); } catch (Exception exc) { Debug.LogException(exc); m_Errors.AddInternalError(exc.StackTrace); } } bool success = !m_Errors.hasErrors; if (success) { m_Builder.BuildTo(asset); OnImportSuccess(asset); } if (!success || m_Errors.hasWarning) { OnImportError(m_Errors); } }
protected void ImportParserStyleSheet(UnityStyleSheet asset, ParserStyleSheet styleSheet) { m_Errors.assetPath = assetPath; if (styleSheet.Errors.Count > 0) { foreach (StylesheetParseError error in styleSheet.Errors) { m_Errors.AddSyntaxError(error.ToString()); } } else { try { VisitSheet(styleSheet); } catch (Exception exc) { Debug.LogException(exc); m_Errors.AddInternalError(exc.StackTrace); } } bool success = !m_Errors.hasErrors; if (success) { m_Builder.BuildTo(asset); if (!s_StyleSheetsWithCircularImportDependencies.Contains(assetPath)) { var importDirectivesCount = styleSheet.ImportDirectives.Count; asset.imports = new UnityStyleSheet.ImportStruct[importDirectivesCount]; for (int i = 0; i < importDirectivesCount; ++i) { var importedPath = styleSheet.ImportDirectives[i].Href; string projectRelativePath, errorMessage; URIValidationResult importResult = URIHelpers.ValidAssetURL(assetPath, importedPath, out errorMessage, out projectRelativePath); UnityStyleSheet importedStyleSheet = null; if (importResult != URIValidationResult.OK) { m_Errors.AddSemanticError(ConvertErrorCode(importResult), errorMessage); } else { importedStyleSheet = DeclareDependencyAndLoad(projectRelativePath) as UnityStyleSheet; m_Context.DependsOnImportedAsset(projectRelativePath); } asset.imports[i] = new UnityStyleSheet.ImportStruct { styleSheet = importedStyleSheet, mediaQueries = styleSheet.ImportDirectives[i].Media.ToArray() }; } if (importDirectivesCount > 0) { asset.FlattenImportedStyleSheetsRecursive(); } } else { asset.imports = new UnityStyleSheet.ImportStruct[0]; var errorMsg = $"The {assetPath} contains circular @import dependencies. All @import directives will be ignored for this StyleSheet."; Debug.LogError(errorMsg); m_Errors.AddInternalError(errorMsg); } OnImportSuccess(asset); } if (!success || m_Errors.hasWarning) { OnImportError(m_Errors); } }
protected virtual void OnImportSuccess(UnityStyleSheet asset) { }
public void Import(UnityStyleSheet asset, string contents) { ParserStyleSheet styleSheet = m_Parser.Parse(contents); ImportParserStyleSheet(asset, styleSheet); }
void ApplyCanvasBackground(VisualElement element, BuilderDocument.CanvasTheme theme, ThemeStyleSheet themeStyleSheet) { if (element == null) { return; } element.RemoveFromClassList(BuilderConstants.CanvasContainerDarkStyleClassName); element.RemoveFromClassList(BuilderConstants.CanvasContainerLightStyleClassName); element.RemoveFromClassList(BuilderConstants.CanvasContainerRuntimeStyleClassName); switch (theme) { case BuilderDocument.CanvasTheme.Dark: element.AddToClassList(BuilderConstants.CanvasContainerDarkStyleClassName); break; case BuilderDocument.CanvasTheme.Light: element.AddToClassList(BuilderConstants.CanvasContainerLightStyleClassName); break; case BuilderDocument.CanvasTheme.Runtime: element.AddToClassList(BuilderConstants.CanvasContainerRuntimeStyleClassName); break; case BuilderDocument.CanvasTheme.Default: string defaultClass = EditorGUIUtility.isProSkin ? BuilderConstants.CanvasContainerDarkStyleClassName : BuilderConstants.CanvasContainerLightStyleClassName; element.AddToClassList(defaultClass); break; case BuilderDocument.CanvasTheme.Custom: element.AddToClassList(BuilderConstants.CanvasContainerRuntimeStyleClassName); break; } }
void ApplyCanvasTheme(VisualElement element, BuilderDocument.CanvasTheme theme, ThemeStyleSheet customThemeSheet) { if (element == null) { return; } // Find the runtime stylesheet. var runtimeStyleSheet = BuilderPackageUtilities.LoadAssetAtPath <StyleSheet>(BuilderConstants.RuntimeThemeUSSPath); if (runtimeStyleSheet == null) #if !UI_BUILDER_PACKAGE || UNITY_2021_1_OR_NEWER { runtimeStyleSheet = UIElementsEditorUtility.GetCommonLightStyleSheet(); } #else { runtimeStyleSheet = UIElementsEditorUtility.s_DefaultCommonLightStyleSheet; } #endif // Remove any null stylesheet. This may occur if an used theme has been deleted. // This should be handle by ui toolkit var i = 0; if (element.styleSheetList != null) { while (i < element.styleSheetList.Count) { var sheet = element.styleSheetList[i]; if (sheet == null) { element.styleSheetList?.Remove(sheet); if (element.styleSheetList.Count == 0) { element.styleSheetList = null; break; } } else { i++; } } } // Should remove the previous custom theme stylesheet if (m_LastCustomTheme) { element.styleSheets.Remove(m_LastCustomTheme); } #if !UI_BUILDER_PACKAGE || UNITY_2021_1_OR_NEWER element.styleSheets.Remove(UIElementsEditorUtility.GetCommonDarkStyleSheet()); element.styleSheets.Remove(UIElementsEditorUtility.GetCommonLightStyleSheet()); #else element.styleSheets.Remove(UIElementsEditorUtility.s_DefaultCommonDarkStyleSheet); element.styleSheets.Remove(UIElementsEditorUtility.s_DefaultCommonLightStyleSheet); #endif element.styleSheets.Remove(runtimeStyleSheet); m_Viewport.canvas.defaultBackgroundElement.style.display = DisplayStyle.Flex; StyleSheet themeStyleSheet = null; m_Viewport.canvas.checkerboardBackgroundElement.style.display = DisplayStyle.None; switch (theme) { case BuilderDocument.CanvasTheme.Dark: #if !UI_BUILDER_PACKAGE || UNITY_2021_1_OR_NEWER themeStyleSheet = UIElementsEditorUtility.GetCommonDarkStyleSheet(); #else themeStyleSheet = UIElementsEditorUtility.s_DefaultCommonDarkStyleSheet; #endif break; case BuilderDocument.CanvasTheme.Light: #if !UI_BUILDER_PACKAGE || UNITY_2021_1_OR_NEWER themeStyleSheet = UIElementsEditorUtility.GetCommonLightStyleSheet(); #else themeStyleSheet = UIElementsEditorUtility.s_DefaultCommonLightStyleSheet; #endif break; case BuilderDocument.CanvasTheme.Runtime: themeStyleSheet = runtimeStyleSheet; m_Viewport.canvas.defaultBackgroundElement.style.display = DisplayStyle.None; m_Viewport.canvas.checkerboardBackgroundElement.style.display = DisplayStyle.Flex; break; case BuilderDocument.CanvasTheme.Default: themeStyleSheet = null; break; case BuilderDocument.CanvasTheme.Custom: m_LastCustomTheme = customThemeSheet; themeStyleSheet = customThemeSheet; break; } if (themeStyleSheet != null) { element.styleSheets.Add(themeStyleSheet); } }
public void ChangeDocumentTheme(VisualElement documentElement, CanvasTheme canvasTheme, ThemeStyleSheet themeSheet) { m_CurrentCanvasTheme = canvasTheme; m_CurrentCanvasThemeStyleSheet = themeSheet; RefreshStyle(documentElement); }
protected void ImportParserStyleSheet(UnityStyleSheet asset, ParserStyleSheet styleSheet) { m_Errors.assetPath = assetPath; if (styleSheet.Errors.Count > 0) { foreach (StylesheetParseError error in styleSheet.Errors) { m_Errors.AddSyntaxError(string.Format(glossary.ussParsingError, error.Message), error.Line); } } else { try { VisitSheet(styleSheet); } catch (System.Exception exc) { m_Errors.AddInternalError(string.Format(glossary.internalErrorWithStackTrace, exc.Message, exc.StackTrace), m_CurrentLine); } } bool hasErrors = m_Errors.hasErrors; if (!hasErrors) { m_Builder.BuildTo(asset); if (!s_StyleSheetsWithCircularImportDependencies.Contains(assetPath)) { var importDirectivesCount = styleSheet.ImportDirectives.Count; asset.imports = new UnityStyleSheet.ImportStruct[importDirectivesCount]; for (int i = 0; i < importDirectivesCount; ++i) { var importedPath = styleSheet.ImportDirectives[i].Href; var response = URIHelpers.ValidateAssetURL(assetPath, importedPath); var importResult = response.result; var errorToken = response.errorToken; var projectRelativePath = response.resolvedProjectRelativePath; if (response.hasWarningMessage) { m_Errors.AddValidationWarning(response.warningMessage, m_CurrentLine); } UnityStyleSheet importedStyleSheet = null; if (importResult != URIValidationResult.OK) { var(code, message) = ConvertErrorCode(importResult); m_Errors.AddSemanticWarning(code, string.Format(message, errorToken), m_CurrentLine); } else { importedStyleSheet = response.resolvedQueryAsset as UnityStyleSheet; if (importedStyleSheet) { m_Context.DependsOnSourceAsset(projectRelativePath); } else { importedStyleSheet = DeclareDependencyAndLoad(projectRelativePath) as UnityStyleSheet; } if (!response.isLibraryAsset) { m_Context.DependsOnImportedAsset(projectRelativePath); } } asset.imports[i] = new UnityStyleSheet.ImportStruct { styleSheet = importedStyleSheet, mediaQueries = styleSheet.ImportDirectives[i].Media.ToArray() }; } if (importDirectivesCount > 0) { asset.FlattenImportedStyleSheetsRecursive(); } } else { asset.imports = new UnityStyleSheet.ImportStruct[0]; m_Errors.AddValidationWarning(glossary.circularImport, -1); } OnImportSuccess(asset); } bool hasWarnings = m_Errors.hasWarning; asset.importedWithErrors = hasErrors; asset.importedWithWarnings = hasWarnings; if (hasErrors || hasWarnings) { OnImportError(m_Errors); } }
public RuleMatcher(StyleSheet sheet, StyleComplexSelector complexSelector, int styleSheetIndexInStack) { this.sheet = sheet; this.complexSelector = complexSelector; }
private static void ParseVarFunction(StyleSheet sheet, StyleValueHandle[] handles, ref int index, out int argCount, out string variableName) { argCount = (int)sheet.ReadFloat(handles[++index]); variableName = sheet.ReadVariable(handles[++index]); }
public StyleVariable(string name, StyleSheet sheet, StyleValueHandle[] handles) { this.name = name; this.sheet = sheet; this.handles = handles; }