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.

ModuleWidget.hpp 4.2KB

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