我将如何在roblox lua中复制“撤消”(ctrl + z)功能?

时间:2019-04-16 13:50:57

标签: lua roblox

我一直在尝试创建文本编辑器和程序中通常具有的“撤消”功能。我已经做了某种“撤消”功能,但一次只能删除1个字母,这不是我想要的。我要的是一次删除整个单词的东西。

我在TextBox上使用了GetPropertyChangedSignal,将文本输入到其中并在其中存储了字符串,然后每当播放器按ctrl + z时,我都会首先将文本框的文本设置为表格的最后一个值,然后删除最后一个值。

这是我使用的代码(不完全是,变量当然是不同的):

local Tab = {};

Box:GetPropertyChangedSignal("Text"):Connect(function()
    Tab[#Tab + 1] = Box.Text;
end);

game:service'Players'.LocalPlayer:GetMouse().KeyDown:Connect(function(key)
    if key == "z" then -- i will add a ctrl check later.
        Box.Text = #Tab > 0 and Tab[#Tab] or "";
        Tab[#Tab] = nil;
    end;
end);

正如我前面提到的,我希望它一次删除整个单词。 我正在考虑使用模式匹配(string.gsubstring.match%s+%w+)一次删除整个单词。

据我所知。帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

Store a list of actions. In order to undo, revert last actions, to redo repeat them. Feel free to merge actions that form a word if you want to undo on word-level only.

H -> append "H"
He -> append "e"
Hel -> append "l"
Helli -> append "i"
Hell -> removeLast "i"
Hello -> append " "
Hello -> append "o"
Hello W -> append "W"

In case it is possible to move the cursor you'll need insert actions where you have to store the position as well.

Another solution would be to store any new word in a table.

{"H"}
{"He"}
{"Hel"}
{"Hell"}
{"Hello"}
{"Hello", " "}
{"Hello", " ", "w"}
{"Hello", " ", "wo"}
{"Hello", " ", "wol"}
{"Hello", " ", "wo"}
{"Hello", " ", "wor"}
{"Hello", " ", "worl"}
{"Hello", " ", "world"}

Start/add a new item for every switch between whitespace and non-whitespace characters. To undo the last word entered, remove it from your list. If you want to undo deletes you'll have to keep a second copy to revert to.

You can also implement linked lists or whatnot. This can become arbitrarily complicated.