You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

history.hpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #pragma once
  2. #include <common.hpp>
  3. #include <math.hpp>
  4. #include <color.hpp>
  5. #include <plugin/Model.hpp>
  6. #include <vector>
  7. #include <jansson.h>
  8. namespace rack {
  9. namespace app {
  10. struct ModuleWidget;
  11. struct CableWidget;
  12. } // namespace app
  13. /** Undo history actions for the Rack application */
  14. namespace history {
  15. /** An undo action with an inverse redo action.
  16. Pointers to Modules, Params, etc. are not allowed in Actions because the object they refer to may be deleted and restored.
  17. Instead, use moduleIds, etc.
  18. */
  19. struct Action {
  20. /** Name of the action, lowercase. Used in the phrase "Undo ..." */
  21. std::string name;
  22. virtual ~Action() {}
  23. virtual void undo() {}
  24. virtual void redo() {}
  25. };
  26. template <class TAction>
  27. struct InverseAction : TAction {
  28. void undo() override {
  29. TAction::redo();
  30. }
  31. void redo() override {
  32. TAction::undo();
  33. }
  34. };
  35. /** Batches multiple actions into one */
  36. struct ComplexAction : Action {
  37. /** Ordered by time occurred. Undoing will replay them backwards. */
  38. std::vector<Action*> actions;
  39. ~ComplexAction();
  40. void undo() override;
  41. void redo() override;
  42. void push(Action *action);
  43. bool isEmpty();
  44. };
  45. /** An action operating on a module.
  46. Subclass this to create your own custom actions for your module.
  47. */
  48. struct ModuleAction : Action {
  49. int moduleId;
  50. };
  51. struct ModuleAdd : ModuleAction {
  52. plugin::Model *model;
  53. math::Vec pos;
  54. json_t *moduleJ;
  55. ModuleAdd() {
  56. name = "add module";
  57. }
  58. ~ModuleAdd();
  59. void setModule(app::ModuleWidget *mw);
  60. void undo() override;
  61. void redo() override;
  62. };
  63. struct ModuleRemove : InverseAction<ModuleAdd> {
  64. ModuleRemove() {
  65. name = "remove module";
  66. }
  67. };
  68. struct ModuleMove : ModuleAction {
  69. math::Vec oldPos;
  70. math::Vec newPos;
  71. void undo() override;
  72. void redo() override;
  73. ModuleMove() {
  74. name = "move module";
  75. }
  76. };
  77. struct ModuleBypass : ModuleAction {
  78. bool bypass;
  79. void undo() override;
  80. void redo() override;
  81. ModuleBypass() {
  82. name = "bypass module";
  83. }
  84. };
  85. struct ModuleChange : ModuleAction {
  86. json_t *oldModuleJ;
  87. json_t *newModuleJ;
  88. ModuleChange() {
  89. name = "change module";
  90. }
  91. ~ModuleChange();
  92. void undo() override;
  93. void redo() override;
  94. };
  95. struct ParamChange : ModuleAction {
  96. int paramId;
  97. float oldValue;
  98. float newValue;
  99. void undo() override;
  100. void redo() override;
  101. ParamChange() {
  102. name = "change parameter";
  103. }
  104. };
  105. struct CableAdd : Action {
  106. int cableId;
  107. int outputModuleId;
  108. int outputId;
  109. int inputModuleId;
  110. int inputId;
  111. NVGcolor color;
  112. void setCable(app::CableWidget *cw);
  113. void undo() override;
  114. void redo() override;
  115. CableAdd() {
  116. name = "add cable";
  117. }
  118. };
  119. struct CableRemove : InverseAction<CableAdd> {
  120. CableRemove() {
  121. name = "remove cable";
  122. }
  123. };
  124. struct State {
  125. std::vector<Action*> actions;
  126. int actionIndex;
  127. /** Action index of saved patch state. */
  128. int savedIndex;
  129. State();
  130. ~State();
  131. void clear();
  132. void push(Action *action);
  133. void undo();
  134. void redo();
  135. bool canUndo();
  136. bool canRedo();
  137. std::string getUndoName();
  138. std::string getRedoName();
  139. void setSaved();
  140. bool isSaved();
  141. };
  142. } // namespace history
  143. } // namespace rack