void DrawFrameGroupEx(Rect r, tk2dSpriteAnimationClip clip, ClipEditor.FrameGroup fg, bool highlighted, bool showTime, bool playHighlight)
        {
            if (highlighted && playHighlight)
            {
                GUI.color = new Color(1.0f, 0.8f, 1.0f, 1.0f);
            }
            else if (playHighlight)
            {
                GUI.color = new Color(1.0f, 0.8f, 0.8f, 1.0f);
            }
            else if (highlighted)
            {
                GUI.color = new Color(0.8f, 0.8f, 1.0f, 1.0f);
            }

            tk2dSpriteCollectionData sc = fg.spriteCollection;
            int    spriteId             = fg.spriteId;
            string name  = sc.inst.spriteDefinitions[spriteId].name;
            string label = name;

            if (showTime)
            {
                string numFrames = (fg.frames.Count == 1) ? "1 frame" : (fg.frames.Count.ToString() + " frames");
                string time      = (fg.frames.Count / clip.fps).ToString("0.000") + "s";
                label = label + "\n" + numFrames + "\n" + time;
            }
            GUI.Label(r, label, "button");

            if (highlighted || playHighlight)
            {
                GUI.color = Color.white;
            }
        }
		public static ClipEditor.FrameGroup NewFrameGroup(List<ClipEditor.FrameGroup> frameGroups, int selectedFrameGroup)
		{
			ClipEditor.FrameGroup src = frameGroups[selectedFrameGroup];
			ClipEditor.FrameGroup fg = new ClipEditor.FrameGroup();
			fg.spriteCollection = src.spriteCollection;
			fg.spriteId = src.spriteId;
			tk2dSpriteAnimationFrame f = new tk2dSpriteAnimationFrame();
			f.spriteCollection = fg.spriteCollection;
			f.spriteId = fg.spriteId;
			fg.frames.Add(f);
			return fg;
		}
 void DrawFrameGroupsOverlay(int controlId, Rect frameGroupRect, tk2dSpriteAnimationClip clip, List <ClipEditor.FrameGroup> frameGroups, float clipTimeMarker)
 {
     // Draw moving frame if active
     if (GUIUtility.hotControl == controlId && state.type == State.Type.Move && state.activeFrame != -1)
     {
         GUI.color = new Color(0.8f, 0.8f, 0.8f, 0.9f);
         ClipEditor.FrameGroup fg = frameGroups[state.activeFrame];
         DrawFrameGroup(new Rect(Event.current.mousePosition.x - state.frameSelectionOffset.x, frameGroupRect.y - frameGroupRect.height, frameWidth * fg.frames.Count, frameGroupRect.height), clip, fg);
         GUI.color = Color.white;
         Repaint();
     }
 }
예제 #4
0
        public static ClipEditor.FrameGroup NewFrameGroup(List <ClipEditor.FrameGroup> frameGroups, int selectedFrameGroup)
        {
            ClipEditor.FrameGroup src = frameGroups[selectedFrameGroup];
            ClipEditor.FrameGroup fg  = new ClipEditor.FrameGroup();
            fg.spriteCollection = src.spriteCollection;
            fg.spriteId         = src.spriteId;
            tk2dSpriteAnimationFrame f = new tk2dSpriteAnimationFrame();

            f.spriteCollection = fg.spriteCollection;
            f.spriteId         = fg.spriteId;
            fg.frames.Add(f);
            return(fg);
        }
