private static StyleProperties CreateStyleProperties(XElement element, SVGStyle style)
        {
            var attributes = element.Attributes();

            XAttribute attribute;

            if (TryGetClassAttribute(attributes, out attribute))
            {
                return(style[attribute.Value]);
            }

            if (TryGetStyleAttribute(attributes, out attribute))
            {
                return(attribute.Value.StartsWith("&")
                    ? style[attribute.Value]
                    : SVGUtils.ParseStyleContent(attribute.Value));
            }

            var styleProps = new StyleProperties();

            foreach (var attr in attributes
                     .Where(IsNotPathAttribute))
            {
                styleProps[attr.Name.LocalName] = attr.Value;
            }

            return(styleProps);
        }
    // border editing callback
    private void OnSpriteEdited(PivotEditingResult result, SVGSpriteAssetFile spriteAsset, Vector2 editedPivot, Vector4 editedBorder)
    {
        SVGCanvasBehaviour svgCanvas = target as SVGCanvasBehaviour;

        if ((svgCanvas != null) && (result == PivotEditingResult.Ok))
        {
            SVGUIAtlas uiAtlas = svgCanvas.UIAtlas;
            if (uiAtlas != null)
            {
                // assign the new border
                uiAtlas.UpdateBorder(spriteAsset, editedBorder);
                SVGUtils.MarkObjectDirty(uiAtlas);
                SVGUtils.MarkSceneDirty();

                // get the list of instantiated SVG sprites that reference the edited one
                List <GameObject> spritesInstances = new List <GameObject>();
                uiAtlas.GetSpritesInstances(spritesInstances, spriteAsset.SpriteRef);
                foreach (GameObject gameObj in spritesInstances)
                {
                    Image uiImage = (Image)gameObj.GetComponent <Image>();
                    if (uiImage != null)
                    {
                        // the Image component won't recognize the change of sprite border, so in order to refresh
                        // instantiated objects we have to unset-set the sprite property
                        uiImage.sprite = null;
                        uiImage.sprite = spriteAsset.SpriteData.Sprite;
                    }
                }
            }
        }
    }
    public bool SvgAssetAdd(TextAsset newSvg, int index)
    {
        bool ok;
        bool alreadyExist = false;

        index = SVGUtils.Clamp(index, 0, this.m_SvgList.Count);
        foreach (SVGAssetInput svgAsset in this.m_SvgList)
        {
            if (svgAsset.TxtAsset == newSvg)
            {
                alreadyExist = true;
                break;
            }
        }

        ok = this.SvgAssetAdd(newSvg, index, alreadyExist);
        if (ok)
        {
            // start with a basic identity scale offset
            this.m_SvgList.Insert(index, new SVGAssetInput(newSvg, 1.0f, false));
            // recalculate atlas hash
            this.UpdateAtlasHash();
        }
        return(ok);
    }
 private IEnumerable <Tuple <string, StyleProperties> > SelectProperties(string rawValue) =>
 SVGUtils
 .SelectClasses(regex: _regexSplitter, rawValue: rawValue)
 .SelectMany(cls => SVGUtils.CreateStylePropertiesWithID(
                 classProperties: cls,
                 idPrefix: _idPrefix,
                 contentEnding: _contentEnding,
                 contentPrefix: _contentPrefix));
 // Constructor.
 public SVGSpriteData(Sprite sprite, Vector2 pivot, Vector4 border, int zOrder, int originalX, int originalY, float generationScale, bool inCurrentInstancesGroup)
 {
     this.Sprite                  = sprite;
     this.Pivot                   = SVGUtils.FixVector2(pivot);
     this.Border                  = SVGUtils.FixVector4(border);
     this.ZOrder                  = zOrder;
     this.OriginalX               = originalX;
     this.OriginalY               = originalY;
     this.GenerationScale         = generationScale;
     this.InCurrentInstancesGroup = inCurrentInstancesGroup;
 }
    // pivot editing callback
    private void OnPivotEdited(PivotEditingResult result, SVGSpriteAssetFile spriteAsset, Vector2 editedPivot, Vector4 editedBorder)
    {
        SVGAtlas atlas = target as SVGAtlas;

        if ((atlas != null) && (result == PivotEditingResult.Ok))
        {
            // assign the new pivot
            atlas.UpdatePivot(spriteAsset, editedPivot);
            SVGUtils.MarkObjectDirty(atlas);
        }
    }
    private void DrawInspector(GameCardBehaviour card)
    {
        bool          needSpriteUpdate = false;
        bool          active           = EditorGUILayout.Toggle(new GUIContent("Active", ""), card.Active);
        bool          backSide         = EditorGUILayout.Toggle(new GUIContent("Back side", ""), card.BackSide);
        GameCardType  animalType       = (GameCardType)EditorGUILayout.EnumPopup("Animal type", card.AnimalType);
        GameBehaviour game             = EditorGUILayout.ObjectField("Game manager", card.Game, typeof(GameBehaviour), true) as GameBehaviour;

        // update active flag, if needed
        if (active != card.Active)
        {
            card.Active = active;
            SVGUtils.MarkSceneDirty();
            if (card.Game != null)
            {
                if (card.Active)
                {
                    card.Game.ShowCard(card);
                }
                else
                {
                    card.Game.HideCard(card);
                }
            }
        }
        // update back side flag, if needed
        if (backSide != card.BackSide)
        {
            card.BackSide = backSide;
            SVGUtils.MarkSceneDirty();
            needSpriteUpdate = true;
        }
        // update animal/card type, if needed
        if (animalType != card.AnimalType)
        {
            card.AnimalType = animalType;
            SVGUtils.MarkSceneDirty();
            needSpriteUpdate = true;
        }
        // update game manager, if needed
        if (game != card.Game)
        {
            card.Game = game;
            SVGUtils.MarkSceneDirty();
        }

        if (needSpriteUpdate && (card.Game != null))
        {
            card.Game.UpdateCardSprite(card);
        }
    }
