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.

127 lines
3.7KB

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