Exemplo n.º 1
0
    // Paste the clipboard data into the chart, overwriting anything else in the process
    public void Paste(uint chartLocationToPaste)
    {
        //if (System.Windows.Forms.Clipboard.GetDataObject().GetFormats().Length > 0 &&
        //    !(
        //        System.Windows.Forms.Clipboard.ContainsText(TextDataFormat.UnicodeText) &&
        //        System.Windows.Forms.Clipboard.ContainsText(TextDataFormat.Text) &&
        //        System.Windows.Forms.Clipboard.GetText() == "")
        //    )     // Something else is pasted on the clipboard instead of Moonscraper stuff.
        //    return;

        FileStream fs = null;

        clipboard = null;
        try
        {
            // Read clipboard data from a file instead of the actual clipboard because the actual clipboard doesn't work for whatever reason
            fs = new FileStream(CLIPBOARD_FILE_LOCATION, FileMode.Open);
            BinaryFormatter formatter = new BinaryFormatter();

            clipboard = (Clipboard)formatter.Deserialize(fs);
        }
        catch (System.Exception e)
        {
            Logger.LogException(e, "Failed to read from clipboard file");
            clipboard = null;
        }
        finally
        {
            if (fs != null)
            {
                fs.Close();
            }
            else
            {
                Debug.LogError("Filestream when reading clipboard data failed to initialise");
            }
        }

        if (Globals.applicationMode == Globals.ApplicationMode.Editor && clipboard != null && clipboard.data.Length > 0)
        {
            List <SongEditCommand> commands = new List <SongEditCommand>();

            Rect collisionRect = clipboard.GetCollisionRect(chartLocationToPaste, editor.currentSong);
            if (clipboard.areaChartPosMin > clipboard.areaChartPosMax)
            {
                Debug.LogError("Clipboard minimum (" + clipboard.areaChartPosMin + ") is greater than clipboard the max (" + clipboard.areaChartPosMax + ")");
            }
            uint colliderChartDistance = TickFunctions.TickScaling(clipboard.areaChartPosMax - clipboard.areaChartPosMin, clipboard.resolution, editor.currentSong.resolution);

            viewModeController.ToggleSongViewMode(!clipboard.data[0].GetType().IsSubclassOf(typeof(ChartObject)));

            {
                List <SongObject> newObjectsToDelete = new List <SongObject>();

                // Overwrite any objects in the clipboard space
                if (clipboard.data[0].GetType().IsSubclassOf(typeof(ChartObject)))
                {
                    foreach (ChartObject chartObject in editor.currentChart.chartObjects)
                    {
                        if (chartObject.tick >= chartLocationToPaste && chartObject.tick <= (chartLocationToPaste + colliderChartDistance) && PrefabGlobals.HorizontalCollisionCheck(PrefabGlobals.GetCollisionRect(chartObject), collisionRect))
                        {
                            newObjectsToDelete.Add(chartObject);
                        }
                    }
                }
                else
                {
                    // Overwrite synctrack, leave sections alone
                    foreach (SyncTrack syncObject in editor.currentSong.syncTrack)
                    {
                        if (syncObject.tick >= chartLocationToPaste && syncObject.tick <= (chartLocationToPaste + colliderChartDistance) && PrefabGlobals.HorizontalCollisionCheck(PrefabGlobals.GetCollisionRect(syncObject), collisionRect))
                        {
                            newObjectsToDelete.Add(syncObject);
                        }
                    }
                }

                if (newObjectsToDelete.Count > 0)
                {
                    commands.Add(new SongEditDelete(newObjectsToDelete));
                }
            }

            {
                uint maxLength = editor.currentSong.TimeToTick(editor.currentSong.length, editor.currentSong.resolution);

                List <SongObject> newObjectsToAddIn = new List <SongObject>();

                // Paste the new objects in
                foreach (SongObject clipboardSongObject in clipboard.data)
                {
                    SongObject objectToAdd = clipboardSongObject.Clone();

                    objectToAdd.tick = chartLocationToPaste +
                                       TickFunctions.TickScaling(clipboardSongObject.tick, clipboard.resolution, editor.currentSong.resolution) -
                                       TickFunctions.TickScaling(clipboard.areaChartPosMin, clipboard.resolution, editor.currentSong.resolution);

                    if (objectToAdd.tick >= maxLength)
                    {
                        break;
                    }

                    if (objectToAdd.GetType() == typeof(Note))
                    {
                        Note note = (Note)objectToAdd;

                        if (clipboard.instrument == Song.Instrument.GHLiveGuitar || clipboard.instrument == Song.Instrument.GHLiveBass)
                        {
                            // Pasting from a ghl track
                            if (!Globals.ghLiveMode)
                            {
                                if (note.ghliveGuitarFret == Note.GHLiveGuitarFret.Open)
                                {
                                    note.guitarFret = Note.GuitarFret.Open;
                                }
                                else if (note.ghliveGuitarFret == Note.GHLiveGuitarFret.White3)
                                {
                                    continue;
                                }
                            }
                        }
                        else if (Globals.ghLiveMode)
                        {
                            // Pasting onto a ghl track
                            if (note.guitarFret == Note.GuitarFret.Open)
                            {
                                note.ghliveGuitarFret = Note.GHLiveGuitarFret.Open;
                            }
                        }

                        note.length = TickFunctions.TickScaling(note.length, clipboard.resolution, editor.currentSong.resolution);
                    }
                    else if (objectToAdd.GetType() == typeof(Starpower))
                    {
                        Starpower sp = (Starpower)objectToAdd;
                        sp.length = TickFunctions.TickScaling(sp.length, clipboard.resolution, editor.currentSong.resolution);
                    }

                    newObjectsToAddIn.Add(objectToAdd);
                }

                if (newObjectsToAddIn.Count > 0)
                {
                    commands.Add(new SongEditAdd(newObjectsToAddIn));
                }
            }

            if (commands.Count > 0)
            {
                BatchedSongEditCommand batchedCommands = new BatchedSongEditCommand(commands);
                editor.commandStack.Push(batchedCommands);
            }
        }
        // 0 objects in clipboard, don't bother pasting
    }