Esempio n. 8
0
    public void UpdateBorder(SVGSpriteAssetFile spriteAsset, Vector4 newBorder)
    {
        SVGSpriteData spriteData = spriteAsset.SpriteData;
        Sprite        oldSprite  = spriteData.Sprite;
        // create a new sprite (same texture, same rectangle, same pivot, different border)
        Sprite newSprite = Sprite.Create(oldSprite.texture, oldSprite.rect, spriteData.Pivot, SVGBasicAtlas.SPRITE_PIXELS_PER_UNIT, 0, SpriteMeshType.FullRect, newBorder);

        spriteData.Border = newBorder;
        newSprite.name    = oldSprite.name;
        // serialized copy from the new sprite (i.e. the sprite with updated border) to the old sprite (i.e. the sprite to be updated)
        // NB: we use this technique because there isn't an explicit sprite method that allows the change of border property
        EditorUtility.CopySerialized(newSprite, oldSprite);
        SVGUtils.MarkObjectDirty(oldSprite);
        // destroy the temporary sprite
        GameObject.DestroyImmediate(newSprite);
    }
Esempio n. 9
0
    private void DrawInspector()
    {
        bool resizeOnStart   = EditorGUILayout.Toggle("Resize on Start()", this.m_EditedLoader.ResizeOnStart);
        bool updateTransform = EditorGUILayout.Toggle("Update transform", this.m_EditedLoader.UpdateTransform);

        string atlasName = (this.m_EditedLoader.Atlas != null) ? this.m_EditedLoader.Atlas.name : "<select>";

        EditorGUILayout.BeginHorizontal();
        {
            EditorGUILayout.PrefixLabel("Atlas");
            if (GUILayout.Button(atlasName, "DropDown"))
            {
                SVGAtlasSelector.Show("", this.OnAtlasSelect);
            }
        }
        EditorGUILayout.EndHorizontal();

        if (this.m_EditedLoader.Atlas != null && this.m_EditedLoader.SpriteReference != null)
        {
            SVGSpriteAssetFile spriteAsset = this.m_EditedLoader.Atlas.GetGeneratedSprite(this.m_EditedLoader.SpriteReference);
            string             buttonText  = (spriteAsset != null) ? spriteAsset.SpriteData.Sprite.name : "<select>";

            EditorGUILayout.BeginHorizontal();
            {
                EditorGUILayout.PrefixLabel("Sprite");
                if (GUILayout.Button(buttonText, "DropDown"))
                {
                    SVGSpriteSelector.Show(this.m_EditedLoader.Atlas, "", this.OnSpriteSelect);
                }
            }
            EditorGUILayout.EndHorizontal();
        }

        if (this.m_EditedLoader.ResizeOnStart != resizeOnStart)
        {
            this.m_EditedLoader.ResizeOnStart = resizeOnStart;
            SVGUtils.MarkObjectDirty(this.m_EditedLoader);
        }

        if (this.m_EditedLoader.UpdateTransform != updateTransform)
        {
            this.m_EditedLoader.UpdateTransform = updateTransform;
            SVGUtils.MarkObjectDirty(this.m_EditedLoader);
        }
    }
