コード例 #1
0
        /// <summary>
        /// Function to reset the builder to the specified object state.
        /// </summary>
        /// <param name="builderObject">[Optional] The specified object state to copy.</param>
        /// <returns>The fluent builder interface.</returns>
        /// <remarks>
        /// <para>
        /// Passing <b>null</b> to the <paramref name="builderObject"/> parameter will clear the builder settings.
        /// </para>
        /// </remarks>
        public GorgonAnimationBuilder ResetTo(IGorgonAnimation builderObject = null)
        {
            Clear();

            if (builderObject == null)
            {
                return(this);
            }

            _positionMode   = builderObject.PositionTrack.InterpolationMode;
            _colorMode      = builderObject.ColorTrack.InterpolationMode;
            _rectBoundsMode = builderObject.RectBoundsTrack.InterpolationMode;
            _rotationMode   = builderObject.RotationTrack.InterpolationMode;
            _scaleMode      = builderObject.ScaleTrack.InterpolationMode;
            _sizeMode       = builderObject.SizeTrack.InterpolationMode;

            _trackPositions.Keys.AddRange(builderObject.PositionTrack.KeyFrames.Select(item => new GorgonKeyVector3(item.Time, item.Value)));
            _trackScale.Keys.AddRange(builderObject.ScaleTrack.KeyFrames.Select(item => new GorgonKeyVector3(item.Time, item.Value)));
            _trackRotation.Keys.AddRange(builderObject.RotationTrack.KeyFrames.Select(item => new GorgonKeyVector3(item.Time, item.Value)));
            _trackSize.Keys.AddRange(builderObject.SizeTrack.KeyFrames.Select(item => new GorgonKeyVector3(item.Time, item.Value)));
            _trackColors.Keys.AddRange(builderObject.ColorTrack.KeyFrames.Select(item => new GorgonKeyGorgonColor(item.Time, item.Value)));
            _trackRectBounds.Keys.AddRange(builderObject.RectBoundsTrack.KeyFrames.Select(item => new GorgonKeyRectangle(item.Time, item.Value)));
            _trackTexture2D.Keys.AddRange(builderObject.Texture2DTrack.KeyFrames.Select(item => new GorgonKeyTexture2D(item.Time,
                                                                                                                       item.Value,
                                                                                                                       item.TextureCoordinates,
                                                                                                                       item.TextureArrayIndex)));


            return(this);
        }
コード例 #2
0
        /// <summary>
        /// Function to clear the builder to a default state.
        /// </summary>
        /// <returns>The fluent builder interface.</returns>
        public GorgonAnimationBuilder Clear()
        {
            _positionMode   = TrackInterpolationMode.Linear;
            _colorMode      = TrackInterpolationMode.Linear;
            _rectBoundsMode = TrackInterpolationMode.Linear;
            _scaleMode      = TrackInterpolationMode.Linear;
            _rotationMode   = TrackInterpolationMode.Linear;
            _sizeMode       = TrackInterpolationMode.Linear;

            _trackRectBounds.Clear();
            _trackPositions.Clear();
            _trackColors.Clear();
            _trackTexture2D.Clear();
            _trackScale.Clear();
            _trackRotation.Clear();
            _trackSize.Clear();

            return(this);
        }
コード例 #3
0
 /// <summary>
 /// Function to change the interpolation mode of the <see cref="IGorgonAnimation.SizeTrack"/>.
 /// </summary>
 /// <param name="mode">The interpolation mode to apply.</param>
 /// <returns>The fluent interface for this builder.</returns>
 public GorgonAnimationBuilder SizeInterpolationMode(TrackInterpolationMode mode)
 {
     _sizeMode = mode;
     return(this);
 }
コード例 #4
0
 /// <summary>
 /// Function to change the interpolation mode of the <see cref="IGorgonAnimation.RectBoundsTrack"/>.
 /// </summary>
 /// <param name="mode">The interpolation mode to apply.</param>
 /// <returns>The fluent interface for this builder.</returns>
 public GorgonAnimationBuilder RectBoundsInterpolationMode(TrackInterpolationMode mode)
 {
     _rectBoundsMode = mode;
     return(this);
 }
