Example #1
0
 /// <summary>
 /// Decode the data from an animation event to an origin -> direction.
 /// </summary>
 /// <param name="intParam">Int parameter passed by event</param>
 /// <param name="floatParam">Float parameter passed by event</param>
 /// <param name="origin">The local space origin of the gizmo</param>
 /// <param name="direction"> The forward direction of the gizmo</param>
 /// <param name="normalizeDirection"> Whether or not the direction vector output will be normalized</param>
 public void DecodeOriginAndDirection(int intParam, float floatParam, out Vector2 origin, out Vector2 direction, bool normalizeDirection = true)
 {
     origin    = IntConverter.DecodeIntToVector2(intParam);
     direction = IntConverter.DecodeIntToVector2((int)floatParam) - origin;
     if (normalizeDirection)
     {
         direction = Vector3.Normalize(direction);
     }
     origin      /= m_Renderer.sprite.pixelsPerUnit;
     direction.y *= -1f;
     origin.y    *= -1f;
 }
        /// <summary>
        /// ギズモ描画
        /// </summary>
        /// <param name="position"></param>
        /// <param name="pivot"></param>
        /// <param name="mouse"></param>
        /// <param name="scale"></param>
        public void DrawEditorGizmos(Vector2 position, Vector2 pivot, Vector2 mouse, float scale)
        {
            UpdateSerializedObject();

            if (Animations == null ||
                SelectedAnimation >= Animations.Length ||
                Animations[SelectedAnimation].frameData == null ||
                SelectedFrame >= Animations[SelectedAnimation].frameData.Length)
            {
                return;
            }

            var framedata   = Animations[SelectedAnimation].frameData[SelectedFrame];
            var eventType   = Event.current.type;
            var eventButton = Event.current.button;

            m_EditorScale   = scale;
            m_PreviewOrigin = position;

            if (framedata.events != null)
            {
                int currGizmo = 0;

                for (int i = 0; i < framedata.events.Length; i++)
                {
                    if (framedata.events[i].id.ToString().Contains(PROJECTILE))
                    {
                        var origin = IntConverter.DecodeIntToVector2(framedata.events[i].intParam) * scale;
                        var dest   = IntConverter.DecodeIntToVector2((int)framedata.events[i].floatParam) * scale;

                        m_MouseOverHandle = null;
                        //If the dots are overlapping we're going to force them apart
                        if (dest == origin)
                        {
                            dest.x += 20 * scale;
                        }

                        if (eventType == EventType.Repaint)
                        {
                            var a         = new Vector2(-1f, 1f);
                            var b         = new Vector2(-1f, -1f);
                            var direction = Vector3.Normalize(dest - origin);
#if UNITY_2017_1_OR_NEWER
                            var rotation = Quaternion.AngleAxis(Vector2.SignedAngle(direction, Vector2.right), -Vector3.forward);
#else
                            var angle    = Vector2.Angle(direction, Vector2.right);
                            var cross    = Vector3.Cross(direction, Vector3.right);
                            var rotation = Quaternion.AngleAxis(cross.z < 0 ? -angle : angle, -Vector3.forward);
#endif
                            a = rotation * a;
                            b = rotation * b;

                            LineDrawer.DrawLine(origin + position, dest + position, 6, Color.black);
                            LineDrawer.DrawLine(dest + position, dest + position + a * 11f, 6, Color.black);
                            LineDrawer.DrawLine(dest + position, dest + position + b * 11f, 6, Color.black);

                            LineDrawer.DrawLine(origin + position, dest + position, 4, Color.white);
                            LineDrawer.DrawLine(dest + position, dest + position + a * 10f, 4, Color.white);
                            LineDrawer.DrawLine(dest + position, dest + position + b * 10f, 4, Color.white);

                            LabelDrawer.DrawLabel(framedata.events[i].id.ToString(), origin + position + new Vector2(0f, 6f));
                        }

                        if (m_GizmoDragHandle[currGizmo * 2 + 0].Draw(position + origin, mouse, MouseCursor.Pan))
                        {
                            var index = i;
                            m_GizmoDragHandle[currGizmo * 2 + 0].action = (Vector2 v) =>
                                                                          Animations[SelectedAnimation].frameData[SelectedFrame].events[index].intParam = IntConverter.EncodeVector2ToInt(v / scale);
                            m_MouseOverHandle = m_GizmoDragHandle[currGizmo * 2 + 0];
                        }
                        if (m_GizmoDragHandle[currGizmo * 2 + 1].Draw(position + dest, mouse, MouseCursor.Pan))
                        {
                            var index = i;
                            m_GizmoDragHandle[currGizmo * 2 + 1].action = (Vector2 v) =>
                                                                          Animations[SelectedAnimation].frameData[SelectedFrame].events[index].floatParam = IntConverter.EncodeVector2ToInt(v / scale);
                            m_MouseOverHandle = m_GizmoDragHandle[currGizmo * 2 + 1];
                        }

                        if (eventType == EventType.MouseDown && m_MouseOverHandle != null && eventButton == 0)
                        {
                            m_ActiveHandle = m_MouseOverHandle;
                            //remove this click from the event so we don't click through other gizmos beneath us.
                            eventButton = 3;
                        }
                        else if (eventType == EventType.MouseUp && eventButton == 0)
                        {
                            m_ActiveHandle = null;
                        }
                        else if (eventType == EventType.MouseDrag && m_ActiveHandle != null && m_ActiveHandle.action != null)
                        {
                            m_ActiveHandle.action(mouse - position);
                        }

                        currGizmo++;
                    }
                }
            }

            if (m_PivotHandle.Draw(m_ActiveHandle == m_PivotHandle ? mouse : position, mouse, MouseCursor.Pan) &&
                m_ActiveHandle == null)
            {
                m_MouseOverHandle = m_PivotHandle;
            }
            else if (m_MouseOverHandle == m_PivotHandle)
            {
                m_MouseOverHandle = null;
            }

            // if (m_ActiveHandle == null && m_MouseOverHandle == m_PivotHandle && eventType == EventType.MouseDown && eventButton == 0)
            // {
            //     m_ActiveHandle = m_MouseOverHandle;
            // }
            // else if (m_ActiveHandle != null && m_ActiveHandle == m_PivotHandle && eventType == EventType.MouseUp)
            // {
            //     if (m_Renderer == null)
            //         m_Renderer = m_targetManager.GetComponent<SpriteRenderer>();
            //     var path = AssetDatabase.GetAssetPath(m_Renderer.sprite);
            //     TextureImporter import = (TextureImporter)AssetImporter.GetAtPath(path);
            //     if (import.spriteImportMode == SpriteImportMode.Multiple && import.spritesheet != null)
            //     {
            //         var metadata = import.spritesheet;
            //         import.isReadable = true;
            //         for (int i = 0; i < metadata.Length; i++)
            //             if (metadata[i].name.Equals(m_Renderer.sprite.name))
            //             {
            //                 var p = (mouse - (position - pivot)) / scale;
            //                 p.x = Mathf.Round(p.x) / metadata[i].rect.width;
            //                 p.y = 1f - (Mathf.Round(p.y) / metadata[i].rect.height);
            //                 metadata[i].pivot = p;
            //                 break;
            //             }
            //         m_ActiveHandle = m_MouseOverHandle = null;
            //         import.spritesheet = metadata;
            //         EditorUtility.SetDirty(import);
            //         import.SaveAndReimport();
            //     }
            //     m_ActiveHandle = m_MouseOverHandle = null;
            // }

            Vector2 alignedPos = (m_ActiveHandle == m_PivotHandle)
                ? mouse - (position - pivot)
                : pivot;

            string labelText = "Pivot (" + Mathf.RoundToInt(alignedPos.x / scale)
                               + ", " + Mathf.RoundToInt(alignedPos.y / scale) + ")";
            Vector2 labelPos = (m_ActiveHandle == m_PivotHandle)
                ? mouse
                : position;
            labelPos += new Vector2(-15f, 6f);
            LabelDrawer.DrawLabel(labelText, labelPos);

            if (m_isEditorMenuOpen == false && eventType == EventType.MouseDown && eventButton == 1)
            {
                if (m_Renderer == null)
                {
                    m_Renderer = m_targetManager.GetComponent <SpriteRenderer>();
                }

                var size        = m_Renderer.sprite.rect.size;
                var pivotPixels = m_Renderer.sprite.pivot;

                CreateEditGizmoMenu(new Vector2(pivotPixels.x / size.x, pivotPixels.y / size.y), mouse);
            }

            ApplySerializedProperties();
        }