5 ms·
> I wound up using a stack-based architecture that pushed operations stored as functions onto a main stack then its reverse operation onto an undo stack. Once I
by jcelerier 9y ago
> I wound up using a stack-based architecture that pushed operations stored as functions onto a main stack then its reverse operation onto an undo stack. Once I worked out the state management problems it worked out quite well.
that's quite common: it's called the Command pattern. https://en.wikipedia.org/wiki/Command_pattern https://en.wikipedia.org/wiki/Command_pattern
Another common approach is Memento (basically, "save everything")
A nice thing that can be added is a pair of serialization / deserialization functions for the state needed for your commands. This way you can save them regularly and restore them if a crash or something like this happens ; the user can then continue to work while keeping its undo / redo stack
- manmal 9y agoThe command pattern only describes the encapsulation of an imperative, but not a complete undo/redo stack.
- jcelerier 9y agoTechnically, yes, but every book I read that talked about the Command pattern did so in the context of an undo-redo system
- flukus 9y ago> A nice thing that can be added is a pair of serialization / deserialization functions for the state needed for your commands. I wonder if this was the origin of modal text editors. Maybe they were designed for machine/programmer convenience more than machine convenience originally.
- ZenPsycho 9y agothey were designed around the constraints of slow teletype machines and slow 300 baud modems. they make no sense as a UI until you take into account how long you used to need to wait for a computer to respond
- DennisP 9y agoA modal editor is not necessarily less efficient in keystrokes than one using control keys; modal just means you don't have to hold your control keys down while completing the command. Using keyboard commands is often faster than pointing with a mouse, if you take advantage of structures in the text; e.g. "select everything inside the parens." And if you're doing that all day, it becomes second nature to build macros on the fly.
- ZenPsycho 9y agoLess efficient in keystrokes? who said anything about that?
- DennisP 9y agoIf you use a keyboard-based editor, presumably one that takes fewer keystrokes is a better UI, other things being equal. (There's also learning curve to consider but everyone has their own tradeoff on that.) Of course there are also mouse-based editors, which my second paragraph addresses.
- btown 9y agoMusescore (open-source music notation software) uses this pattern, for those interested... but not in a particularly DRY or stable way (so much duplicated logic!). I'm sure other open-source examples exist, but this should give you an idea: https://github.com/musescore/MuseScore/blob/master/libmscore/undo.cpp#L245 https://github.com/musescore/MuseScore/blob/master/libmscore... https://github.com/musescore/MuseScore/blob/master/libmscore/undo.cpp#L953 https://github.com/musescore/MuseScore/blob/master/libmscore... https://github.com/musescore/MuseScore/blob/c4af0c22bc90a6ba180f8596fad8e4000c64a028/libmscore/measure.cpp#L1086 https://github.com/musescore/MuseScore/blob/c4af0c22bc90a6ba...
- aaron-lebo 9y agohttp://gameprogrammingpatterns.com/command.html http://gameprogrammingpatterns.com/command.html
- jcelerier 9y agoHere's how I do it (interestingly, on a Qt-based art software too :p ) https://github.com/OSSIA/score/blob/master/base/lib/core/command/CommandStack.cpp#L147 https://github.com/OSSIA/score/blob/master/base/lib/core/com... https://github.com/OSSIA/score/blob/master/base/plugins/score-plugin-media/Media/Commands/ChangeAudioFile.cpp https://github.com/OSSIA/score/blob/master/base/plugins/scor... https://github.com/OSSIA/score/blob/master/base/plugins/score-plugin-midi/Midi/Commands/AddNote.cpp https://github.com/OSSIA/score/blob/master/base/plugins/scor... https://github.com/OSSIA/score/blob/master/base/plugins/score-plugin-automation/Automation/Commands/SetAutomationMin.hpp https://github.com/OSSIA/score/blob/master/base/plugins/scor... Also once your commands are serialized you can send them over the network for distributed edition