Esempio n. 10
0
    // used by SVGAtlasEditor, upon the "Update" button click (recalcScale = true); this function is used even
    // in the build post-processor, in order to restore sprites (recalcScale = false)
    public void UpdateEditorSprites(bool recalcScale)
    {
        float newScale;

        if (recalcScale)
        {
            Vector2 gameViewRes = SVGUtils.GetGameView();

            float currentWidth  = (this.m_DeviceTestWidth <= 0) ? gameViewRes.x : (float)this.m_DeviceTestWidth;
            float currentHeight = (this.m_DeviceTestHeight <= 0) ? gameViewRes.y : (float)this.m_DeviceTestHeight;
            newScale = SVGRuntimeGenerator.ScaleFactorCalc((float)this.m_ReferenceWidth, (float)this.m_ReferenceHeight, currentWidth, currentHeight, this.m_ScaleType, this.m_Match, this.m_OffsetScale);
        }
        else
        {
            newScale = this.m_EditorGenerationScale;
        }

        this.UpdateEditorSprites(newScale);
    }
 protected bool SvgAssetMove(SVGAssetInput svgAsset, int fromIndex, int toIndex)
 {
     if (fromIndex >= 0)
     {
         // clamp the destination index
         toIndex = SVGUtils.Clamp(toIndex, 0, this.m_SvgList.Count);
         // check if movement has sense
         if (fromIndex != toIndex)
         {
             // perform the real movement
             this.m_SvgList.Insert(toIndex, this.m_SvgList[fromIndex]);
             if (toIndex <= fromIndex)
             {
                 ++fromIndex;
             }
             this.m_SvgList.RemoveAt(fromIndex);
             return(true);
         }
     }
     return(false);
 }
    private void DrawInspector()
    {
        bool resizeOnStart;

        EditorGUILayout.BeginHorizontal();
        {
            EditorGUILayout.PrefixLabel("Resize on Start()");
            resizeOnStart = EditorGUILayout.Toggle(this.m_EditedLoader.ResizeOnStart);
        }
        EditorGUILayout.EndHorizontal();

        SVGUIAtlas uiAtlas = this.m_EditedLoader.UIAtlas;

        if ((uiAtlas != null) && (this.m_EditedLoader.SpriteReference != null))
        {
            SVGSpriteAssetFile spriteAsset = uiAtlas.GetGeneratedSprite(this.m_EditedLoader.SpriteReference);
            string             buttonText  = (spriteAsset != null) ? spriteAsset.SpriteData.Sprite.name : "<select>";

            EditorGUILayout.BeginHorizontal();
            {
                EditorGUILayout.PrefixLabel("Sprite");
                if (GUILayout.Button(buttonText, "DropDown"))
                {
                    SVGSpriteSelector.Show(uiAtlas, "", this.OnSpriteSelect);
                }
                if (GUILayout.Button("Edit", GUILayout.Width(80)))
                {
                    // show pivot editor
                    SVGPivotEditor.Show(spriteAsset, this.OnSpriteEdited);
                }
            }
            EditorGUILayout.EndHorizontal();
        }

        if (this.m_EditedLoader.ResizeOnStart != resizeOnStart)
        {
            this.m_EditedLoader.ResizeOnStart = resizeOnStart;
            SVGUtils.MarkObjectDirty(this.m_EditedLoader);
        }
    }
    public override void OnInspectorGUI()
    {
        // get the target object
        SVGCanvasBehaviour canvasBehaviour = target as SVGCanvasBehaviour;

        if ((canvasBehaviour != null) && (canvasBehaviour.UIAtlas != null))
        {
            Canvas canvas = canvasBehaviour.GetComponent <Canvas>();
            if (canvas != null)
            {
                base.OnInspectorGUI();
                // maintain the canvas scale factor synched between Canvas an SVGUIAtlas!
                canvasBehaviour.EnsureCanvasAssigned();
                bool isDirty = this.DrawInspector(canvasBehaviour.UIAtlas, canvas);
                if (isDirty)
                {
                    SVGUtils.MarkObjectDirty(canvasBehaviour.UIAtlas);
                    SVGUtils.MarkSceneDirty();
                }
            }
        }
    }
    public override void OnInspectorGUI()
    {
        if (SVGAtlasEditor.m_Styles == null)
        {
            SVGAtlasEditor.m_Styles = new Styles();
        }

        // get the target object
        SVGAtlas atlas = target as SVGAtlas;

        if (atlas != null)
        {
            base.OnInspectorGUI();
            // we assign the Name property, so we can check if the user has renamed the atlas asset file
            atlas.Name = atlas.name;
            bool isDirty = this.DrawInspector(atlas);
            if (isDirty)
            {
                SVGUtils.MarkObjectDirty(atlas);
                SVGUtils.MarkSceneDirty();
            }
        }
    }
    // border editing callback
    private void OnSpriteEdited(PivotEditingResult result, SVGSpriteAssetFile spriteAsset, Vector2 editedPivot, Vector4 editedBorder)
    {
        SVGUISpriteLoaderBehaviour spriteLoader = target as SVGUISpriteLoaderBehaviour;

        if ((spriteLoader != null) && (result == PivotEditingResult.Ok))
        {
            SVGUIAtlas uiAtlas = spriteLoader.UIAtlas;
            if (uiAtlas != null)
            {
                // assign the new border
                uiAtlas.UpdateBorder(spriteAsset, editedPivot, editedBorder);
                SVGUtils.MarkObjectDirty(uiAtlas);
                Image uiImage = spriteLoader.GetComponent <Image>();
                if (uiImage != null)
                {
                    // the Image component does not recognize the change of sprite border, so in order to refresh
                    // instantiated objects we have to unset-set the sprite property
                    uiImage.sprite = null;
                    uiImage.sprite = spriteAsset.SpriteData.Sprite;
                }
            }
        }
    }
