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.

328 lines
7.6KB

  1. #pragma once
  2. #include <list>
  3. #include <memory>
  4. #include "../ext/nanovg/src/nanovg.h"
  5. #include "../ext/oui/blendish.h"
  6. #include "../ext/nanosvg/src/nanosvg.h"
  7. #include "math.hpp"
  8. #include "util.hpp"
  9. namespace rack {
  10. ////////////////////
  11. // resources
  12. ////////////////////
  13. // Constructing these directly will load from the disk each time. Use the load() functions to load from disk and cache them as long as the shared_ptr is held.
  14. // Implemented in gui.cpp
  15. struct Font {
  16. int handle;
  17. Font(const std::string &filename);
  18. ~Font();
  19. static std::shared_ptr<Font> load(const std::string &filename);
  20. };
  21. struct Image {
  22. int handle;
  23. Image(const std::string &filename);
  24. ~Image();
  25. static std::shared_ptr<Image> load(const std::string &filename);
  26. };
  27. struct SVG {
  28. NSVGimage *handle;
  29. SVG(const std::string &filename);
  30. ~SVG();
  31. static std::shared_ptr<SVG> load(const std::string &filename);
  32. };
  33. ////////////////////
  34. // base class and traits
  35. ////////////////////
  36. /** A node in the 2D scene graph */
  37. struct Widget {
  38. /** Stores position and size */
  39. Rect box = Rect(Vec(), Vec(INFINITY, INFINITY));
  40. Widget *parent = NULL;
  41. std::list<Widget*> children;
  42. virtual ~Widget();
  43. Vec getAbsolutePos();
  44. Rect getChildrenBoundingBox();
  45. template <class T>
  46. T *getAncestorOfType() {
  47. if (!parent) return NULL;
  48. T *p = dynamic_cast<T*>(parent);
  49. if (p) return p;
  50. return parent->getAncestorOfType<T>();
  51. }
  52. /** Adds widget to list of children.
  53. Gives ownership of widget to this widget instance.
  54. */
  55. void addChild(Widget *widget);
  56. /** Removes widget from list of children if it exists.
  57. Does not delete widget but transfers ownership to caller
  58. Silently fails if widget is not a child
  59. */
  60. void removeChild(Widget *widget);
  61. void clearChildren();
  62. /** Advances the module by one frame */
  63. virtual void step();
  64. /** Draws to NanoVG context */
  65. virtual void draw(NVGcontext *vg);
  66. // Events
  67. /** Called when a mouse button is pressed over this widget
  68. 0 for left, 1 for right, 2 for middle.
  69. Return `this` to accept the event.
  70. Return NULL to reject the event and pass it to the widget behind this one.
  71. */
  72. virtual Widget *onMouseDown(Vec pos, int button);
  73. virtual Widget *onMouseUp(Vec pos, int button);
  74. virtual Widget *onMouseMove(Vec pos, Vec mouseRel);
  75. /** Called when this widget begins responding to `onMouseMove` events */
  76. virtual void onMouseEnter() {}
  77. /** Called when another widget begins responding to `onMouseMove` events */
  78. virtual void onMouseLeave() {}
  79. virtual Widget *onScroll(Vec pos, Vec scrollRel);
  80. /** Called when a widget responds to `onMouseDown` for a left button press */
  81. virtual void onDragStart() {}
  82. /** Called when the left button is released and this widget is being dragged */
  83. virtual void onDragEnd() {}
  84. /** Called when a widget responds to `onMouseMove` and is being dragged */
  85. virtual void onDragMove(Vec mouseRel) {}
  86. /** Called when a widget responds to `onMouseUp` for a left button release and a widget is being dragged */
  87. virtual void onDragDrop(Widget *origin) {}
  88. virtual void onAction() {}
  89. virtual void onChange() {}
  90. };
  91. /** Widget that does not respond to events */
  92. struct TransparentWidget : virtual Widget {
  93. Widget *onMouseDown(Vec pos, int button) {return NULL;}
  94. Widget *onMouseUp(Vec pos, int button) {return NULL;}
  95. Widget *onMouseMove(Vec pos, Vec mouseRel) {return NULL;}
  96. Widget *onScroll(Vec pos, Vec scrollRel) {return NULL;}
  97. };
  98. /** Widget that itself responds to mouse events */
  99. struct OpaqueWidget : virtual Widget {
  100. Widget *onMouseDown(Vec pos, int button) {
  101. Widget *w = Widget::onMouseDown(pos, button);
  102. if (w) return w;
  103. onMouseDown(button);
  104. return this;
  105. }
  106. Widget *onMouseUp(Vec pos, int button) {
  107. Widget *w = Widget::onMouseUp(pos, button);
  108. if (w) return w;
  109. onMouseUp(button);
  110. return this;
  111. }
  112. Widget *onMouseMove(Vec pos, Vec mouseRel) {
  113. Widget *w = Widget::onMouseMove(pos, mouseRel);
  114. if (w) return w;
  115. onMouseMove(mouseRel);
  116. return this;
  117. }
  118. /** "High level" events called by the above lower level events.
  119. Use these if you don't care about the clicked position.
  120. */
  121. virtual void onMouseDown(int button) {}
  122. virtual void onMouseUp(int button) {}
  123. virtual void onMouseMove(Vec mouseRel) {}
  124. };
  125. struct SpriteWidget : virtual Widget {
  126. Vec spriteOffset;
  127. Vec spriteSize;
  128. std::shared_ptr<Image> spriteImage;
  129. int index = 0;
  130. void draw(NVGcontext *vg);
  131. };
  132. struct QuantityWidget : virtual Widget {
  133. float value = 0.0;
  134. float minValue = 0.0;
  135. float maxValue = 1.0;
  136. float defaultValue = 0.0;
  137. std::string label;
  138. /** Include a space character if you want a space after the number, e.g. " Hz" */
  139. std::string unit;
  140. /** The digit place to round for displaying values.
  141. A precision of -2 will display as "1.00" for example.
  142. */
  143. int precision = -2;
  144. QuantityWidget();
  145. void setValue(float value);
  146. void setLimits(float minValue, float maxValue);
  147. void setDefaultValue(float defaultValue);
  148. /** Generates the display value */
  149. std::string getText();
  150. };
  151. ////////////////////
  152. // gui elements
  153. ////////////////////
  154. struct Label : Widget {
  155. std::string text;
  156. Label() {
  157. box.size.y = BND_WIDGET_HEIGHT;
  158. }
  159. void draw(NVGcontext *vg);
  160. };
  161. // Deletes itself from parent when clicked
  162. struct MenuOverlay : OpaqueWidget {
  163. void step();
  164. Widget *onScroll(Vec pos, Vec scrollRel) {
  165. return this;
  166. }
  167. void onDragDrop(Widget *origin);
  168. };
  169. struct Menu : OpaqueWidget {
  170. Menu() {
  171. box.size = Vec(0, 0);
  172. }
  173. // Resizes menu and calls addChild()
  174. void pushChild(Widget *child);
  175. void draw(NVGcontext *vg);
  176. };
  177. struct MenuEntry : OpaqueWidget {
  178. std::string text;
  179. MenuEntry() {
  180. box.size = Vec(0, BND_WIDGET_HEIGHT);
  181. }
  182. float computeMinWidth(NVGcontext *vg);
  183. };
  184. struct MenuLabel : MenuEntry {
  185. void draw(NVGcontext *vg);
  186. };
  187. struct MenuItem : MenuEntry {
  188. BNDwidgetState state = BND_DEFAULT;
  189. void draw(NVGcontext *vg);
  190. void onMouseEnter();
  191. void onMouseLeave() ;
  192. void onDragDrop(Widget *origin);
  193. };
  194. struct Button : OpaqueWidget {
  195. std::string text;
  196. BNDwidgetState state = BND_DEFAULT;
  197. Button() {
  198. box.size.y = BND_WIDGET_HEIGHT;
  199. }
  200. void draw(NVGcontext *vg);
  201. void onMouseEnter();
  202. void onMouseLeave();
  203. void onDragStart();
  204. void onDragEnd();
  205. void onDragDrop(Widget *origin);
  206. };
  207. struct ChoiceButton : Button {
  208. void draw(NVGcontext *vg);
  209. };
  210. struct RadioButton : OpaqueWidget, QuantityWidget {
  211. BNDwidgetState state = BND_DEFAULT;
  212. RadioButton() {
  213. box.size.y = BND_WIDGET_HEIGHT;
  214. }
  215. void draw(NVGcontext *vg);
  216. void onMouseEnter();
  217. void onMouseLeave();
  218. void onDragDrop(Widget *origin);
  219. };
  220. struct Slider : OpaqueWidget, QuantityWidget {
  221. BNDwidgetState state = BND_DEFAULT;
  222. Slider() {
  223. box.size.y = BND_WIDGET_HEIGHT;
  224. }
  225. void draw(NVGcontext *vg);
  226. void onDragStart();
  227. void onDragMove(Vec mouseRel);
  228. void onDragEnd();
  229. };
  230. struct ScrollBar : OpaqueWidget {
  231. enum { VERTICAL, HORIZONTAL } orientation;
  232. float containerOffset = 0.0;
  233. float containerSize = 0.0;
  234. BNDwidgetState state = BND_DEFAULT;
  235. ScrollBar() {
  236. box.size = Vec(BND_SCROLLBAR_WIDTH, BND_SCROLLBAR_HEIGHT);
  237. }
  238. void draw(NVGcontext *vg);
  239. void move(float delta);
  240. void onDragStart();
  241. void onDragMove(Vec mouseRel);
  242. void onDragEnd();
  243. };
  244. // Handles a container with scrollbars
  245. struct ScrollWidget : OpaqueWidget {
  246. Widget *container;
  247. ScrollBar *hScrollBar;
  248. ScrollBar *vScrollBar;
  249. ScrollWidget();
  250. void step();
  251. void draw(NVGcontext *vg);
  252. Widget *onScroll(Vec pos, Vec scrollRel);
  253. };
  254. struct Tooltip : Widget {
  255. void step();
  256. void draw(NVGcontext *vg);
  257. };
  258. struct Scene : OpaqueWidget {
  259. Widget *overlay = NULL;
  260. void setOverlay(Widget *w);
  261. void step();
  262. };
  263. ////////////////////
  264. // globals
  265. ////////////////////
  266. extern Vec gMousePos;
  267. extern Widget *gHoveredWidget;
  268. extern Widget *gDraggedWidget;
  269. extern Widget *gSelectedWidget;
  270. extern int gGuiFrame;
  271. extern Scene *gScene;
  272. } // namespace rack