예제 #5
0
        bool AutoFill(List <ClipEditor.FrameGroup> frameGroups, int selectedFrame, bool reverse)
        {
            ClipEditor.FrameGroup selectedFrameGroup = frameGroups[selectedFrame];
            if (selectedFrameGroup.spriteCollection != null && selectedFrameGroup.spriteId >= 0 && selectedFrameGroup.spriteId < selectedFrameGroup.spriteCollection.inst.Count)
            {
                string na = selectedFrameGroup.spriteCollection.inst.spriteDefinitions[selectedFrameGroup.spriteId].name;

                int numStartA = na.Length - 1;
                if (na[numStartA] >= '0' && na[numStartA] <= '9')
                {
                    while (numStartA > 0 && na[numStartA - 1] >= '0' && na[numStartA - 1] <= '9')
                    {
                        numStartA--;
                    }

                    string baseName = na.Substring(0, numStartA).ToLower();
                    int    baseNo   = System.Convert.ToInt32(na.Substring(numStartA));

                    int        maxAllowedMissing = 10;
                    int        allowedMissing    = maxAllowedMissing;
                    List <int> pendingFrames     = new List <int>();
                    int        startOffset       = reverse ? -1 : 1;
                    int        frameInc          = reverse ? -1 : 1;
                    for (int frameNo = baseNo + startOffset; frameNo >= 0; frameNo += frameInc)
                    {
                        int frameIdx = FindFrameIndex(selectedFrameGroup.spriteCollection.inst.spriteDefinitions, baseName, frameNo);
                        if (frameIdx == -1)
                        {
                            if (--allowedMissing <= 0)
                            {
                                break;
                            }
                        }
                        else
                        {
                            pendingFrames.Add(frameIdx);
                            allowedMissing = maxAllowedMissing;                             // reset
                        }
                    }

                    int numInserted = 0;
                    int insertIndex = selectedFrame + 1;
                    ClipEditor.FrameGroup nextFrameGroup = (insertIndex >= frameGroups.Count) ? null : frameGroups[insertIndex];
                    while (pendingFrames.Count > 0)
                    {
                        int frameToInsert = pendingFrames[0];
                        pendingFrames.RemoveAt(0);

                        if (nextFrameGroup != null &&
                            nextFrameGroup.spriteCollection == selectedFrameGroup.spriteCollection &&
                            nextFrameGroup.spriteId == frameToInsert)
                        {
                            break;
                        }

                        ClipEditor.FrameGroup fg = AnimOperatorUtil.NewFrameGroup(frameGroups, selectedFrame);
                        fg.spriteId = frameToInsert;
                        fg.Update();
                        frameGroups.Insert(insertIndex++, fg);
                        numInserted++;
                    }

                    return(numInserted > 0);
                }
            }

            return(false);
        }