Esempio n. 16
0
    public void UpdatePivot(SVGSpriteAssetFile spriteAsset, Vector2 newPivot)
    {
        SVGSpriteRef  spriteRef  = spriteAsset.SpriteRef;
        SVGSpriteData spriteData = spriteAsset.SpriteData;
        Sprite        oldSprite  = spriteData.Sprite;
        // keep track of pivot movement
        Vector2 deltaPivot    = newPivot - spriteData.Pivot;
        Vector2 deltaMovement = (new Vector2(deltaPivot.x * oldSprite.rect.width, deltaPivot.y * oldSprite.rect.height)) / SVGBasicAtlas.SPRITE_PIXELS_PER_UNIT;
        // create a new sprite (same texture, same rectangle, different pivot)
        Sprite newSprite = Sprite.Create(oldSprite.texture, oldSprite.rect, newPivot, SVGBasicAtlas.SPRITE_PIXELS_PER_UNIT, 0, SpriteMeshType.FullRect, spriteData.Border);

        GameObject[] allObjects = GameObject.FindObjectsOfType <GameObject>();

        foreach (GameObject gameObj in allObjects)
        {
            if (gameObj.activeInHierarchy)
            {
                SVGSpriteLoaderBehaviour loader = gameObj.GetComponent <SVGSpriteLoaderBehaviour>();
                // we must be sure that the loader component must refer to this atlas
                if (loader != null && loader.Atlas == this)
                {
                    // check if the instance uses the specified sprite
                    if (loader.SpriteReference.TxtAsset == spriteRef.TxtAsset && loader.SpriteReference.ElemIdx == spriteRef.ElemIdx)
                    {
                        SVGAtlas.UpdatePivotHierarchy(gameObj, deltaMovement, 0);
                    }
                }
            }
        }

        spriteData.Pivot = newPivot;
        newSprite.name   = oldSprite.name;
        EditorUtility.CopySerialized(newSprite, oldSprite);
        SVGUtils.MarkObjectDirty(oldSprite);
        // destroy the temporary sprite
        GameObject.DestroyImmediate(newSprite);
    }
