public static void Initialize(Pattern pattern)
        {
            EditorSequencerPianoRoll window = GetWindow <EditorSequencerPianoRoll>("Piano roll");

            Current           = pattern;
            window.instrument = 0;

            //focus on first note
            window.Focus();
        }
        public static void Initialize()
        {
            //Show existing window instance. If one doesn't exist, make one.
            EditorSequencerPianoRoll window = GetWindow <EditorSequencerPianoRoll>("Piano roll");

            window.instrument = 0;

            //focus on first note
            window.Focus();
        }
        public static void Initialize(InstancedPattern instancedPattern, Track track)
        {
            EditorSequencerPianoRoll window = GetWindow <EditorSequencerPianoRoll>("Piano roll");

            var pattern = track.uniquePatterns[instancedPattern.pattern];

            Current           = pattern;
            window.instrument = 0;

            //focus on first note
            window.Focus();
        }
        private void OnGUI()
        {
            Repaint();

            Toolbar();
            Drag();
            Grid();
            Mouse();

            //draw the tracks
            int tracks = Mathf.CeilToInt(Screen.width / MaxTracks);

            for (int i = 0; i < tracks; i++)
            {
                Rect position = new Rect(0, TopOffset + i * TrackHeight, LeftOffset, TrackHeight);
                if (i < MaxTracks)
                {
                    GUI.Box(position, "Track " + (i + 1), EditorStyles.helpBox);
                }
                else
                {
                    GUI.Box(position, "", EditorStyles.helpBox);
                }
            }

            //draw the patterns
            bool deselect = true;

            foreach (var pattern in Current.patterns)
            {
                Rect rect     = new Rect(LeftOffset + pattern.time, TopOffset + pattern.order * TrackHeight, pattern.GetLength(Current), TrackHeight);
                bool contains = rect.Contains(Event.current.mousePosition);
                if (contains)
                {
                    deselect = false;
                }

                if (MouseDown && contains && dragOffset == null)
                {
                    //start dragging the pattern
                    var pos = new Vector2(pattern.time + LeftOffset, TopOffset + pattern.order * TrackHeight);
                    dragOffset      = GetPatternPosition(Event.current.mousePosition - pos - Vector2.down * TopOffset);
                    patternDragging = pattern;
                    dragStart       = Event.current.mousePosition;
                    dragDistance    = 0f;
                    //Debug.Log("Started dragging " + pattern.Name);
                }
                else if (MouseDown && dragOffset != null)
                {
                    if (pattern == patternDragging)
                    {
                        //snap the position of the pattern to the mouse
                        pattern.time = GetPatternPosition().x - LeftOffset - (int)dragOffset.Value.x;
                        pattern.time = pattern.time < 0 ? 0 : pattern.time;

                        float my = Event.current.mousePosition.y < TopOffset ? TopOffset : Event.current.mousePosition.y;
                        int   patternPositionY = GetPatternPosition(new Vector2(Event.current.mousePosition.x, my)).y;
                        patternPositionY = patternPositionY < 0 ? 0 : patternPositionY;

                        pattern.order = (byte)(((uint)patternPositionY - (uint)dragOffset.Value.y) / (float)TrackHeight);
                        pattern.order = pattern.order > MaxTracks - 1 ? (byte)(MaxTracks - 1) : pattern.order;
                        dragDistance  = Vector2.Distance(dragStart, Event.current.mousePosition);
                    }
                }
                else if (!MouseDown && dragOffset != null)
                {
                    //stop dragging
                    //Debug.Log("Stopped dragging");
                    patternDragging = null;
                    dragOffset      = null;
                }

                //display the pattern box
                if (DrawBox(rect, pattern.name, 0.5f))
                {
                    if (EditorApplication.timeSinceStartup > nextPianoRollOpen)
                    {
                        //Debug.Log(Event.current.button);
                        //only register a click if the drag distance was small enough
                        if (dragDistance < 1f)
                        {
                            //left click to edit
                            //right click to delete
                            if (Event.current.button == 0)
                            {
                                //double click to open piano roll
                                if (lastClick != null && EditorApplication.timeSinceStartup - lastClick < 0.25)
                                {
                                    EditorSequencerPianoRoll.Initialize(pattern, Current);
                                    lastClick = null;
                                }
                                else
                                {
                                    //if double click failed
                                    //simply select it on the browser
                                    if (pattern.pattern != EditorSequencerBrowser.Selected)
                                    {
                                        EditorSequencerBrowser.Selected = pattern.pattern;

                                        //new selected pattern is different, reset the double click timer
                                        lastClick = null;
                                    }
                                    else
                                    {
                                        lastClick = EditorApplication.timeSinceStartup;
                                    }
                                }
                            }
                            else if (Event.current.button == 1)
                            {
                                Current.patterns.Remove(pattern);
                                return;
                            }
                        }
                    }
                }

                //display the pattern notes
                DrawNotes(rect, pattern);
            }

            //didnt click on any patterns in the playlist
            //so place a pattern here if one is selected
            if (deselect && EditorSequencerBrowser.Selected != null)
            {
                if (Event.current.button == 0)
                {
                    if (Event.current.type == EventType.MouseDown)
                    {
                        nextPianoRollOpen = EditorApplication.timeSinceStartup + 0.5;
                        var mouse   = GetPatternPosition();
                        var pattern = Current.uniquePatterns[EditorSequencerBrowser.Selected.Value];
                        Add(mouse, pattern);
                    }
                }
            }
        }