예제 #6
0
        bool ProcessSpriteImport(List <ClipEditor.FrameGroup> frameGroups, string spriteNames)
        {
            tk2dSpriteCollectionData coll = frameGroups[0].spriteCollection;

            // make new list
            List <int> spriteIds   = new List <int>();
            List <int> frameCounts = new List <int>();

            int lineNumber = 1;

            string[] lines = spriteNames.Split('\n');
            foreach (string line in lines)
            {
                if (line.Trim().Length != 0)
                {
                    string spriteName = line;
                    int    frameCount = 1;
                    int    splitIndex = line.LastIndexOf(';');
                    if (splitIndex != -1)
                    {
                        spriteName = line.Substring(0, splitIndex);
                        string frameCountStr = line.Substring(splitIndex + 1, line.Length - 1 - splitIndex);
                        if (!System.Int32.TryParse(frameCountStr, out frameCount))
                        {
                            Debug.LogError("Parse error in line " + lineNumber.ToString());
                            return(false);
                        }
                        frameCount = Mathf.Max(frameCount, 1);
                    }
                    int spriteId = coll.GetSpriteIdByName(spriteName, -1);
                    if (spriteId == -1)
                    {
                        Debug.LogError(string.Format("Unable to find sprite '{0}' in sprite collection", spriteName));
                        return(false);
                    }
                    spriteIds.Add(spriteId);
                    frameCounts.Add(frameCount);
                }
                lineNumber++;
            }

            List <ClipEditor.FrameGroup> newFrameGroups = new List <ClipEditor.FrameGroup>();

            for (int i = 0; i < spriteIds.Count; ++i)
            {
                if (i < frameGroups.Count && frameGroups[i].spriteId == spriteIds[i])
                {
                    if (frameGroups[i].frames.Count != frameCounts[i])
                    {
                        frameGroups[i].SetFrameCount(frameCounts[i]);
                    }
                    newFrameGroups.Add(frameGroups[i]);
                }
                else
                {
                    ClipEditor.FrameGroup fg = new ClipEditor.FrameGroup();
                    fg.spriteCollection = coll;
                    fg.spriteId         = spriteIds[i];
                    fg.SetFrameCount(frameCounts[i]);
                    newFrameGroups.Add(fg);
                }
            }
            frameGroups.Clear();
            foreach (ClipEditor.FrameGroup fg in newFrameGroups)
            {
                frameGroups.Add(fg);
            }

            operations = AnimEditOperations.ClipContentChanged;
            return(true);
        }
		bool ProcessSpriteImport(List<ClipEditor.FrameGroup> frameGroups, string spriteNames)
		{
			tk2dSpriteCollectionData coll = frameGroups[0].spriteCollection;

			// make new list
			List<int> spriteIds = new List<int>();
			List<int> frameCounts = new List<int>();

			int lineNumber = 1;
			string[] lines = spriteNames.Split('\n');
			foreach (string line in lines)
			{
				if (line.Trim().Length != 0)
				{
					string spriteName = line;
					int frameCount = 1;
					int splitIndex = line.LastIndexOf(';');
					if (splitIndex != -1)
					{
						spriteName = line.Substring(0, splitIndex);
						string frameCountStr = line.Substring(splitIndex + 1, line.Length - 1 - splitIndex);
						if (!System.Int32.TryParse(frameCountStr, out frameCount))
						{
							Debug.LogError("Parse error in line " + lineNumber.ToString());
							return false;
						}
						frameCount = Mathf.Max(frameCount, 1);
					}
					int spriteId = coll.GetSpriteIdByName(spriteName, -1);
					if (spriteId == -1)
					{
						Debug.LogError(string.Format("Unable to find sprite '{0}' in sprite collection", spriteName));
						return false;
					}
					spriteIds.Add(spriteId);
					frameCounts.Add(frameCount);
				}
				lineNumber++;
			}

			List<ClipEditor.FrameGroup> newFrameGroups = new List<ClipEditor.FrameGroup>();
			for (int i = 0; i < spriteIds.Count; ++i)
			{
				if (i < frameGroups.Count && frameGroups[i].spriteId == spriteIds[i])
				{
					if (frameGroups[i].frames.Count != frameCounts[i])
						frameGroups[i].SetFrameCount(frameCounts[i]);
					newFrameGroups.Add(frameGroups[i]);
				}
				else
				{
					ClipEditor.FrameGroup fg = new ClipEditor.FrameGroup();
					fg.spriteCollection = coll;
					fg.spriteId = spriteIds[i];
					fg.SetFrameCount(frameCounts[i]);
					newFrameGroups.Add(fg);
				}	
			}
			frameGroups.Clear();
			foreach (ClipEditor.FrameGroup fg in newFrameGroups)
				frameGroups.Add(fg);

			operations = AnimEditOperations.ClipContentChanged;
			return true;
		}
 void DrawFrameGroup(Rect r, tk2dSpriteAnimationClip clip, ClipEditor.FrameGroup fg)
 {
     DrawFrameGroupEx(r, clip, fg, false, false, false);
 }
        void DrawFrameGroups(int controlId, Rect frameGroupRect, tk2dSpriteAnimationClip clip, List <ClipEditor.FrameGroup> frameGroups, float clipTimeMarker)
        {
            bool singleFrameMode = clip.wrapMode == tk2dSpriteAnimationClip.WrapMode.Single;

            // Initialize startframe in framegroups
            int numFrames = 0;

            foreach (ClipEditor.FrameGroup fg in frameGroups)
            {
                fg.startFrame = numFrames;
                numFrames    += fg.frames.Count;
            }

            // Draw frames
            int currrentFrameGroup = 0;

            foreach (ClipEditor.FrameGroup fg in frameGroups)
            {
                Rect r = GetRectForFrameGroup(frameGroupRect, fg);
                DrawFrameGroupEx(r, clip, fg,
                                 /* highlighted: */ currrentFrameGroup == state.selectedFrame,
                                 /* showTime: */ currrentFrameGroup == state.selectedFrame,
                                 /* playHighlight: */ clipTimeMarker >= fg.startFrame && clipTimeMarker < fg.startFrame + fg.frames.Count);
                if (!singleFrameMode)
                {
                    EditorGUIUtility.AddCursorRect(GetResizeRectFromFrameRect(r), MouseCursor.ResizeHorizontal);
                }

                currrentFrameGroup++;
            }

            // Add frame button
            if ((int)state.type < (int)State.Type.Action)
            {
                Rect addFrameButonRect = GetRectForFrame(frameGroupRect, clip.frames.Length, 1);
                addFrameButonRect = new Rect(addFrameButonRect.x + addFrameButonRect.width * 0.25f, addFrameButonRect.y + addFrameButonRect.height * 0.25f,
                                             addFrameButonRect.height * 0.5f, addFrameButonRect.height * 0.5f);
                if (!singleFrameMode &&
                    GUI.Button(addFrameButonRect, "+"))
                {
                    frameGroups.Add(AnimOperatorUtil.NewFrameGroup(frameGroups, frameGroups.Count - 1));
                    ClipEditor.RecalculateFrames(clip, frameGroups);
                    state.selectedFrame = frameGroups.Count - 1;
                    Repaint();
                }
            }

            // Draw insert marker
            if (GUIUtility.hotControl == controlId && state.type == State.Type.Move && state.activeFrame != -1 && state.insertMarker != -1)
            {
                Vector2 v = GetInsertMarkerPositionForFrameGroup(frameGroupRect, state.insertMarker, frameGroups);
                GUI.color = Color.green;
                GUI.Box(new Rect(v.x, v.y, 2, frameGroupRect.height), "", tk2dEditorSkin.WhiteBox);
                GUI.color = Color.white;
            }

            // Keyboard shortcuts
            Event ev = Event.current;

            if (ev.type == EventType.KeyDown && GUIUtility.keyboardControl == 0 &&
                state.type == State.Type.None && state.selectedFrame != -1)
            {
                int newFrame = state.selectedFrame;
                switch (ev.keyCode)
                {
                case KeyCode.LeftArrow:
                case KeyCode.Comma: newFrame--; break;

                case KeyCode.RightArrow:
                case KeyCode.Period: newFrame++; break;

                case KeyCode.Home: newFrame = 0; break;

                case KeyCode.End: newFrame = frameGroups.Count - 1; break;

                case KeyCode.Escape: state.selectedFrame = -1; Repaint(); ev.Use(); break;
                }

                if (ev.type != EventType.Used && frameGroups.Count > 0)
                {
                    newFrame = Mathf.Clamp(newFrame, 0, frameGroups.Count - 1);
                    if (newFrame != state.selectedFrame)
                    {
                        state.selectedFrame = newFrame;
                        Repaint();
                        ev.Use();
                    }
                }
            }
            if (state.selectedFrame != -1 && (GUIUtility.hotControl == controlId || (GUIUtility.keyboardControl == 0 && state.type == State.Type.None)))
            {
                if (ev.type == EventType.KeyDown && (ev.keyCode == KeyCode.Delete || ev.keyCode == KeyCode.Backspace) && frameGroups.Count > 1)
                {
                    frameGroups.RemoveAt(state.selectedFrame);
                    ClipEditor.RecalculateFrames(clip, frameGroups);
                    GUIUtility.hotControl = 0;
                    state.Reset();
                    Repaint();
                    ev.Use();
                }
            }

            if (ev.type == EventType.MouseDown || GUIUtility.hotControl == controlId)
            {
                switch (ev.GetTypeForControl(controlId))
                {
                case EventType.MouseDown:
                    if (frameGroupRect.Contains(ev.mousePosition))
                    {
                        int frameGroup = GetSelectedFrameGroup(frameGroupRect, ev.mousePosition, frameGroups, false);
                        if (frameGroup != state.selectedFrame)
                        {
                            Repaint();
                            state.selectedFrame = frameGroup;
                        }
                        if (frameGroup != -1)
                        {
                            Rect r          = GetRectForFrameGroup(frameGroupRect, frameGroups[frameGroup]);
                            Rect resizeRect = GetResizeRectFromFrameRect(r);
                            state.frameSelectionOffset = ev.mousePosition - new Vector2(r.x, 0);
                            state.type = resizeRect.Contains(ev.mousePosition) ? State.Type.Resize : State.Type.MoveHandle;
                            if (state.type == State.Type.Resize)
                            {
                                if (singleFrameMode)
                                {
                                    state.ResetState();                                             // disallow resize in single frame mode
                                }
                                else
                                {
                                    state.backupFrames = new List <tk2dSpriteAnimationFrame>(frameGroups[frameGroup].frames);                                            // make a backup of frames for triggers
                                    state.activeFrame  = frameGroup;
                                    state.insertMarker = state.activeFrame;
                                }
                            }
                            else
                            {
                                state.activeFrame  = frameGroup;
                                state.insertMarker = state.activeFrame;
                            }
                        }
                        GUIUtility.hotControl = controlId;
                    }
                    GUIUtility.keyboardControl = 0;
                    break;

                case EventType.MouseDrag:
                {
                    switch (state.type)
                    {
                    case State.Type.MoveHandle:
                    case State.Type.Move:
                    {
                        state.type         = State.Type.Move;
                        state.insertMarker = GetSelectedFrameGroup(frameGroupRect, ev.mousePosition, frameGroups, true);
                    }
                    break;

                    case State.Type.Resize:
                    {
                        int frame = GetSelectedFrame(frameGroupRect, ev.mousePosition + new Vector2(frameWidth * 0.5f, 0.0f));
                        ClipEditor.FrameGroup fg = frameGroups[state.activeFrame];
                        int  frameCount          = Mathf.Max(1, frame - fg.startFrame);
                        bool changed             = frameCount != fg.frames.Count;
                        if (changed)
                        {
                            fg.frames = new List <tk2dSpriteAnimationFrame>(state.backupFrames);
                            fg.SetFrameCount(frameCount);
                            Repaint();
                            ClipEditor.RecalculateFrames(clip, frameGroups);
                        }
                    }
                    break;
                    }
                }
                break;

                case EventType.MouseUp:
                    switch (state.type)
                    {
                    case State.Type.Move:
                    {
                        int finalInsertMarker = (state.insertMarker > state.activeFrame) ? (state.insertMarker - 1) : state.insertMarker;
                        if (state.activeFrame != finalInsertMarker)
                        {
                            ClipEditor.FrameGroup tmpFrameGroup = frameGroups[state.activeFrame];
                            frameGroups.RemoveAt(state.activeFrame);
                            frameGroups.Insert(finalInsertMarker, tmpFrameGroup);
                            state.selectedFrame = finalInsertMarker;
                            ClipEditor.RecalculateFrames(clip, frameGroups);
                            Repaint();
                        }
                    }
                    break;
                    }
                    if (state.type != State.Type.None)
                    {
                        Repaint();
                    }
                    state.ResetState();
                    GUIUtility.keyboardControl = 0;
                    GUIUtility.hotControl      = 0;
                    break;
                }
            }

            if (clipTimeMarker >= 0.0f)
            {
                float x = clipLeftHeaderSpace + frameWidth * clipTimeMarker;
                GUI.color = Color.red;
                GUI.Box(new Rect(frameGroupRect.x + x, frameGroupRect.y, 2, frameGroupRect.height), "", tk2dEditorSkin.WhiteBox);
                GUI.color = Color.white;
            }
        }
 Rect GetRectForFrameGroup(Rect fgRect, ClipEditor.FrameGroup frameGroup)
 {
     return(new Rect(fgRect.x + clipLeftHeaderSpace + frameWidth * frameGroup.startFrame, fgRect.y, frameGroup.frames.Count * frameWidth, fgRect.height));
 }