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.

136 lines
2.1KB

  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. struct ModuleWidget;
  10. struct CableWidget;
  11. namespace history {
  12. struct Action {
  13. virtual ~Action() {}
  14. virtual void undo() {}
  15. virtual void redo() {}
  16. };
  17. template <class TAction>
  18. struct InverseAction : TAction {
  19. void undo() override {
  20. TAction::redo();
  21. }
  22. void redo() override {
  23. TAction::undo();
  24. }
  25. };
  26. /** Batches multiple actions into one */
  27. struct ComplexAction : Action {
  28. /** Ordered by time occurred. Undoing will replay them backwards. */
  29. std::vector<Action*> actions;
  30. ~ComplexAction();
  31. void undo() override;
  32. void redo() override;
  33. void push(Action *action);
  34. };
  35. /** An action operating on a module
  36. Subclass this to create your own custom actions for your module.
  37. */
  38. struct ModuleAction : Action {
  39. int moduleId;
  40. };
  41. struct ModuleAdd : ModuleAction {
  42. Model *model;
  43. math::Vec pos;
  44. json_t *moduleJ;
  45. ~ModuleAdd();
  46. void setModule(ModuleWidget *mw);
  47. void undo() override;
  48. void redo() override;
  49. };
  50. struct ModuleRemove : InverseAction<ModuleAdd> {};
  51. struct ModuleMove : ModuleAction {
  52. math::Vec oldPos;
  53. math::Vec newPos;
  54. void undo() override;
  55. void redo() override;
  56. };
  57. struct ModuleBypass : ModuleAction {
  58. bool bypass;
  59. void undo() override;
  60. void redo() override;
  61. };
  62. struct ModuleChange : ModuleAction {
  63. json_t *oldModuleJ;
  64. json_t *newModuleJ;
  65. ~ModuleChange();
  66. void undo() override;
  67. void redo() override;
  68. };
  69. struct ParamChange : ModuleAction {
  70. int paramId;
  71. float oldValue;
  72. float newValue;
  73. void undo() override;
  74. void redo() override;
  75. };
  76. struct CableAdd : Action {
  77. int cableId;
  78. int outputModuleId;
  79. int outputId;
  80. int inputModuleId;
  81. int inputId;
  82. NVGcolor color;
  83. void setCable(CableWidget *cw);
  84. void undo() override;
  85. void redo() override;
  86. };
  87. struct CableRemove : InverseAction<CableAdd> {};
  88. struct State {
  89. std::vector<Action*> actions;
  90. int actionIndex = 0;
  91. ~State();
  92. void clear();
  93. void push(Action *action);
  94. void undo();
  95. void redo();
  96. bool canUndo();
  97. bool canRedo();
  98. };
  99. } // namespace history
  100. } // namespace rack