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.3KB

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