public static UiTreeRoot Parse(string psdPath) { using (var document = PsdDocument.Create(psdPath)) { var uiTree = new UiTreeRoot(); uiTree.Name = Path.GetFileName(psdPath); uiTree.Width = document.Width; uiTree.Height = document.Height; uiTree.Configs = _ParseConfig(document); PsdLayerConfig config = uiTree.Configs.GetLayerConfig(DocumentRootMagicLayerId); uiTree.Pivot = _GetPivot(config); uiTree.XAnchor = _GetXAnchorType(config); uiTree.YAnchor = _GetYAnchorType(config); var imageResource = document.ImageResources; var resolutionProperty = imageResource["Resolution"] as Reader_ResolutionInfo; int horizontalResolution = Convert.ToInt32(resolutionProperty.Value["HorizontalRes"]); uiTree.HorizontalPixelPerInch = horizontalResolution; foreach (PsdLayer layer in document.Childs) { uiTree.Children.Add(_ParsePsdLayerRecursive(uiTree, layer)); } return(uiTree); } }
// RectTransform private static Vector2 _GetPivot(PsdLayerConfig config) { float pivotX = config.GetLayerConfigAsFloat(XPivotPropertyTag, 0.5f); float pivotY = config.GetLayerConfigAsFloat(YPivotPropertyTag, 0.5f); return(new Vector2(pivotX, pivotY)); }
private static WidgetType _GetWidgetType(PsdLayerConfig config) { return(config.GetLayerConfigAsWidgetType(WidgetTypePropertyTag)); }
private static YAnchorType _GetYAnchorType(PsdLayerConfig config) { return(config.GetLayerConfigAsYAnchorType(YAnchorPropertyTag)); }
private static UiNode _ParsePsdLayerRecursive(UiTreeRoot tree, PsdLayer layer) { int id = (int)layer.Resources["lyid.ID"]; string name = layer.Name; bool isVisible = layer.IsVisible; PsdLayerConfig config = tree.Configs.GetLayerConfig(id); bool isSkipped = config.GetLayerConfigAsBool(IsSkippedPropertyTag); Vector2 pivot = _GetPivot(config); XAnchorType xAnchor = _GetXAnchorType(config); YAnchorType yAnchor = _GetYAnchorType(config); var rect = new Rect { xMin = layer.Left, xMax = layer.Right, yMin = tree.Height - layer.Bottom, yMax = tree.Height - layer.Top }; bool isGroup = _IsGroupLayer(layer); bool isText = _IsTextLayer(layer); var baseUiNode = new UiNode { Id = id, Name = name, IsVisible = isVisible, IsSkipped = isSkipped, Pivot = pivot, XAnchor = xAnchor, YAnchor = yAnchor, Rect = rect }; if (isGroup) { var children = new List <UiNode>(); foreach (PsdLayer childLayer in layer.Childs) { children.Add(_ParsePsdLayerRecursive(tree, childLayer)); } return(new GroupNode(baseUiNode) { Children = children }); } else if (isText) { var engineData = (StructureEngineData)layer.Resources["TySh.Text.EngineData"]; var engineDict = (Properties)engineData["EngineDict"]; var styleRun = (Properties)engineDict["StyleRun"]; var runArray = (ArrayList)styleRun["RunArray"]; var firstRunArrayElement = (Properties)runArray[0]; var firstStyleSheet = (Properties)firstRunArrayElement["StyleSheet"]; var firstStyleSheetData = (Properties)firstStyleSheet["StyleSheetData"]; var fontIndex = (int)firstStyleSheetData["Font"]; var fontSize = _GetFontSizeFromStyleSheetData(firstStyleSheetData); // TODO: Fix this hack fontSize = fontSize / 75 * 18; var textColor = _GetTextColorFromStyleSheetData(firstStyleSheetData); var documentResources = (Properties)engineData["DocumentResources"]; var fontSet = (ArrayList)documentResources["FontSet"]; var font = (Properties)fontSet[fontIndex]; var fontName = (string)font["Name"]; var text = (string)layer.Resources["TySh.Text.Txt"]; return(new TextNode(baseUiNode) { FontSize = fontSize, FontName = fontName, Text = text, TextColor = textColor }); } else { WidgetType widgetType = config.GetLayerConfigAsWidgetType(WidgetTypePropertyTag); Texture2D texture2D = GetTexture2DFromPsdLayer(layer); return(new ImageNode(baseUiNode) { WidgetType = widgetType, SpriteSource = texture2D != null ? new InMemoryTextureSpriteSource { Texture2D = texture2D } : (ISpriteSource) new NullSpriteSource() }); } }
public void SetLayerConfig(int id, PsdLayerConfig config) { _layerConfigList[id] = config; }