コード例 #5
0
 /// <summary>
 /// Function to change the interpolation mode of the <see cref="IGorgonAnimation.ColorTrack"/>.
 /// </summary>
 /// <param name="mode">The interpolation mode to apply.</param>
 /// <returns>The fluent interface for this builder.</returns>
 public GorgonAnimationBuilder ColorInterpolationMode(TrackInterpolationMode mode)
 {
     _colorMode = mode;
     return(this);
 }
コード例 #6
0
 /// <summary>
 /// Function to change the interpolation mode of the <see cref="IGorgonAnimation.RotationTrack"/>.
 /// </summary>
 /// <param name="mode">The interpolation mode to apply.</param>
 /// <returns>The fluent interface for this builder.</returns>
 public GorgonAnimationBuilder RotationInterpolationMode(TrackInterpolationMode mode)
 {
     _rotationMode = mode;
     return(this);
 }
コード例 #7
0
        /// <summary>
        /// Function to convert a JSON formatted string into a <see cref="IGorgonAnimation"/> object.
        /// </summary>
        /// <param name="renderer">The renderer for the animation.</param>
        /// <param name="json">The JSON string containing the animation data.</param>
        /// <returns>A new <see cref="IGorgonAnimation"/>.</returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="renderer"/>, or the <paramref name="json"/> parameter is <b>null</b>.</exception>
        /// <exception cref="ArgumentEmptyException">Thrown when the <paramref name="json"/> parameter is empty.</exception>
        /// <exception cref="GorgonException">Thrown if the JSON string does not contain animation data, or there is a version mismatch.</exception>
        public IGorgonAnimation FromJson(Gorgon2D renderer, string json)
        {
            if (renderer == null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }

            if (json == null)
            {
                throw new ArgumentNullException(nameof(json));
            }

            if (string.IsNullOrWhiteSpace(json))
            {
                throw new ArgumentEmptyException(nameof(json));
            }

            string animName   = string.Empty;
            float  animLength = 0;
            int    loopCount  = 0;
            bool   isLooped   = false;

            var colorConvert   = new JsonGorgonColorKeyConverter();
            var textureConvert = new JsonTextureKeyConverter(renderer.Graphics);
            var vec3Converter  = new JsonVector3KeyConverter();
            var rectConverter  = new JsonRectKeyConverter();

            TrackInterpolationMode posInterp   = TrackInterpolationMode.None;
            TrackInterpolationMode scaleInterp = TrackInterpolationMode.None;
            TrackInterpolationMode rotInterp   = TrackInterpolationMode.None;
            TrackInterpolationMode sizeInterp  = TrackInterpolationMode.None;
            TrackInterpolationMode colorInterp = TrackInterpolationMode.None;
            TrackInterpolationMode boundInterp = TrackInterpolationMode.None;

            List <GorgonKeyVector3>     positions = null;
            List <GorgonKeyVector3>     rotations = null;
            List <GorgonKeyVector3>     scales    = null;
            List <GorgonKeyVector3>     sizes     = null;
            List <GorgonKeyGorgonColor> colors    = null;
            List <GorgonKeyRectangle>   bounds    = null;
            List <GorgonKeyTexture2D>   textures  = null;

            using (var baseReader = new StringReader(json))
                using (var reader = new JsonTextReader(baseReader))
                {
                    if (!IsReadableJObject(reader))
                    {
                        throw new GorgonException(GorgonResult.CannotRead, Resources.GOR2DIO_ERR_JSON_NOT_ANIM);
                    }

                    while (reader.Read())
                    {
                        if (reader.TokenType != JsonToken.PropertyName)
                        {
                            continue;
                        }

                        string propName = reader.Value.ToString().ToUpperInvariant();

                        switch (propName)
                        {
                        case "POSITIONS":
                            positions = ReadVector3(reader, vec3Converter, out posInterp);
                            break;

                        case "SCALES":
                            scales = ReadVector3(reader, vec3Converter, out scaleInterp);
                            break;

                        case "ROTATIONS":
                            rotations = ReadVector3(reader, vec3Converter, out rotInterp);
                            break;

                        case "SIZE":
                            sizes = ReadVector3(reader, vec3Converter, out sizeInterp);
                            break;

                        case "BOUNDS":
                            bounds = ReadRects(reader, rectConverter, out boundInterp);
                            break;

                        case "COLORS":
                            colors = ReadColors(reader, colorConvert, out colorInterp);
                            break;

                        case "TEXTURES":
                            textures = ReadTextures(reader, textureConvert);
                            break;

                        case "NAME":
                            animName = reader.ReadAsString();
                            break;

                        case "LENGTH":
                            animLength = (float)(reader.ReadAsDecimal() ?? 0);
                            break;

                        case "ISLOOPED":
                            isLooped = (reader.ReadAsBoolean() ?? false);
                            break;

                        case "LOOPCOUNT":
                            loopCount = (reader.ReadAsInt32() ?? 0);
                            break;
                        }
                    }
                }

            // There's no name, so it's a broken JSON.
            if (string.IsNullOrWhiteSpace(animName))
            {
                throw new GorgonException(GorgonResult.CannotRead, Resources.GOR2DIO_ERR_JSON_NOT_ANIM);
            }

            var builder = new GorgonAnimationBuilder();

            if ((positions != null) && (positions.Count > 0))
            {
                builder.PositionInterpolationMode(posInterp)
                .EditPositions()
                .SetKeys(positions)
                .EndEdit();
            }

            if ((scales != null) && (scales.Count > 0))
            {
                builder.ScaleInterpolationMode(scaleInterp)
                .EditScale()
                .SetKeys(scales)
                .EndEdit();
            }

            if ((rotations != null) && (rotations.Count > 0))
            {
                builder.RotationInterpolationMode(rotInterp)
                .EditRotation()
                .SetKeys(rotations)
                .EndEdit();
            }

            if ((sizes != null) && (sizes.Count > 0))
            {
                builder.SizeInterpolationMode(sizeInterp)
                .EditSize()
                .SetKeys(sizes)
                .EndEdit();
            }

            if ((colors != null) && (colors.Count > 0))
            {
                builder.ColorInterpolationMode(colorInterp)
                .EditColors()
                .SetKeys(colors)
                .EndEdit();
            }

            if ((bounds != null) && (bounds.Count > 0))
            {
                builder.RotationInterpolationMode(boundInterp)
                .EditRectangularBounds()
                .SetKeys(bounds)
                .EndEdit();
            }

            if ((textures != null) && (textures.Count > 0))
            {
                builder.Edit2DTexture()
                .SetKeys(textures)
                .EndEdit();
            }

            IGorgonAnimation result = builder.Build(animName, animLength);

            result.LoopCount = loopCount;
            result.IsLooped  = isLooped;
            result.Speed     = 1.0f;

            return(result);
        }
