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;
		}
Beispiel #2
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);
        }
        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;
            }
        }