private static void ReadFontStyle(StyleSheet sheet, StyleRule rule, bool throwIfNotFound, GUIStyle style) { string fontStyleStr = null; string weight = null; GetProperty(rule, ConverterUtils.k_FontStyle, throwIfNotFound, property => { fontStyleStr = sheet.ReadEnum(property.values[0]); }); GetProperty(rule, ConverterUtils.k_FontWeight, throwIfNotFound, property => { weight = sheet.ReadEnum(property.values[0]); }); FontStyle fontStyle; if (ConverterUtils.TryGetFontStyle(fontStyleStr, weight, out fontStyle)) { style.fontStyle = fontStyle; } }
internal static StyleSheetResolver ResolveFromSheetsFolder(IEnumerable <string> folders, SkinTarget target, StyleSheetResolver.ResolvingOptions options = null, string sheetPostFix = "") { var sheetPaths = ConverterUtils.GetSheetPathsFromRootFolders(folders, target, sheetPostFix); if (sheetPaths.Length == 0) { throw new Exception("Cannot find sheets to generate skin"); } var resolver = new StyleSheetResolver(options ?? new StyleSheetResolver.ResolvingOptions() { ThrowIfCannotResolve = true }); foreach (var sheet in sheetPaths) { resolver.AddStyleSheets(sheet); } return(resolver); }
private static void ReadRectOffset(StyleSheetCache cache, StyleRule rule, string name, string suffix, bool throwIfNotFound, RectOffset offset) { var sheet = cache.sheet; GetProperty(rule, ConverterUtils.ToUssPropertyName(name, "left", suffix), throwIfNotFound, property => { offset.left = (int)sheet.ReadFloat(property.values[0]); }); GetProperty(rule, ConverterUtils.ToUssPropertyName(name, "right", suffix), throwIfNotFound, property => { offset.right = (int)sheet.ReadFloat(property.values[0]); }); GetProperty(rule, ConverterUtils.ToUssPropertyName(name, "top", suffix), throwIfNotFound, property => { offset.top = (int)sheet.ReadFloat(property.values[0]); }); GetProperty(rule, ConverterUtils.ToUssPropertyName(name, "bottom", suffix), throwIfNotFound, property => { offset.bottom = (int)sheet.ReadFloat(property.values[0]); }); }
internal static GUIStyle FromUSS(string ussStyleRuleName, string ussInPlaceStyleOverride = null, GUISkin srcSkin = null) { if (GUISkin.current == null) { return(null); } // Check if the style already exists in skin var blockName = RuleNameToBlockName(ussStyleRuleName); var styleName = ConverterUtils.ToStyleName(ussStyleRuleName); var inSkin = (srcSkin ? srcSkin : GUISkin.current).FindStyle(styleName); var style = new GUIStyle() { name = styleName }; if (inSkin != null) { style.Assign(inSkin); } PopulateFromUSS(EditorResources.styleCatalog, style, blockName, ussInPlaceStyleOverride); return(style); }
private static void AddProperty(StyleSheetBuilderHelper helper, string name, string suffix, RectOffset offset, RectOffset defaultValue, string comment = "") { // Note: Same order as CSS which is NOT the same order as the RectOffset constructor if (helper.options.exportDefaultValues || offset.left != defaultValue.left) { helper.AddProperty(ConverterUtils.ToUssPropertyName(name, "left", suffix), offset.left, comment); } if (helper.options.exportDefaultValues || offset.right != defaultValue.right) { helper.AddProperty(ConverterUtils.ToUssPropertyName(name, "right", suffix), offset.right); } if (helper.options.exportDefaultValues || offset.top != defaultValue.top) { helper.AddProperty(ConverterUtils.ToUssPropertyName(name, "top", suffix), offset.top); } if (helper.options.exportDefaultValues || offset.bottom != defaultValue.bottom) { helper.AddProperty(ConverterUtils.ToUssPropertyName(name, "bottom", suffix), offset.bottom); } }
// Public API to serialize GUIStyle and GUISkin public static void AddStyle(StyleSheetBuilderHelper helper, string name, GUIStyle style, GUIStyle defaultStyle = null, string extendName = null) { defaultStyle = defaultStyle ?? GUIStyle.none; // All common Style property helper.BeginRule(); using (helper.builder.BeginComplexSelector(0)) { // Construct rule according to the GUIStyle -> is it custom? is it bound on a type? helper.builder.AddSimpleSelector(new[] { ConverterUtils.CreateSelectorPart(name) }, StyleSelectorRelationship.None); } if (!string.IsNullOrEmpty(extendName)) { helper.AddPropertyString(ConverterUtils.k_Extend, extendName); } // Loop for each GUIStyle property if (helper.options.exportDefaultValues || style.alignment != defaultStyle.alignment) { helper.AddProperty(ConverterUtils.k_TextAlignment, ConverterUtils.ToUssString(style.alignment), "GUIStyle.alignment"); } if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.border, defaultStyle.border)) { AddProperty(helper, ConverterUtils.k_Border, "", style.border, defaultStyle.border, "GUIStyle.border"); } if (helper.options.exportDefaultValues || style.clipping != defaultStyle.clipping) { helper.AddProperty(ConverterUtils.k_Clipping, ConverterUtils.ToUssString(style.clipping), "GUIStyle.clipping"); } if (helper.options.exportDefaultValues || style.contentOffset != defaultStyle.contentOffset) { helper.AddProperty(ConverterUtils.k_ContentOffset, style.contentOffset, "GUIStyle.contentOffset"); } if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.fixedHeight, defaultStyle.fixedHeight)) { helper.AddProperty(ConverterUtils.k_Height, style.fixedHeight, "GUIStyle.fixedHeight"); } if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.fixedWidth, defaultStyle.fixedWidth)) { helper.AddProperty(ConverterUtils.k_Width, style.fixedWidth, "GUIStyle.fixedWidth"); } if (helper.options.exportDefaultValues || style.font != defaultStyle.font) { AddPropertyResource(helper, ConverterUtils.k_Font, style.font, "GUIStyle.font"); } if (helper.options.exportDefaultValues || style.fontSize != defaultStyle.fontSize) { helper.AddProperty(ConverterUtils.k_FontSize, style.fontSize, "GUIStyle.fontSize"); } if (helper.options.exportDefaultValues || style.fontStyle != defaultStyle.fontStyle) { AddProperty(helper, style.fontStyle, "GUIStyle.fontSize"); } if (helper.options.exportDefaultValues || style.imagePosition != defaultStyle.imagePosition) { helper.AddProperty(ConverterUtils.k_ImagePosition, ConverterUtils.ToUssString(style.imagePosition), "GUIStyle.imagePosition"); } if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.margin, defaultStyle.margin)) { AddProperty(helper, ConverterUtils.k_Margin, null, style.margin, defaultStyle.margin, "GUIStyle.margin"); } // Always export name: helper.AddPropertyString(ConverterUtils.k_Name, style.name, "GUIStyle.name"); if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.overflow, defaultStyle.overflow)) { AddProperty(helper, ConverterUtils.k_Overflow, null, style.overflow, defaultStyle.overflow, "GUIStyle.overflow"); } if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.padding, defaultStyle.padding)) { AddProperty(helper, ConverterUtils.k_Padding, null, style.padding, defaultStyle.padding, "GUIStyle.padding"); } if (helper.options.exportDefaultValues || style.richText != defaultStyle.richText) { helper.AddProperty(ConverterUtils.k_RichText, style.richText, "GUIStyle.richText"); } if (helper.options.exportDefaultValues || style.stretchHeight != defaultStyle.stretchHeight) { helper.AddProperty(ConverterUtils.k_StretchHeight, style.stretchHeight, "GUIStyle.stretchHeight"); } if (helper.options.exportDefaultValues || style.stretchWidth != defaultStyle.stretchWidth) { helper.AddProperty(ConverterUtils.k_StretchWidth, style.stretchWidth, "GUIStyle.stretchWidth"); } if (helper.options.exportDefaultValues || style.wordWrap != defaultStyle.wordWrap) { helper.AddProperty(ConverterUtils.k_WordWrap, style.wordWrap, "GUIStyle.wordWrap"); } // Add Normal state properties if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.normal, defaultStyle.normal)) { AddState(helper, style.normal, defaultStyle.normal); } helper.EndRule(); // Add one rule for each GUIStyleState (other than normal) if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.active, defaultStyle.active)) { AddState(helper, name, style.active, "active", defaultStyle.active); } if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.focused, defaultStyle.focused)) { AddState(helper, name, style.focused, "focused", defaultStyle.focused); } if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.hover, defaultStyle.hover)) { AddState(helper, name, style.hover, "hover", defaultStyle.hover); } if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.onActive, defaultStyle.onActive)) { AddState(helper, name, style.onActive, "onActive", defaultStyle.onActive); } if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.onFocused, defaultStyle.onFocused)) { AddState(helper, name, style.onFocused, "onFocused", defaultStyle.onFocused); } if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.onHover, defaultStyle.onHover)) { AddState(helper, name, style.onHover, "onHover", defaultStyle.onHover); } if (helper.options.exportDefaultValues || !GUISkinCompare.CompareTo(style.onNormal, defaultStyle.onNormal)) { AddState(helper, name, style.onNormal, "onNormal", defaultStyle.onNormal); } }
internal static GUISkin CreateGUISkinFromSheetsFolder(SkinTarget target, IEnumerable <string> folders, GUISkin skin = null) { skin = skin ?? ConverterUtils.CreateDefaultGUISkin(); PopulateSkin(target, folders, skin); return(skin); }
private static void PopulateStyle(StyleSheetCache cache, StyleComplexSelector complexSelector, GUIStyle style, bool throwIfIncomplete = false) { var rule = complexSelector.rule; var complexSelectorStr = StyleSheetToUss.ToUssSelector(complexSelector); var sheet = cache.sheet; // GUIStyle.alignment GetProperty(rule, ConverterUtils.k_TextAlignment, throwIfIncomplete, property => { style.alignment = ConverterUtils.ToTextAnchor(sheet.ReadEnum(property.values[0])); }); // GUIStyle.border ReadRectOffset(cache, rule, ConverterUtils.k_Border, "", throwIfIncomplete, style.border); // GUIStyle.clipping GetProperty(rule, ConverterUtils.k_Clipping, throwIfIncomplete, property => { style.clipping = ConverterUtils.ToTextClipping(sheet.ReadEnum(property.values[0])); }); // GUIStyle.contentOffset GetProperty(rule, ConverterUtils.k_ContentOffset, throwIfIncomplete, property => { style.contentOffset = StyleSheetBuilderHelper.ReadVector2(sheet, property); }); // GUIStyle.fixedHeight GetProperty(rule, ConverterUtils.k_Height, throwIfIncomplete, property => { style.fixedHeight = sheet.ReadFloat(property.values[0]); }); // GUIStyle.fixedWidth GetProperty(rule, ConverterUtils.k_Width, throwIfIncomplete, property => { style.fixedWidth = sheet.ReadFloat(property.values[0]); }); // GUIStyle.font GetProperty(rule, ConverterUtils.k_Font, false, property => { style.font = ReadResource <Font>(sheet, property); }); // GUIStyle.fixedWidth GetProperty(rule, ConverterUtils.k_FontSize, throwIfIncomplete, property => { style.fontSize = (int)sheet.ReadFloat(property.values[0]); }); // GUIStyle.fontStyle ReadFontStyle(sheet, rule, throwIfIncomplete, style); // GUIStyle.imagePosition GetProperty(rule, ConverterUtils.k_ImagePosition, throwIfIncomplete, property => { style.imagePosition = ConverterUtils.ToImagePosition(sheet.ReadEnum(property.values[0])); }); // GUIStyle.margin ReadRectOffset(cache, rule, ConverterUtils.k_Margin, null, throwIfIncomplete, style.margin); // GUIStyle.name GetProperty(rule, ConverterUtils.k_Name, throwIfIncomplete, property => { style.name = sheet.ReadString(property.values[0]); }); // GUIStyle.overflow ReadRectOffset(cache, rule, ConverterUtils.k_Overflow, null, throwIfIncomplete, style.overflow); // GUIStyle.padding ReadRectOffset(cache, rule, ConverterUtils.k_Padding, null, throwIfIncomplete, style.padding); // GUIStyle.richText GetProperty(rule, ConverterUtils.k_RichText, throwIfIncomplete, property => { style.richText = ReadBool(sheet, property); }); // GUIStyle.stretchHeight GetProperty(rule, ConverterUtils.k_StretchHeight, throwIfIncomplete, property => { style.stretchHeight = ReadBool(sheet, property); }); // GUIStyle.stretchWidth GetProperty(rule, ConverterUtils.k_StretchWidth, throwIfIncomplete, property => { style.stretchWidth = ReadBool(sheet, property); }); // GUIStyle.wordWrap GetProperty(rule, ConverterUtils.k_WordWrap, throwIfIncomplete, property => { style.wordWrap = ReadBool(sheet, property); }); ReadState(cache, rule, style.normal, throwIfIncomplete); ReadState(cache, complexSelectorStr, style.active, "active", throwIfIncomplete); ReadState(cache, complexSelectorStr, style.focused, "focused", throwIfIncomplete); ReadState(cache, complexSelectorStr, style.hover, "hover", throwIfIncomplete); ReadState(cache, complexSelectorStr, style.onActive, "onActive", throwIfIncomplete); ReadState(cache, complexSelectorStr, style.onFocused, "onFocused", throwIfIncomplete); ReadState(cache, complexSelectorStr, style.onHover, "onHover", throwIfIncomplete); ReadState(cache, complexSelectorStr, style.onNormal, "onNormal", throwIfIncomplete); }
internal static void PopulateStyle(StyleCatalog catalog, GUIStyle style, string blockName, bool useExtensionDefaultValues = true) { if (string.IsNullOrEmpty(blockName)) { blockName = ConverterUtils.EscapeSelectorName(style.name); } var styleBlock = catalog.GetStyle(blockName); var rootBlock = catalog.GetStyle(StyleCatalogKeyword.root, StyleState.root); style.name = styleBlock.GetText(ConverterUtils.k_Name, style.name); if (string.IsNullOrEmpty(style.name)) { style.name = BlockNameToStyleName(blockName); } style.fixedWidth = styleBlock.GetFloat(StyleCatalogKeyword.width, style.fixedWidth); style.fixedHeight = styleBlock.GetFloat(StyleCatalogKeyword.height, style.fixedHeight); GetStyleRectOffset(styleBlock, "margin", style.margin); GetStyleRectOffset(styleBlock, "padding", style.padding); style.stretchHeight = styleBlock.GetBool("-unity-stretch-height".GetHashCode(), style.stretchHeight); style.stretchWidth = styleBlock.GetBool("-unity-stretch-width".GetHashCode(), style.stretchWidth); GetStyleRectOffset(styleBlock, "-unity-slice", style.border); GetStyleRectOffset(styleBlock, "-unity-overflow", style.overflow); var contentOffsetKey = "-unity-content-offset".GetHashCode(); if (styleBlock.HasValue(contentOffsetKey, StyleValue.Type.Rect)) { var contentOffsetSize = styleBlock.GetRect(contentOffsetKey); style.contentOffset = new Vector2(contentOffsetSize.width, contentOffsetSize.height); } // Support both properties for font: style.font = styleBlock.GetResource <Font>("-unity-font".GetHashCode(), style.font); style.font = styleBlock.GetResource <Font>("font".GetHashCode(), style.font); if (style.fontSize == 0 || styleBlock.HasValue(StyleCatalogKeyword.fontSize, StyleValue.Type.Number)) { style.fontSize = styleBlock.GetInt(StyleCatalogKeyword.fontSize, style.fontSize); } var fontStyleStr = styleBlock.GetText(ConverterUtils.k_FontStyle.GetHashCode()); var fontWeightStr = styleBlock.GetText(ConverterUtils.k_FontWeight.GetHashCode()); FontStyle fontStyle; if (ConverterUtils.TryGetFontStyle(fontStyleStr, fontWeightStr, out fontStyle)) { style.fontStyle = fontStyle; } style.imagePosition = ConverterUtils.ToImagePosition(styleBlock.GetText("-unity-image-position".GetHashCode(), ConverterUtils.ToUssString(style.imagePosition))); style.clipping = ConverterUtils.ToTextClipping(styleBlock.GetText("-unity-clipping".GetHashCode(), ConverterUtils.ToUssString(style.clipping))); style.alignment = ConverterUtils.ToTextAnchor(styleBlock.GetText("-unity-text-align".GetHashCode(), ConverterUtils.ToUssString(style.alignment))); style.richText = styleBlock.GetBool("-unity-rich-text".GetHashCode(), style.richText); style.wordWrap = styleBlock.GetBool("-unity-word-wrap".GetHashCode(), style.wordWrap); var defaultStyleState = useExtensionDefaultValues ? new GUIStyleState() { textColor = styleBlock.GetColor(StyleCatalogKeyword.color, rootBlock.GetColor("--unity-text-color")) } : null; PopulateStyleState(styleBlock, style.normal, defaultStyleState); PopulateStyleState(catalog.GetStyle(blockName, StyleState.hover), style.hover, defaultStyleState); PopulateStyleState(catalog.GetStyle(blockName, StyleState.focus), style.focused, defaultStyleState); // Supports GUISkin Generation selector that assumes GUIStyle.active maps to :hover:active PopulateStyleState(catalog.GetStyle(blockName, StyleState.hover | StyleState.active), style.active, defaultStyleState); PopulateStyleState(catalog.GetStyle(blockName, StyleState.active), style.active, null); //// All "on" states uses their parent pseudo class (without :checked) as their default value PopulateStyleState(catalog.GetStyle(blockName, StyleState.@checked), style.onNormal, useExtensionDefaultValues ? style.normal : null); PopulateStyleState(catalog.GetStyle(blockName, StyleState.@checked | StyleState.hover), style.onHover, useExtensionDefaultValues ? style.hover : null); // Supports GUISkin Generation selector that assumes GUIStyle.onActive maps to :hover:active:checked PopulateStyleState(catalog.GetStyle(blockName, StyleState.@checked | StyleState.active), style.onActive, useExtensionDefaultValues ? style.active : null); PopulateStyleState(catalog.GetStyle(blockName, StyleState.@checked | StyleState.hover | StyleState.active), style.onActive, null); // Supports GUISkin Generation selector that assumes GUIStyle.onFocused maps to:hover:focus:checked PopulateStyleState(catalog.GetStyle(blockName, StyleState.@checked | StyleState.focus), style.onFocused, useExtensionDefaultValues ? style.focused : null); PopulateStyleState(catalog.GetStyle(blockName, StyleState.@checked | StyleState.hover | StyleState.focus), style.onFocused, null); }