コード例 #8
0
        /// <summary>
        /// Function to read a track containing 3D vector data.
        /// </summary>
        /// <param name="reader">The JSON reader.</param>
        /// <param name="converter">The converter used to convert the data from JSON.</param>
        /// <param name="interpolation">The interpolation mode for the track.</param>
        /// <returns>A list of 3D vector key frames.</returns>
        private static List <GorgonKeyVector3> ReadVector3(JsonReader reader, JsonVector3KeyConverter converter, out TrackInterpolationMode interpolation)
        {
            List <GorgonKeyVector3> ReadVec3Keys()
            {
                var  keys     = new List <GorgonKeyVector3>();
                Type vec3Type = typeof(GorgonKeyVector3);

                while ((reader.Read()) && (reader.TokenType != JsonToken.EndArray))
                {
                    if (reader.TokenType != JsonToken.StartObject)
                    {
                        continue;
                    }

                    keys.Add(converter.ReadJson(reader, vec3Type, null, false, null));
                }

                return(keys);
            }

            List <GorgonKeyVector3> positions = null;

            interpolation = TrackInterpolationMode.None;

            while ((reader.Read()) && (reader.TokenType != JsonToken.EndObject))
            {
                if (reader.TokenType != JsonToken.PropertyName)
                {
                    continue;
                }

                string propName = reader.Value.ToString().ToUpperInvariant();

                switch (propName)
                {
                case "INTERPOLATIONMODE":
                    interpolation = (TrackInterpolationMode)(reader.ReadAsInt32() ?? 0);
                    break;

                case "KEYFRAMES":
                    positions = ReadVec3Keys();
                    break;
                }
            }

            return(positions);
        }