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.

169 lines
2.7KB

  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. struct Action {
  16. /** Name of the action, lowercase. Used in the phrase "Undo ..." */
  17. std::string name;
  18. virtual ~Action() {}
  19. virtual void undo() {}
  20. virtual void redo() {}
  21. };
  22. template <class TAction>
  23. struct InverseAction : TAction {
  24. void undo() override {
  25. TAction::redo();
  26. }
  27. void redo() override {
  28. TAction::undo();
  29. }
  30. };
  31. /** Batches multiple actions into one */
  32. struct ComplexAction : Action {
  33. /** Ordered by time occurred. Undoing will replay them backwards. */
  34. std::vector<Action*> actions;
  35. ~ComplexAction();
  36. void undo() override;
  37. void redo() override;
  38. void push(Action *action);
  39. };
  40. /** An action operating on a module
  41. Subclass this to create your own custom actions for your module.
  42. */
  43. struct ModuleAction : Action {
  44. int moduleId;
  45. };
  46. struct ModuleAdd : ModuleAction {
  47. plugin::Model *model;
  48. math::Vec pos;
  49. json_t *moduleJ;
  50. ModuleAdd() {
  51. name = "add module";
  52. }
  53. ~ModuleAdd();
  54. void setModule(app::ModuleWidget *mw);
  55. void undo() override;
  56. void redo() override;
  57. };
  58. struct ModuleRemove : InverseAction<ModuleAdd> {
  59. ModuleRemove() {
  60. name = "remove module";
  61. }
  62. };
  63. struct ModuleMove : ModuleAction {
  64. math::Vec oldPos;
  65. math::Vec newPos;
  66. void undo() override;
  67. void redo() override;
  68. ModuleMove() {
  69. name = "move module";
  70. }
  71. };
  72. struct ModuleBypass : ModuleAction {
  73. bool bypass;
  74. void undo() override;
  75. void redo() override;
  76. ModuleBypass() {
  77. name = "bypass module";
  78. }
  79. };
  80. struct ModuleChange : ModuleAction {
  81. json_t *oldModuleJ;
  82. json_t *newModuleJ;
  83. ModuleChange() {
  84. name = "change module";
  85. }
  86. ~ModuleChange();
  87. void undo() override;
  88. void redo() override;
  89. };
  90. struct ParamChange : ModuleAction {
  91. int paramId;
  92. float oldValue;
  93. float newValue;
  94. void undo() override;
  95. void redo() override;
  96. ParamChange() {
  97. name = "change parameter";
  98. }
  99. };
  100. struct CableAdd : Action {
  101. int cableId;
  102. int outputModuleId;
  103. int outputId;
  104. int inputModuleId;
  105. int inputId;
  106. NVGcolor color;
  107. void setCable(app::CableWidget *cw);
  108. void undo() override;
  109. void redo() override;
  110. CableAdd() {
  111. name = "add cable";
  112. }
  113. };
  114. struct CableRemove : InverseAction<CableAdd> {
  115. CableRemove() {
  116. name = "remove cable";
  117. }
  118. };
  119. struct State {
  120. std::vector<Action*> actions;
  121. int actionIndex = 0;
  122. ~State();
  123. void clear();
  124. void push(Action *action);
  125. void undo();
  126. void redo();
  127. bool canUndo();
  128. bool canRedo();
  129. std::string getUndoName();
  130. std::string getRedoName();
  131. };
  132. } // namespace history
  133. } // namespace rack