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.

155 lines
4.1KB

  1. #pragma once
  2. #include <list>
  3. #include "common.hpp"
  4. #include "events.hpp"
  5. #include "color.hpp"
  6. #include "widgets/Widget.hpp"
  7. namespace rack {
  8. ////////////////////
  9. // Base widget
  10. ////////////////////
  11. /** Instead of inheriting from Widget directly, inherit from VirtualWidget to guarantee that only one copy of Widget's member variables are used by each instance of the Widget hierarchy.
  12. */
  13. struct VirtualWidget : virtual Widget {};
  14. struct TransformWidget : VirtualWidget {
  15. /** The transformation matrix */
  16. float transform[6];
  17. TransformWidget();
  18. math::Rect getChildrenBoundingBox() override;
  19. void identity();
  20. void translate(math::Vec delta);
  21. void rotate(float angle);
  22. void scale(math::Vec s);
  23. void draw(NVGcontext *vg) override;
  24. };
  25. struct ZoomWidget : VirtualWidget {
  26. float zoom = 1.0;
  27. math::Vec getRelativeOffset(math::Vec v, Widget *relative) override;
  28. math::Rect getViewport(math::Rect r) override;
  29. void setZoom(float zoom);
  30. void draw(NVGcontext *vg) override;
  31. void onMouseDown(EventMouseDown &e) override;
  32. void onMouseUp(EventMouseUp &e) override;
  33. void onMouseMove(EventMouseMove &e) override;
  34. void onHoverKey(EventHoverKey &e) override;
  35. void onScroll(EventScroll &e) override;
  36. void onPathDrop(EventPathDrop &e) override;
  37. };
  38. ////////////////////
  39. // Trait widgets
  40. ////////////////////
  41. /** Widget that does not respond to events */
  42. struct TransparentWidget : VirtualWidget {
  43. void onMouseDown(EventMouseDown &e) override {}
  44. void onMouseUp(EventMouseUp &e) override {}
  45. void onMouseMove(EventMouseMove &e) override {}
  46. void onScroll(EventScroll &e) override {}
  47. };
  48. /** Widget that automatically responds to all mouse events but gives a chance for children to respond instead */
  49. struct OpaqueWidget : VirtualWidget {
  50. void onMouseDown(EventMouseDown &e) override {
  51. Widget::onMouseDown(e);
  52. if (!e.target)
  53. e.target = this;
  54. e.consumed = true;
  55. }
  56. void onMouseUp(EventMouseUp &e) override {
  57. Widget::onMouseUp(e);
  58. if (!e.target)
  59. e.target = this;
  60. e.consumed = true;
  61. }
  62. void onMouseMove(EventMouseMove &e) override {
  63. Widget::onMouseMove(e);
  64. if (!e.target)
  65. e.target = this;
  66. e.consumed = true;
  67. }
  68. void onScroll(EventScroll &e) override {
  69. Widget::onScroll(e);
  70. e.consumed = true;
  71. }
  72. };
  73. struct SVGWidget : VirtualWidget {
  74. std::shared_ptr<SVG> svg;
  75. /** Sets the box size to the svg image size */
  76. void wrap();
  77. /** Sets and wraps the SVG */
  78. void setSVG(std::shared_ptr<SVG> svg);
  79. void draw(NVGcontext *vg) override;
  80. };
  81. /** Caches a widget's draw() result to a framebuffer so it is called less frequently
  82. When `dirty` is true, its children will be re-rendered on the next call to step() override.
  83. Events are not passed to the underlying scene.
  84. */
  85. struct FramebufferWidget : VirtualWidget {
  86. /** Set this to true to re-render the children to the framebuffer the next time it is drawn */
  87. bool dirty = true;
  88. /** A margin in pixels around the children in the framebuffer
  89. This prevents cutting the rendered SVG off on the box edges.
  90. */
  91. float oversample;
  92. /** The root object in the framebuffer scene
  93. The FramebufferWidget owns the pointer
  94. */
  95. struct Internal;
  96. Internal *internal;
  97. FramebufferWidget();
  98. ~FramebufferWidget();
  99. void draw(NVGcontext *vg) override;
  100. int getImageHandle();
  101. void onZoom(EventZoom &e) override;
  102. };
  103. /** A Widget representing a float value */
  104. struct QuantityWidget : VirtualWidget {
  105. float value = 0.0;
  106. float minValue = 0.0;
  107. float maxValue = 1.0;
  108. float defaultValue = 0.0;
  109. std::string label;
  110. /** Include a space character if you want a space after the number, e.g. " Hz" */
  111. std::string unit;
  112. /** The decimal place to round for displaying values.
  113. A precision of 2 will display as "1.00" for example.
  114. */
  115. int precision = 2;
  116. QuantityWidget();
  117. void setValue(float value);
  118. void setLimits(float minValue, float maxValue);
  119. void setDefaultValue(float defaultValue);
  120. /** Generates the display value */
  121. std::string getText();
  122. };
  123. ////////////////////
  124. // globals
  125. ////////////////////
  126. extern Widget *gHoveredWidget;
  127. extern Widget *gDraggedWidget;
  128. extern Widget *gDragHoveredWidget;
  129. extern Widget *gFocusedWidget;
  130. extern Widget *gTempWidget;
  131. } // namespace rack