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.

139 lines
4.0KB

  1. #pragma once
  2. #include <app/common.hpp>
  3. #include <widget/OpaqueWidget.hpp>
  4. #include <ui/Menu.hpp>
  5. #include <app/PortWidget.hpp>
  6. #include <app/ParamWidget.hpp>
  7. #include <plugin/Model.hpp>
  8. #include <engine/Module.hpp>
  9. #include <history.hpp>
  10. namespace rack {
  11. namespace app {
  12. /** Manages an engine::Module in the rack. */
  13. struct ModuleWidget : widget::OpaqueWidget {
  14. struct Internal;
  15. Internal* internal;
  16. plugin::Model* model = NULL;
  17. /** Owned */
  18. engine::Module* module = NULL;
  19. ModuleWidget();
  20. DEPRECATED ModuleWidget(engine::Module* module) : ModuleWidget() {
  21. setModule(module);
  22. }
  23. ~ModuleWidget();
  24. plugin::Model* getModel();
  25. void setModel(plugin::Model* model);
  26. /** Returns the attached Module. */
  27. engine::Module* getModule();
  28. /** Returns the attached Module, casted to the given Module type. */
  29. template <class TModule>
  30. TModule* getModule() {
  31. return dynamic_cast<TModule*>(getModule());
  32. }
  33. /** Associates this ModuleWidget with the Module.
  34. Transfers ownership.
  35. */
  36. void setModule(engine::Module* module);
  37. widget::Widget* getPanel();
  38. /** Sets the panel and sets the size of the ModuleWidget from the panel.
  39. Transfers ownership.
  40. */
  41. void setPanel(widget::Widget* panel);
  42. void setPanel(std::shared_ptr<window::Svg> svg);
  43. /** Convenience functions for adding special widgets.
  44. Just calls addChild() with additional checking.
  45. It is not required to call this method. You may instead use addChild() in a child widget for example.
  46. */
  47. void addParam(ParamWidget* param);
  48. void addInput(PortWidget* input);
  49. void addOutput(PortWidget* output);
  50. /** Scans children widgets recursively for a ParamWidget with the given paramId. */
  51. ParamWidget* getParam(int paramId);
  52. PortWidget* getInput(int portId);
  53. PortWidget* getOutput(int portId);
  54. /** Scans children widgets recursively for all ParamWidgets. */
  55. std::vector<ParamWidget*> getParams();
  56. std::vector<PortWidget*> getPorts();
  57. std::vector<PortWidget*> getInputs();
  58. std::vector<PortWidget*> getOutputs();
  59. void draw(const DrawArgs& args) override;
  60. void drawLayer(const DrawArgs& args, int layer) override;
  61. /** Override to add context menu entries to your subclass.
  62. It is recommended to add a blank `ui::MenuSeparator` first for spacing.
  63. */
  64. virtual void appendContextMenu(ui::Menu* menu) {}
  65. void onHover(const HoverEvent& e) override;
  66. void onHoverKey(const HoverKeyEvent& e) override;
  67. void onButton(const ButtonEvent& e) override;
  68. void onDragStart(const DragStartEvent& e) override;
  69. void onDragEnd(const DragEndEvent& e) override;
  70. void onDragMove(const DragMoveEvent& e) override;
  71. void onDragHover(const DragHoverEvent& e) override;
  72. json_t* toJson();
  73. void fromJson(json_t* rootJ);
  74. /** Returns whether paste was successful. */
  75. bool pasteJsonAction(json_t* rootJ);
  76. void copyClipboard();
  77. bool pasteClipboardAction();
  78. void load(std::string filename);
  79. void loadAction(std::string filename);
  80. void loadTemplate();
  81. void loadDialog();
  82. void save(std::string filename);
  83. void saveTemplate();
  84. void saveTemplateDialog();
  85. bool hasTemplate();
  86. void clearTemplate();
  87. void clearTemplateDialog();
  88. void saveDialog();
  89. /** Disconnects cables from all ports
  90. Called when the user clicks Disconnect Cables in the context menu.
  91. */
  92. void disconnect();
  93. /** Resets the parameters of the module and calls the Module's randomize().
  94. Called when the user clicks Initialize in the context menu.
  95. */
  96. void resetAction();
  97. /** Randomizes the parameters of the module and calls the Module's randomize().
  98. Called when the user clicks Randomize in the context menu.
  99. */
  100. void randomizeAction();
  101. void appendDisconnectActions(history::ComplexAction* complexAction);
  102. void disconnectAction();
  103. void cloneAction(bool cloneCables = true);
  104. void bypassAction(bool bypassed);
  105. /** Deletes `this` */
  106. void removeAction();
  107. void createContextMenu();
  108. // Returns the rack position in grid coordinates
  109. math::Vec getGridPosition();
  110. void setGridPosition(math::Vec pos);
  111. math::Vec getGridSize();
  112. math::Rect getGridBox();
  113. PRIVATE math::Vec& dragOffset();
  114. PRIVATE bool& dragEnabled();
  115. PRIVATE engine::Module* releaseModule();
  116. };
  117. } // namespace app
  118. } // namespace rack