Exemple #1
0
    public void CompileAnimation()
    {
        var        nodes      = StyleParser.Parse(@"
            animation anim1 {
                [keyframes] {
                    0% { 
                        BackgroundColor = red; 
                        BackgroundColor = red; 
                    }
                    50% {
                        TextFontSize = 11;
                        BackgroundColor = green; 
                    }
                    60% {
                        PreferredSize = 40px, 30px;
                    }
                    100% {
                         BackgroundColor = green; 
                    }
                }
            }
        ".Trim());
        StyleSheet styleSheet = NewStyleSheetCompiler().Compile("test", nodes);

        Assert.AreEqual(1, styleSheet.animations.Length);
        AnimationData animationData = styleSheet.animations[0];

        Assert.AreEqual("anim1", animationData.name);
        Assert.AreEqual(4, animationData.frames.Count);
        StyleAnimationKeyFrame frame0 = animationData.frames[0];

        Assert.AreEqual(1, frame0.properties.Count);
        Assert.AreEqual(0, frame0.key);
        Assert.AreEqual(StylePropertyId.BackgroundColor, frame0.properties[0].propertyId);
        Assert.AreEqual(Color.red, frame0.properties[0].styleProperty.AsColor);

        StyleAnimationKeyFrame frame1 = animationData.frames[1];

        Assert.AreEqual(2, frame1.properties.Count);
        Assert.AreEqual(0.5f, frame1.key);
        Assert.AreEqual(StylePropertyId.TextFontSize, frame1.properties[0].propertyId);
        Assert.AreEqual(StylePropertyId.BackgroundColor, frame1.properties[1].propertyId);

        StyleAnimationKeyFrame frame2 = animationData.frames[2];

        Assert.AreEqual(2, frame2.properties.Count);
        Assert.AreEqual(0.6f, frame2.key);
        Assert.AreEqual(StylePropertyId.PreferredWidth, frame2.properties[0].propertyId);
        Assert.AreEqual(StylePropertyId.PreferredHeight, frame2.properties[1].propertyId);

        StyleAnimationKeyFrame frame3 = animationData.frames[3];

        Assert.AreEqual(1, frame3.properties.Count);
        Assert.AreEqual(1, frame3.key);
        Assert.AreEqual(StylePropertyId.BackgroundColor, frame3.properties[0].propertyId);
    }
Exemple #2
0
        private unsafe StyleAnimationKeyFrame[] CompileKeyFrames(AnimationRootNode animNode, AnimationData[] styleSheetAnimations, UISoundData[] uiSoundData)
        {
            if (animNode.keyframeNodes == null)
            {
                // todo throw error or log warning?
                return(new StyleAnimationKeyFrame[0]);
            }

            int keyframeCount = 0;

            for (int i = 0; i < animNode.keyframeNodes.Count; i++)
            {
                keyframeCount += animNode.keyframeNodes[i].keyframes.Count;
            }

            StyleAnimationKeyFrame[] frames = new StyleAnimationKeyFrame[keyframeCount];

            int nextKeyframeIndex = 0;

            for (int i = 0; i < animNode.keyframeNodes.Count; i++)
            {
                KeyFrameNode keyFrameNode = animNode.keyframeNodes[i];

                // todo -- this is madness and not working, fix it!!!!!!!
                for (int j = 0; j < keyFrameNode.children.Count; j++)
                {
                    StyleASTNode keyFrameProperty = keyFrameNode.children[j];
                    if (keyFrameProperty is PropertyNode propertyNode)
                    {
                        StylePropertyMappers.MapProperty(s_ScratchStyle, propertyNode, context);
                    }
                    else if (keyFrameProperty is MaterialPropertyNode materialPropertyNode)
                    {
                        string materialName = materialPropertyNode.materialName;

                        if (context.materialDatabase.TryGetBaseMaterialId(materialName, out MaterialId materialId))
                        {
                            if (context.materialDatabase.TryGetMaterialProperty(materialId, materialPropertyNode.identifier, out MaterialPropertyInfo propertyInfo))
                            {
                                fixed(char *charptr = materialPropertyNode.value)
                                {
                                    CharStream            stream = new CharStream(charptr, 0, (uint)materialPropertyNode.value.Length);
                                    MaterialKeyFrameValue kfv    = default;

                                    switch (propertyInfo.propertyType)
                                    {
                                    case MaterialPropertyType.Color:

                                        if (stream.TryParseColorProperty(out Color32 color))
                                        {
                                            kfv = new MaterialKeyFrameValue(materialId, propertyInfo.propertyId, new MaterialPropertyValue2()
                                            {
                                                colorValue = color
                                            });
                                        }

                                        break;

                                    case MaterialPropertyType.Float:
                                        if (stream.TryParseFloat(out float floatVal))
                                        {
                                            kfv = new MaterialKeyFrameValue(materialId, propertyInfo.propertyId, new MaterialPropertyValue2()
                                            {
                                                floatValue = floatVal
                                            });
                                        }
                                        break;

                                    case MaterialPropertyType.Vector:
                                        break;

                                    case MaterialPropertyType.Range:
                                        break;

                                    case MaterialPropertyType.Texture:
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException();
                                    }
                                }
                            }
                        }
                    }
                }

                StructList <StyleKeyFrameValue> styleKeyValues = new StructList <StyleKeyFrameValue>(s_ScratchStyle.PropertyCount);

                for (int j = 0; j < s_ScratchStyle.PropertyCount; j++)
                {
                    styleKeyValues[j] = new StyleKeyFrameValue(s_ScratchStyle[j]);
                }

                styleKeyValues.size = s_ScratchStyle.PropertyCount;

                for (int keyframeIndex = 0; keyframeIndex < keyFrameNode.keyframes.Count; keyframeIndex++)
                {
                    float time = keyFrameNode.keyframes[keyframeIndex] / 100f;

                    frames[nextKeyframeIndex] = new StyleAnimationKeyFrame(time)
                    {
                        properties = styleKeyValues
                    };

                    nextKeyframeIndex++;
                }

                s_ScratchStyle.PropertyCount = 0;
            }

            return(frames);
        }