实现自定义剪贴板的最佳方式/数据结构是什么?

时间:2013-09-03 04:35:15

标签: c# data-structures queue unity3d clipboard

我正在使用C#在Unity3D中工作,并为我的游戏开发一个非常简单的文件管理器。 您可以对文件/文件夹执行简单的操作,其中一个是“剪切” (也许是“复制”,以后再用)

为了更好地想象正在发生的事情:

enter image description here

对于复制和粘贴,我需要自定义Clipboard。我想到了两个数据结构来实现它,StackQueue。堆栈的问题是,如果我剪切'a','b'和'c',它们将被反向粘贴,LIFO。所以我认为队列更好。

public class Clipboard<T>
{
    private SLLQueue<T> queue; // SLL stands for SinglyLinkedList, the queue is implemented that way...

    public Clipboard()
    {
        queue = new SLLQueue<T>();
    }

    public void Add(T data)
    {
        queue.Clear();
        Append(data);
    }

    public void Add(T[] data)
    {
        queue.Clear();
        foreach (var d in data)
            Append(d);
    }

    private void Append(T data)
    {
        queue.Push(data);
    }

    public T[] Clear()
    {
        int len = queue.Count;
        var data = new T[len];
        for (int i = 0; i < len; i++)
        {
            data[i] = queue.Pop();
        }
        return data;
    }
}

这够好吗? - 这是正确的数据结构吗? - 我是否以正确的方式实施了Clipboard的操作?

非常感谢你的帮助。

编辑:我不是在研究系统的剪贴板。

编辑:我已经使用了队列实现,这里是剪贴板的工作方式。

    public void CutSelection()
    {
        // if there's already something cut, change its fade/alpha back to normal
        var clipContents = fileManager.Clipboard.Clear();
        foreach (var c in clipContents)
            c.Icon.alpha = 1f;

        // adds all the currently selected items to the clip
        fileManager.Clipboard.Add(Selection);

        // cut their alpha to half
        foreach (var s in Selection)
            s.Icon.alpha = CUT_ALPHA;
    }

public void Paste()
{
     // clear the clip / get the content inside it, get their alpha back to full and move them to the current folder
    var clipContents = Clipboard.Clear();
    if (clipContents != null) {
        for (int i = 0, len = clipContents.Length; i < len; i++) {
            clipContents[i].Icon.alpha = 1;
            clipContents[i].MoveTo(currentFolder);
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

使用Clipboard Class。 这是一个example,用它来添加和检索数据。

相关问题