撤消/重做实现 [英] Undo/Redo implementation

查看:64
本文介绍了撤消/重做实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给我一些想法如何实现撤消/重做功能 - 就像我们在文本编辑器中一样。
我应该使用哪些算法,以及我可以阅读什么。谢谢。

解决方案

我知道撤消的类型的两个主要部分




  • SAVE STATE:
    一个类别的撤销是实际保存历史记录状态的地方。在这种情况下,发生什么事情是,在每一点你都会保持状态在一些内存位置。当你想做一个撤消,你只是交换当前的状态和交换在已经在内存的状态。例如,如何在Adobe Photoshop中完成历史记录或重新打开Goog​​le Chrome中的封闭标签。






  • 生成状态:
    另一个类别是在哪里,而不是维护状态本身,您只需记住操作是什么。当您需要撤消时,您需要做出与该特定操作相反的逻辑。一个简单的例子,当你在一些支持撤销的文本编辑器中执行 Ctrl + B 时,它被记住为粗体动作。现在,每个动作都是其逻辑反转的映射。因此,当您执行 Ctrl + Z 时,它会从反向操作表中查找,并发现撤消操作是 Ctrl + < kbd> B 。这是执行,你得到你以前的状态。所以,这里你以前的状态不是存储在内存中,而是在你需要时生成。



对于文本编辑器,以这种方式生成状态不是太计算密集,但对于像Adobe Photoshop这样的程序,它可能计算量太大或只是简单的不可能。例如 - 对于模糊操作,您将指定一个 de-Blur 操作,但是由于数据已丢失,因此无法让您处于原始状态。所以,根据情况 - 逻辑反向行动的可能性及其可行性,你需要在这两个大类之间进行选择,然后按照你想要的方式实现。当然,有可能有一个适合你的混合策略。



另外,有时像Gmail一样,有时限的撤消是可能的,因为动作(发送邮件)从未完成。所以,你不是撤消在那里,你只是不做的行动本身。


Give me some thoughts how to implement undo/redo functionality - like we have in text editors. What algorithms should I use and what I may read. thanks.

解决方案

I know about two major divisions of the types of undo's

  • SAVE STATE: One category of undo is where you actually save history states. In this case what happens is that at every point you keep on saving the state in some location of memory. When you want to do an undo, you just swap out the current state and swap in the state which was already there in the memory. This is how it is done with History in Adobe Photoshop or reopening closed tabs in Google Chrome, for example.

  • GENERATE STATE: The other category is where instead of maintaining the states themselves, you just remember what the actions were. when you need to undo, you need to do a logical reverse of that particular action. For a simple example, when you do a Ctrl+B in some text editor that supports undo's, it is remembered as a Bold action. Now with each action is a mapping of its logical reverses. So, when you do a Ctrl+Z, it looks up from a inverse actions table and finds that the undo action is a Ctrl+B again. That is performed and you get your previous state. So, here your previous state was not stored in memory but generated when you needed it.

For text editors, generating the state this way is not too computation intensive but for programs like Adobe Photoshop, it might be too computationally intensive or just plain impossible. For example - for a Blur action, you will specify a de-Blur action, but that can never get you to the original state because the data is already lost. So, depending on the situation - possibility of a logical inverse action and the feasibility of it, you need to choose between these two broad categories, and then implement them the way you want. Ofcourse, it is possible to have a hybrid strategy that works for you.

Also, sometimes, like in Gmail, a time limited undo is possible because the action (sending the mail) is never done in the first place. So, you are not "undo"ing there, you are just "not doing" the action itself.

这篇关于撤消/重做实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