Example #1
0
    // Use this for initialization
    void Start()
    {
        Animations.AnimationSet set = new Animations.AnimationSet();
        set.animations = new Animations.RGBAnimation[5];
        for (int a = 0; a < set.animations.Length; ++a)
        {
            Animations.RGBAnimation anim = new Animations.RGBAnimation();
            anim.tracks   = new Animations.RGBAnimationTrack[a + 1];
            anim.duration = 2500;
            for (int i = 0; i < anim.tracks.Length; ++i)
            {
                var keyframes = new Animations.RGBKeyframe[Animations.Constants.MAX_KEYFRAMES];
                for (int j = 0; j < 3; ++j)
                {
                    keyframes[j].time  = (byte)j;
                    keyframes[j].red   = (byte)j;
                    keyframes[j].green = (byte)j;
                    keyframes[j].blue  = (byte)j;
                }

                var track = new Animations.RGBAnimationTrack();
                track.keyframes = keyframes;
                track.count     = 3;
                track.duration  = (short)(2000 - 10 * i);
                track.startTime = (short)(123 + i);
                track.ledIndex  = (byte)(i);
                anim.tracks[i]  = track;
            }

            set.animations[a] = anim;

            var bytes = Animations.RGBAnimation.ToByteArray(anim);
            Debug.Log(bytes.ToString());
        }
    }
 public void UploadCurrentAnim(Animations.AnimationSet animSet)
 {
     // Try to send the anim down!
     foreach (var die in dice)
     {
         Debug.Log("Uploading anim on die " + die.name);
         StartCoroutine(die.UploadAnimationSet(animSet));
     }
 }
Example #3
0
        public void FromAnimationSet(AnimationSet set)
        {
            // Reset the animations, and read them in!
            animations = new List <EditAnimation>();
            for (int i = 0; i < set.animations.Length; ++i)
            {
                var anim     = set.getAnimation((ushort)i);
                var editAnim = new EditAnimation();
                for (int j = 0; j < anim.trackCount; ++j)
                {
                    var track     = anim.GetTrack(set, (ushort)j);
                    var editTrack = new EditTrack();
                    editTrack.ledIndices = new List <int>();
                    editTrack.ledIndices.Add(track.ledIndex);
                    var rgbTrack = track.GetTrack(set);
                    for (int k = 0; k < rgbTrack.keyFrameCount; ++k)
                    {
                        var kf     = rgbTrack.GetKeyframe(set, (ushort)k);
                        var editKf = new EditKeyframe();
                        editKf.time = (float)kf.time() / 1000.0f;
                        if (kf.colorIndex() == AnimationSet.SPECIAL_COLOR_INDEX)
                        {
                            editKf.color = new Color32(255, 255, 255, 0); // Important part is alpha
                        }
                        else
                        {
                            editKf.color = ColorMapping.InverseRemap(set.getColor(kf.colorIndex()));
                        }
                        editTrack.keyframes.Add(editKf);
                    }
                    editAnim.tracks.Add(editTrack);
                }

                // De-duplicate tracks
                for (int l = 0; l < editAnim.tracks.Count; ++l)
                {
                    for (int m = l + 1; m < editAnim.tracks.Count; ++m)
                    {
                        if (editAnim.tracks[m].keyframes.SequenceEqual(editAnim.tracks[l].keyframes, EditKeyframe.DefaultComparer))
                        {
                            // Concatenate the leds
                            editAnim.tracks[l].ledIndices.AddRange(editAnim.tracks[m].ledIndices);

                            // Remove the second edit anim
                            editAnim.tracks.RemoveAt(m);
                            m--;
                        }
                    }
                }

                editAnim.@event            = (Die.AnimationEvent)anim.animationEvent;
                editAnim.@specialColorType = (Die.SpecialColor)anim.specialColorType;
                animations.Add(editAnim);
            }
        }
Example #4
0
 void Awake()
 {
     _animSet = new Animations.AnimationSet
     {
         animations = Enumerable.Range(0, 6)
                      .Select(i => new Animations.RGBAnimation()
         {
             duration = 1000, tracks = new Animations.RGBAnimationTrack[0]
         })
                      .ToArray()
     };
     _widthPadding = (transform as RectTransform).rect.width - _ticksRoot.rect.width;
     Duration      = _minDuration;
     Zoom          = _minZoom;
     Clear();
 }
Example #5
0
        public uint evaluate(AnimationSet set, int time)
        {
            if (keyFrameCount == 0)
            {
                return(0);
            }

            // Find the first keyframe
            ushort nextIndex = 0;

            while (nextIndex < keyFrameCount && GetKeyframe(set, nextIndex).time() < time)
            {
                nextIndex++;
            }

            if (nextIndex == 0)
            {
                // The first keyframe is already after the requested time, clamp to first value
                return(GetKeyframe(set, nextIndex).color(set));
            }
            else if (nextIndex == keyFrameCount)
            {
                // The last keyframe is still before the requested time, clamp to the last value
                return(GetKeyframe(set, (ushort)(nextIndex - 1)).color(set));
            }
            else
            {
                // Grab the prev and next keyframes
                var    nextKeyframe      = GetKeyframe(set, nextIndex);
                ushort nextKeyframeTime  = nextKeyframe.time();
                uint   nextKeyframeColor = nextKeyframe.color(set);

                var    prevKeyframe      = GetKeyframe(set, (ushort)(nextIndex - 1));
                ushort prevKeyframeTime  = prevKeyframe.time();
                uint   prevKeyframeColor = prevKeyframe.color(set);

                // Compute the interpolation parameter
                // To stick to integer math, we'll scale the values
                int scaler        = 1024;
                int scaledPercent = (time - prevKeyframeTime) * scaler / (nextKeyframeTime - prevKeyframeTime);
                int scaledRed     = Utils.getRed(prevKeyframeColor) * (scaler - scaledPercent) + Utils.getRed(nextKeyframeColor) * scaledPercent;
                int scaledGreen   = Utils.getGreen(prevKeyframeColor) * (scaler - scaledPercent) + Utils.getGreen(nextKeyframeColor) * scaledPercent;
                int scaledBlue    = Utils.getBlue(prevKeyframeColor) * (scaler - scaledPercent) + Utils.getBlue(nextKeyframeColor) * scaledPercent;
                return(Utils.toColor((byte)(scaledRed / scaler), (byte)(scaledGreen / scaler), (byte)(scaledBlue / scaler)));
            }
        }
Example #6
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);
        }
Example #7
0
        public byte   padding;         // 0 - 20

        public ref RGBKeyframe GetKeyframe(AnimationSet set, ushort keyframeIndex)
        {
            Debug.Assert(keyframeIndex < keyFrameCount);
            return(ref set.getKeyframe((ushort)(keyframesOffset + keyframeIndex)));
        }
Example #8
0
 public uint color(AnimationSet set)
 {
     return(set.getColor32(colorIndex()));
 }
Example #9
0
        public byte   specialColorType; // is really SpecialColor


        public ref AnimationTrack GetTrack(AnimationSet set, ushort index)
        {
            Debug.Assert(index < trackCount);
            return(ref set.getTrack((ushort)(tracksOffset + index)));
        }
Example #10
0
 public uint evaluate(AnimationSet set, int time)
 {
     return(GetTrack(set).evaluate(set, time));
 }
Example #11
0
        public byte   padding;     // Keyframe count

        public ref RGBTrack GetTrack(AnimationSet set)
        {
            return(ref set.getRGBTrack(trackOffset));
        }