Esempio n. 17
0
 public Plot()
     : base()
 {
     Id = SVGUtils.Id(this);
 }
    private void DrawInspector(SVGBackgroundBehaviour svgBackground)
    {
        SVGBackgroundScaleType scaleAdaption = SVGBackgroundScaleType.Vertical;
        int       size         = 256;
        int       slicedWidth  = 256;
        int       slicedHeight = 256;
        bool      needUpdate   = false;
        bool      fullUpdate   = false;
        TextAsset svgFile      = EditorGUILayout.ObjectField("SVG file", svgBackground.SVGFile, typeof(TextAsset), true) as TextAsset;
        bool      sliced       = EditorGUILayout.Toggle(new GUIContent("Sliced", "Check if you want to slice the background in order to fit specified width/height"), svgBackground.Sliced);

        if (sliced)
        {
            slicedWidth  = EditorGUILayout.IntField(new GUIContent("Width", "Sliced width, in pixels"), svgBackground.SlicedWidth);
            slicedHeight = EditorGUILayout.IntField(new GUIContent("Height", "Sliced height, in pixels"), svgBackground.SlicedHeight);
        }
        else
        {
            scaleAdaption = (SVGBackgroundScaleType)EditorGUILayout.EnumPopup("Scale adaption", svgBackground.ScaleAdaption);
            size          = EditorGUILayout.IntField(new GUIContent("Size", "Size in pixels"), svgBackground.Size);
        }
        Color clearColor      = EditorGUILayout.ColorField("Clear color", svgBackground.ClearColor);
        bool  generateOnStart = EditorGUILayout.Toggle(new GUIContent("Generate on Start()", "Generate the background texture/sprite on Start()"), svgBackground.GenerateOnStart);
        bool  fastUpload      = EditorGUILayout.Toggle(new GUIContent("Fast upload", "Use the fast native method (OpenGL/DirectX) to upload the texture to the GPU"), svgBackground.FastUpload);

        // show dimensions in pixel and world units
        EditorGUILayout.LabelField("Width (pixel unit)", svgBackground.PixelWidth.ToString());
        EditorGUILayout.LabelField("Height (pixel units)", svgBackground.PixelHeight.ToString());
        EditorGUILayout.LabelField("Width (world units)", svgBackground.WorldWidth.ToString());
        EditorGUILayout.LabelField("Height (world units)", svgBackground.WorldHeight.ToString());

        // update svg file, if needed
        if (svgFile != svgBackground.SVGFile)
        {
            svgBackground.SVGFile = svgFile;
            SVGUtils.MarkSceneDirty();
            needUpdate = true;
            // in this case we must destroy the current document and load the new one
            fullUpdate = true;
        }

        if (sliced != svgBackground.Sliced)
        {
            svgBackground.Sliced = sliced;
            SVGUtils.MarkSceneDirty();
            needUpdate = true;
        }

        if (sliced)
        {
            // update sliced width (in pixels), if needed
            if (slicedWidth != svgBackground.SlicedWidth)
            {
                svgBackground.SlicedWidth = slicedWidth;
                SVGUtils.MarkSceneDirty();
                needUpdate = true;
            }
            // update sliced height (in pixels), if needed
            if (slicedHeight != svgBackground.SlicedHeight)
            {
                svgBackground.SlicedHeight = slicedHeight;
                SVGUtils.MarkSceneDirty();
                needUpdate = true;
            }
        }
        else
        {
            // update scale adaption, if needed
            if (scaleAdaption != svgBackground.ScaleAdaption)
            {
                svgBackground.ScaleAdaption = scaleAdaption;
                SVGUtils.MarkSceneDirty();
                needUpdate = true;
            }
            // update size (in pixels), if needed
            if (size != svgBackground.Size)
            {
                svgBackground.Size = size;
                SVGUtils.MarkSceneDirty();
                needUpdate = true;
            }
        }
        // update clear color, if needed
        if (clearColor != svgBackground.ClearColor)
        {
            svgBackground.ClearColor = clearColor;
            SVGUtils.MarkSceneDirty();
            needUpdate = true;
        }
        // update "update on start" flag, if needed
        if (generateOnStart != svgBackground.GenerateOnStart)
        {
            svgBackground.GenerateOnStart = generateOnStart;
            SVGUtils.MarkSceneDirty();
        }
        // update "fast upload" flag, if needed
        if (fastUpload != svgBackground.FastUpload)
        {
            svgBackground.FastUpload = fastUpload;
            SVGUtils.MarkSceneDirty();
        }
        // update the background, if needed
        if (needUpdate)
        {
            svgBackground.UpdateBackground(fullUpdate);
        }
    }