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.

168 lines
2.6KB

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