Ejemplo n.º 1
0
        public RGBTrack ToTrack(EditDataSet editSet, DataSet.AnimationBits bits)
        {
            RGBTrack ret = new RGBTrack();

            ret.keyframesOffset = (ushort)bits.rgbKeyframes.Count;
            ret.keyFrameCount   = (byte)gradient.keyframes.Count;
            ret.ledMask         = 0;
            foreach (int index in ledIndices)
            {
                ret.ledMask |= (uint)(1 << index);
            }

            // Add the keyframes
            foreach (var editKeyframe in gradient.keyframes)
            {
                var kf = editKeyframe.ToRGBKeyframe(editSet, bits);
                bits.rgbKeyframes.Add(kf);
            }

            return(ret);
        }
Ejemplo n.º 2
0
        public RGBTrack[] ToRGBTracks(EditDataSet editSet, DataSet.AnimationBits bits)
        {
            RGBTrack[] ret = new RGBTrack[gradients.Count];
            for (int i = 0; i < gradients.Count; ++i)
            {
                RGBTrack t = new RGBTrack();
                t.keyframesOffset = (ushort)bits.rgbKeyframes.Count;
                t.keyFrameCount   = (byte)gradients[i].keyframes.Count;
                t.ledMask         = 0;
                t.ledMask         = (uint)(1 << i);

                // Add the keyframes
                foreach (var editKeyframe in gradients[i].keyframes)
                {
                    var kf = editKeyframe.ToRGBKeyframe(editSet, bits);
                    bits.rgbKeyframes.Add(kf);
                }
                ret[i] = t;
            }

            return(ret);
        }
Ejemplo n.º 3
0
 public bool Equals(RGBTrack other)
 {
     return(keyframesOffset == other.keyframesOffset && keyFrameCount == other.keyFrameCount);
 }
Ejemplo n.º 4
0
        public AnimationSet ToAnimationSet()
        {
            AnimationSet set = new AnimationSet();

            // Collect all colors used and stuff them in the palette
            var colors = new Dictionary <Color32, int>();
            int index  = 0;

            foreach (var anim in animations)
            {
                foreach (var animTrack in anim.tracks)
                {
                    foreach (var keyframe in animTrack.keyframes)
                    {
                        var color = keyframe.color;
                        if (color.a != 0)
                        {
                            int ignore = 0;
                            if (!colors.TryGetValue(keyframe.color, out ignore))
                            {
                                colors.Add(keyframe.color, index);
                                index++;
                            }
                        }
                        // else its a special color
                    }
                }
            }

            // Add the colors to the palette
            set.palette = new byte[colors.Count * 3];
            foreach (var ci in colors)
            {
                Color32 color = ColorMapping.RemapColor(ci.Key);
                int     i     = ci.Value;
                set.palette[i * 3 + 0] = color.r;
                set.palette[i * 3 + 1] = color.g;
                set.palette[i * 3 + 2] = color.b;
            }

            int currentTrackOffset    = 0;
            int currentKeyframeOffset = 0;

            var anims     = new List <Animation>();
            var tracks    = new List <AnimationTrack>();
            var rgbTracks = new List <RGBTrack>();
            var keyframes = new List <RGBKeyframe>();

            // Add animations
            for (int animIndex = 0; animIndex < animations.Count; ++animIndex)
            {
                var editAnim = animations[animIndex];
                var anim     = new Animation();
                anim.duration         = (ushort)(editAnim.duration * 1000.0f);
                anim.tracksOffset     = (ushort)currentTrackOffset;
                anim.trackCount       = (ushort)editAnim.tracks.Sum(t => t.ledIndices.Count);
                anim.animationEvent   = (byte)editAnim.@event;
                anim.specialColorType = (byte)editAnim.@specialColorType;
                anims.Add(anim);

                // Now add tracks
                for (int j = 0; j < editAnim.tracks.Count; ++j)
                {
                    var editTrack = editAnim.tracks[j];

                    foreach (var led in editTrack.ledIndices)
                    {
                        var track = new AnimationTrack();

                        var rgbTrack = new RGBTrack();
                        rgbTrack.keyframesOffset = (ushort)currentKeyframeOffset;
                        rgbTrack.keyFrameCount   = (byte)editTrack.keyframes.Count;
                        track.trackOffset        = (ushort)rgbTracks.Count;
                        rgbTracks.Add(rgbTrack);

                        track.ledIndex = (byte)led;

                        // Now add keyframes
                        for (int k = 0; k < editTrack.keyframes.Count; ++k)
                        {
                            var editKeyframe = editTrack.keyframes[k];
                            int colorIndex   = AnimationSet.SPECIAL_COLOR_INDEX;
                            if (editKeyframe.color.a != 0)
                            {
                                colorIndex = colors[editKeyframe.color];
                            }
                            var keyframe = new RGBKeyframe();
                            keyframe.setTimeAndColorIndex((ushort)(editKeyframe.time * 1000.0f), (ushort)colorIndex);
                            keyframes.Add(keyframe);
                        }
                        currentKeyframeOffset += editTrack.keyframes.Count;

                        tracks.Add(track);
                    }
                }
                currentTrackOffset += editAnim.tracks.Sum(t => t.ledIndices.Count);
            }

            set.keyframes  = keyframes.ToArray();
            set.rgbTracks  = rgbTracks.ToArray();
            set.tracks     = tracks.ToArray();
            set.animations = anims.ToArray();

            set.Compress();
            return(set);
        }
