Пример #1
0
    public void ParseKeyFrames()
    {
        LightList <StyleASTNode> nodes = StyleParser.Parse(@"
            animation anim1 {
                [keyframes] {
                    0% { BackgroundColor = red; }
                    100% { BackgroundColor = green; }
                }
            }
        ");

        Assert.AreEqual(1, nodes.Count);
        AnimationRootNode rootNode = nodes[0] as AnimationRootNode;

        KeyFrameNode keyFrameNode0 = rootNode.keyframeNodes[0];

        Assert.AreEqual(0, keyFrameNode0.keyframes[0]);
        Assert.AreEqual(1, keyFrameNode0.children.Count);
        Assert.AreEqual(StyleASTNodeType.Property, keyFrameNode0.children[0].type);

        KeyFrameNode keyFrameNode1 = rootNode.keyframeNodes[1];

        Assert.AreEqual(100, keyFrameNode1.keyframes[0]);
        Assert.AreEqual(1, keyFrameNode1.children.Count);
        Assert.AreEqual(StyleASTNodeType.Property, keyFrameNode1.children[0].type);
    }
Пример #2
0
        private void ParseAnimationKeyFrames(AnimationRootNode rootNode)
        {
            while (tokenStream.HasMoreTokens && !AdvanceIfTokenType(StyleTokenType.BracesClose))
            {
                string value = AssertTokenTypeAndAdvance(StyleTokenType.Number);
                AssertTokenTypeAndAdvance(StyleTokenType.Mod);

                KeyFrameNode keyFrameNode = StyleASTNodeFactory.KeyFrameNode(int.Parse(value));

                while (AdvanceIfTokenType(StyleTokenType.Comma))
                {
                    keyFrameNode.keyframes.Add(int.Parse(AssertTokenTypeAndAdvance(StyleTokenType.Number)));
                    AssertTokenTypeAndAdvance(StyleTokenType.Mod);
                }

                AssertTokenTypeAndAdvance(StyleTokenType.BracesOpen);

                while (tokenStream.HasMoreTokens && !AdvanceIfTokenType(StyleTokenType.BracesClose))
                {
                    ParseProperties(keyFrameNode);
                }

                rootNode.AddKeyFrameNode(keyFrameNode);
            }
        }
Пример #3
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);
        }