Example #5
0
        private void OnGUI()
        {
            Repaint();

            //display the create pattern button
            if (GUILayout.Button("New Pattern", EditorStyles.toolbarButton))
            {
                int    runs        = 1;
                string patternName = "Pattern " + runs;
                while (true)
                {
                    runs++;

                    //check if this pattern name already exists
                    bool exists = false;
                    for (int i = 0; i < Current.uniquePatterns.Count; i++)
                    {
                        if (Current.uniquePatterns[i].name == patternName)
                        {
                            //this pattern name is already taken,
                            //make a new one

                            patternName = "Pattern " + runs;
                            exists      = true;
                            break;
                        }
                    }

                    if (!exists)
                    {
                        //pattern with this name doesnt exist
                        //so break
                        break;
                    }
                }
                var pattern = new Pattern(patternName, Current);
                Current.uniquePatterns.Add(pattern);
            }

            //no patterns found in the track, show info
            if (Current.uniquePatterns.Count == 0)
            {
                EditorGUILayout.HelpBox("No patterns in the current track.", MessageType.Info);
            }

            //display a list of all the patterns
            bool deselect = true;

            for (int i = 0; i < Current.uniquePatterns.Count; i++)
            {
                Pattern pattern = Current.uniquePatterns[i];
                Rect    rect    = GUILayoutUtility.GetRect(Screen.width, PatternHeight);
                if (rect.Contains(Event.current.mousePosition))
                {
                    deselect = false;
                    if (Event.current.type == EventType.MouseDown)
                    {
                        DragAndDrop.PrepareStartDrag();// reset data
                        DragAndDrop.SetGenericData("pattern_index", Current.uniquePatterns.IndexOf(pattern));
                        DragAndDrop.objectReferences = new Object[] { new Object() };
                        Event.current.Use();
                    }
                    if (Event.current.type == EventType.MouseDrag)
                    {
                        if (DragAndDrop.GetGenericData("pattern_index") != null)
                        {
                            DragAndDrop.StartDrag("Dragging " + pattern.name);
                            Event.current.Use();
                        }
                    }
                }

                if (DrawBox(rect, pattern.name, Selected == i ? 1.2f : 0.75f))
                {
                    //open piano roll on left click
                    //delete on right click
                    if (Event.current.button == 0)
                    {
                        //double click to open piano roll
                        if (lastClick != null && EditorApplication.timeSinceStartup - lastClick < 0.25)
                        {
                            EditorSequencerPianoRoll.Initialize(pattern);
                            lastClick = null;
                        }
                        else
                        {
                            //if double click failed
                            //simply select it on the browser
                            if (Selected != i)
                            {
                                Selected  = i;
                                lastClick = null;
                            }
                            else
                            {
                                lastClick = EditorApplication.timeSinceStartup;
                            }
                        }
                    }
                    else if (Event.current.button == 1)
                    {
                        //deleted a selected pattern, reset the selection index
                        if (Selected == i)
                        {
                            Selected = null;
                        }

                        Current.uniquePatterns.Remove(pattern);

                        //also delete any patterns on the playlist that reference this pattern
                        int d = 0;
                        while (d < Current.patterns.Count)
                        {
                            if (Current.patterns[d].pattern == i)
                            {
                                Current.patterns.RemoveAt(d);

                                //deleted a pattern
                                //restart the loop and try again until this reaches the last index
                                d = 0;
                                continue;
                            }

                            d++;
                        }

                        //shift other patterns down
                        for (int p = 0; p < Current.patterns.Count; p++)
                        {
                            if (Current.patterns[p].pattern != 0)
                            {
                                Current.patterns[p].pattern--;
                            }
                        }

                        return;
                    }
                }
            }

            //clicked on nothing
            if (deselect)
            {
                if (Event.current.button == 0)
                {
                    if (Event.current.type == EventType.MouseDown)
                    {
                        Selected = null;
                    }
                }
            }
        }