public (IComponent[], IAsset[]) Render(XdObjectJson xdObject, XdAssetHolder assetHolder, IObbGetter obbGetter) { var components = new List <IComponent>(); var assets = new List <IAsset>(); var color = xdObject.GetFillUnityColor(); var svg = SvgUtil.CreateSvg(xdObject); xdObject.Group.Children = new XdObjectJson[] { }; var size = obbGetter.Get(xdObject).Size; var spriteUid = $"{xdObject.GetSimpleName()}_{xdObject.Id.Substring(0, 8)}.png"; var svgHash = FastHash.CalculateHash(svg); var cachedSvg = assetHolder.GetCachedSvg(svgHash); if (cachedSvg != null) { spriteUid = cachedSvg.SpriteUid; } else { assets.Add(new SpriteAsset(spriteUid, svgHash, size, null, null)); var xdImportSettings = XdImporter.Settings; assetHolder.Save(spriteUid, () => SvgToPng.Convert(svg, size, xdImportSettings)); assetHolder.SaveCacheSvg(spriteUid, svgHash); } components.Add(new ImageComponent( spriteUid, new Color(1f, 1f, 1f, color.a), Vector2Int.one )); return(components.ToArray(), assets.ToArray()); }
public (IComponent[], IAsset[]) Render(XdObjectJson xdObject, Obb obb, XdAssetHolder assetHolder) { var components = new List <IComponent>(); var font = xdObject.Style.Font; var fontSize = font.Size; var color = xdObject.GetFillUnityColor(); var rawText = xdObject.Text.RawText; var textAlign = TextComponent.TextAlign.MiddleLeft; var wrap = false; var paragraphAlign = xdObject.Style?.TextAttributes?.ParagraphAlign ?? "left"; if (xdObject.Text?.Frame?.Type == "positioned") { wrap = false; if (paragraphAlign == "left") { textAlign = TextComponent.TextAlign.MiddleLeft; } if (paragraphAlign == "center") { textAlign = TextComponent.TextAlign.MiddleCenter; } if (paragraphAlign == "right") { textAlign = TextComponent.TextAlign.MiddleRight; } } if (xdObject.Text?.Frame?.Type == "area" || xdObject.Text?.Frame?.Type == "autoHeight") { wrap = true; if (paragraphAlign == "left") { textAlign = TextComponent.TextAlign.UpperLeft; } if (paragraphAlign == "center") { textAlign = TextComponent.TextAlign.UpperCenter; } if (paragraphAlign == "right") { textAlign = TextComponent.TextAlign.UpperRight; } } var lineHeight = xdObject.Style?.TextAttributes?.LineHeight; components.Add(new TextComponent(rawText, fontSize, color, textAlign, font.PostscriptName, wrap, lineHeight)); return(components.ToArray(), new IAsset[] { }); }
public static (ImageComponent, IAsset[]) RenderImage(XdObjectJson xdObject, Obb obb, XdAssetHolder assetHolder) { ImageComponent imageComponent = null; SpriteAsset asset = null; var color = xdObject.GetFillUnityColor(); var ux = xdObject.Style?.Fill?.Pattern?.Meta?.Ux; var flipX = ux?.FlipX ?? false; var flipY = ux?.FlipY ?? false; var direction = new Vector2Int(flipX ? -1 : 1, flipY ? -1 : 1); var shapeType = xdObject.Shape?.Type; if (!string.IsNullOrWhiteSpace(ux?.Uid)) { var spriteUid = $"{xdObject.GetSimpleName()}_{ux?.Uid.Substring(0, 8)}.png"; asset = new SpriteAsset(spriteUid, xdObject.Style.Fill.Pattern.Meta.Ux.HrefLastModifiedDate, obb.Size, null, null); imageComponent = new ImageComponent( spriteUid, color, direction ); assetHolder.Save(spriteUid, xdObject.Style.Fill.Pattern.Meta); } else if (SvgUtil.Types.Contains(shapeType)) { var spriteUid = $"{xdObject.GetSimpleName()}_{xdObject.Id.Substring(0, 8)}.png"; var svg = SvgUtil.CreateSvg(xdObject); var svgHash = FastHash.CalculateHash(svg); var cachedSvg = assetHolder.GetCachedSvg(svgHash); if (cachedSvg != null) { spriteUid = cachedSvg.SpriteUid; } else { asset = new SpriteAsset(spriteUid, svgHash, obb.Size, null, null); var xdImportSettings = XdImporter.Settings; assetHolder.Save(spriteUid, () => SvgToPng.Convert(svg, obb.Size, xdImportSettings)); assetHolder.SaveCacheSvg(spriteUid, svgHash); } imageComponent = new ImageComponent( spriteUid, new Color(1f, 1f, 1f, color.a), direction ); } else { Debug.LogError($"Unknown shape type {shapeType} in {xdObject.Name}({xdObject.Id}, {xdObject.Guid})"); } var assets = new List <IAsset>(); if (!xdObject.HasParameter("placeholder") && asset != null) { assets.Add(asset); } return(imageComponent, assets.ToArray()); }