Ejemplo n.º 5
0
        public void Compress()
        {
            // First try to find identical sets of keyframes in tracks
            for (int t = 0; t < rgbTracks.Length; ++t)
            {
                RGBTrack trackT = rgbTracks[t];
                for (int r = t + 1; r < rgbTracks.Length; ++r)
                {
                    RGBTrack trackR = rgbTracks[r];

                    // Only try to collapse tracks that are not exactly the same
                    if (!trackT.Equals(trackR))
                    {
                        if (trackR.keyFrameCount == trackT.keyFrameCount)
                        {
                            // Compare actual keyframes
                            bool kfEquals = true;
                            for (int k = 0; k < trackR.keyFrameCount; ++k)
                            {
                                var kfRk = trackR.GetKeyframe(this, (ushort)k);
                                var kfTk = trackT.GetKeyframe(this, (ushort)k);
                                if (!kfRk.Equals(kfTk))
                                {
                                    kfEquals = false;
                                    break;
                                }
                            }

                            if (kfEquals)
                            {
                                // Sweet, we can compress the keyframes
                                // Fix up any other tracks
                                for (int i = 0; i < rgbTracks.Length; ++i)
                                {
                                    RGBTrack tr = rgbTracks[i];
                                    if (tr.keyframesOffset > trackR.keyframesOffset)
                                    {
                                        tr.keyframesOffset -= trackR.keyFrameCount;
                                        rgbTracks[i]        = tr;
                                    }
                                }

                                // Remove the duplicate keyframes
                                var newKeyframes = new RGBKeyframe[keyframes.Length - trackR.keyFrameCount];
                                for (int i = 0; i < trackR.keyframesOffset; ++i)
                                {
                                    newKeyframes[i] = keyframes[i];
                                }
                                for (int i = trackR.keyframesOffset + trackR.keyFrameCount; i < keyframes.Length; ++i)
                                {
                                    newKeyframes[i - trackR.keyFrameCount] = keyframes[i];
                                }
                                keyframes = newKeyframes;

                                // And make R point to the keyframes of T
                                trackR.keyframesOffset = trackT.keyframesOffset;
                                rgbTracks[r]           = trackR;
                            }
                        }
                    }
                }
            }

            // Then remove duplicate RGB tracks
            for (int t = 0; t < rgbTracks.Length; ++t)
            {
                ref RGBTrack trackT = ref rgbTracks[t];
                for (int r = t + 1; r < rgbTracks.Length; ++r)
                {
                    ref RGBTrack trackR = ref rgbTracks[r];
                    if (trackR.Equals(trackT))
                    {
                        // Remove track R and fix anim tracks
                        // Fix up other animation tracks
                        for (int j = 0; j < tracks.Length; ++j)
                        {
                            ref AnimationTrack trj = ref tracks[j];
                            if (trj.trackOffset == r)
                            {
                                trj.trackOffset = (ushort)t;
                            }
                            else if (trj.trackOffset > r)
                            {
                                trj.trackOffset--;
                            }
                        }

                        // Remove the duplicate RGBTrack
                        var newRGBTracks = new RGBTrack[rgbTracks.Length - 1];
                        for (int j = 0; j < r; ++j)
                        {
                            newRGBTracks[j] = rgbTracks[j];
                        }
                        for (int j = r + 1; j < rgbTracks.Length; ++j)
                        {
                            newRGBTracks[j - 1] = rgbTracks[j];
                        }
                        rgbTracks = newRGBTracks;
                    }