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.

92 lines
2.1KB

  1. #pragma once
  2. #include <functional>
  3. #include "IMWidgets.hpp"
  4. class IPanelHost
  5. {
  6. public:
  7. virtual void setExpanded(bool) = 0;
  8. virtual bool isExpanded() = 0;
  9. };
  10. /**
  11. * Bundles up all the handling of expanding and contracting
  12. * the panel. Relies on IPanelHost to talk back to the
  13. * main widget.
  14. */
  15. class CHBPanelManager
  16. {
  17. public:
  18. CHBPanelManager(IPanelHost*);
  19. MenuItem* createMenuItem();
  20. void makePanel(ModuleWidget* widget);
  21. void addMenuItems(Menu*);
  22. void poll();
  23. private:
  24. DynamicSVGPanel* panel;
  25. int expWidth = 60;
  26. IPanelHost* const panelHost;
  27. ModuleWidget* widget = nullptr;
  28. void setPanelSize();
  29. };
  30. CHBPanelManager::CHBPanelManager(IPanelHost* host) : panelHost(host)
  31. {
  32. }
  33. inline void CHBPanelManager::addMenuItems(Menu* menu)
  34. {
  35. menu->addChild(createMenuItem());
  36. }
  37. inline void CHBPanelManager::setPanelSize()
  38. {
  39. widget->box.size = panel->box.size;
  40. const int expansionWidth = panelHost->isExpanded() ? 0 : -expWidth;
  41. widget->box.size.x += expansionWidth;
  42. }
  43. inline void CHBPanelManager::poll()
  44. {
  45. setPanelSize(); // TODO: only do on change? (possible optimization)
  46. }
  47. inline void CHBPanelManager::makePanel(ModuleWidget* wdg)
  48. {
  49. widget = wdg;
  50. panel = new DynamicSVGPanel();
  51. panel->box.size = Vec(16 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT);
  52. panel->box.size.x += expWidth;
  53. panel->expWidth = &expWidth;
  54. // I think this dynamically adds the real size. can get rid of the other stuff.
  55. panel->addPanel(SVG::load(assetPlugin(plugin, "res/cheby-wide-ghost.svg")));
  56. widget->box.size = panel->box.size;
  57. // printf("widget box a = %f exp=%f\n", widget->box.size.x, expWidth);
  58. // TODO: use setPanelSize
  59. const int expansionWidth = panelHost->isExpanded() ? 0 : -expWidth;
  60. widget->box.size.x += expansionWidth;
  61. widget->addChild(panel);
  62. }
  63. inline MenuItem* CHBPanelManager::createMenuItem()
  64. {
  65. auto statusCB = [this]() {
  66. return panelHost->isExpanded();
  67. };
  68. auto actionCB = [this]() {
  69. bool b = !panelHost->isExpanded();
  70. panelHost->setExpanded(b);
  71. setPanelSize();
  72. };
  73. return new SQPanelItem(statusCB, actionCB);
  74. }