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.

404 lines
9.4KB

  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 widget
  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 void onSelect() {}
  80. virtual void onDeselect() {}
  81. virtual void onText(int codepoint) {}
  82. virtual void onKey(int key) {}
  83. virtual Widget *onScroll(Vec pos, Vec scrollRel);
  84. /** Called when a widget responds to `onMouseDown` for a left button press */
  85. virtual void onDragStart() {}
  86. /** Called when the left button is released and this widget is being dragged */
  87. virtual void onDragEnd() {}
  88. /** Called when a widget responds to `onMouseMove` and is being dragged */
  89. virtual void onDragMove(Vec mouseRel) {}
  90. /** Called when a widget responds to `onMouseUp` for a left button release and a widget is being dragged */
  91. virtual void onDragEnter(Widget *origin) {}
  92. virtual void onDragLeave(Widget *origin) {}
  93. virtual void onDragDrop(Widget *origin) {}
  94. virtual void onAction() {}
  95. virtual void onChange() {}
  96. };
  97. struct TransformWidget : Widget {
  98. /** The transformation matrix */
  99. float transform[6];
  100. TransformWidget();
  101. void identity();
  102. void translate(Vec delta);
  103. void rotate(float angle);
  104. void scale(Vec s);
  105. void draw(NVGcontext *vg);
  106. };
  107. ////////////////////
  108. // Trait widgets
  109. ////////////////////
  110. /** Widget that does not respond to events */
  111. struct TransparentWidget : virtual Widget {
  112. Widget *onMouseDown(Vec pos, int button) {return NULL;}
  113. Widget *onMouseUp(Vec pos, int button) {return NULL;}
  114. Widget *onMouseMove(Vec pos, Vec mouseRel) {return NULL;}
  115. Widget *onScroll(Vec pos, Vec scrollRel) {return NULL;}
  116. };
  117. /** Widget that itself responds to mouse events */
  118. struct OpaqueWidget : virtual Widget {
  119. Widget *onMouseDown(Vec pos, int button) {
  120. Widget *w = Widget::onMouseDown(pos, button);
  121. if (w) return w;
  122. onMouseDown(button);
  123. return this;
  124. }
  125. Widget *onMouseUp(Vec pos, int button) {
  126. Widget *w = Widget::onMouseUp(pos, button);
  127. if (w) return w;
  128. onMouseUp(button);
  129. return this;
  130. }
  131. Widget *onMouseMove(Vec pos, Vec mouseRel) {
  132. Widget *w = Widget::onMouseMove(pos, mouseRel);
  133. if (w) return w;
  134. onMouseMove(mouseRel);
  135. return this;
  136. }
  137. /** "High level" events called by the above lower level events.
  138. Use these if you don't care about the clicked position.
  139. */
  140. virtual void onMouseDown(int button) {}
  141. virtual void onMouseUp(int button) {}
  142. virtual void onMouseMove(Vec mouseRel) {}
  143. };
  144. struct SpriteWidget : virtual Widget {
  145. Vec spriteOffset;
  146. Vec spriteSize;
  147. std::shared_ptr<Image> spriteImage;
  148. int index = 0;
  149. void draw(NVGcontext *vg);
  150. };
  151. struct SVGWidget : virtual Widget {
  152. std::shared_ptr<SVG> svg;
  153. /** Sets the box size to the svg image size */
  154. void wrap();
  155. void draw(NVGcontext *vg);
  156. };
  157. /** Caches a widget's draw() result to a framebuffer so it is called less frequently
  158. When `dirty` is true, `scene` will be re-rendered on the next call to step().
  159. Events are not passed to the underlying scene.
  160. */
  161. struct FramebufferWidget : virtual Widget {
  162. /** Set this to true to re-render the scene to the framebuffer in the next step() */
  163. bool dirty = true;
  164. /** A margin in pixels around the scene in the framebuffer
  165. This prevents cutting the rendered SVG off on the box edges.
  166. */
  167. int margin = 0;
  168. /** The root object in the framebuffer scene
  169. The FramebufferWidget owns the pointer
  170. */
  171. Widget *scene = NULL;
  172. struct Internal;
  173. Internal *internal;
  174. FramebufferWidget();
  175. ~FramebufferWidget();
  176. void setScene(Widget *w) {
  177. scene = w;
  178. }
  179. void step();
  180. void draw(NVGcontext *vg);
  181. };
  182. struct QuantityWidget : virtual Widget {
  183. float value = 0.0;
  184. float minValue = 0.0;
  185. float maxValue = 1.0;
  186. float defaultValue = 0.0;
  187. std::string label;
  188. /** Include a space character if you want a space after the number, e.g. " Hz" */
  189. std::string unit;
  190. /** The digit place to round for displaying values.
  191. A precision of -2 will display as "1.00" for example.
  192. */
  193. int precision = -2;
  194. QuantityWidget();
  195. void setValue(float value);
  196. void setLimits(float minValue, float maxValue);
  197. void setDefaultValue(float defaultValue);
  198. /** Generates the display value */
  199. std::string getText();
  200. };
  201. ////////////////////
  202. // GUI widgets
  203. ////////////////////
  204. struct Label : Widget {
  205. std::string text;
  206. Label() {
  207. box.size.y = BND_WIDGET_HEIGHT;
  208. }
  209. void draw(NVGcontext *vg);
  210. };
  211. // Deletes itself from parent when clicked
  212. struct MenuOverlay : OpaqueWidget {
  213. void step();
  214. Widget *onScroll(Vec pos, Vec scrollRel) {
  215. return this;
  216. }
  217. void onDragDrop(Widget *origin);
  218. };
  219. struct Menu : OpaqueWidget {
  220. Menu() {
  221. box.size = Vec(0, 0);
  222. }
  223. // Resizes menu and calls addChild()
  224. void pushChild(Widget *child);
  225. void draw(NVGcontext *vg);
  226. };
  227. struct MenuEntry : OpaqueWidget {
  228. std::string text;
  229. MenuEntry() {
  230. box.size = Vec(0, BND_WIDGET_HEIGHT);
  231. }
  232. float computeMinWidth(NVGcontext *vg);
  233. };
  234. struct MenuLabel : MenuEntry {
  235. void draw(NVGcontext *vg);
  236. };
  237. struct MenuItem : MenuEntry {
  238. BNDwidgetState state = BND_DEFAULT;
  239. void draw(NVGcontext *vg);
  240. void onMouseEnter();
  241. void onMouseLeave() ;
  242. void onDragDrop(Widget *origin);
  243. };
  244. struct Button : OpaqueWidget {
  245. std::string text;
  246. BNDwidgetState state = BND_DEFAULT;
  247. Button() {
  248. box.size.y = BND_WIDGET_HEIGHT;
  249. }
  250. void draw(NVGcontext *vg);
  251. void onMouseEnter();
  252. void onMouseLeave();
  253. void onDragStart();
  254. void onDragEnd();
  255. void onDragDrop(Widget *origin);
  256. };
  257. struct ChoiceButton : Button {
  258. void draw(NVGcontext *vg);
  259. };
  260. struct RadioButton : OpaqueWidget, QuantityWidget {
  261. BNDwidgetState state = BND_DEFAULT;
  262. RadioButton() {
  263. box.size.y = BND_WIDGET_HEIGHT;
  264. }
  265. void draw(NVGcontext *vg);
  266. void onMouseEnter();
  267. void onMouseLeave();
  268. void onDragDrop(Widget *origin);
  269. };
  270. struct Slider : OpaqueWidget, QuantityWidget {
  271. BNDwidgetState state = BND_DEFAULT;
  272. Slider() {
  273. box.size.y = BND_WIDGET_HEIGHT;
  274. }
  275. void draw(NVGcontext *vg);
  276. void onDragStart();
  277. void onDragMove(Vec mouseRel);
  278. void onDragEnd();
  279. };
  280. struct ScrollBar : OpaqueWidget {
  281. enum { VERTICAL, HORIZONTAL } orientation;
  282. float containerOffset = 0.0;
  283. float containerSize = 0.0;
  284. BNDwidgetState state = BND_DEFAULT;
  285. ScrollBar() {
  286. box.size = Vec(BND_SCROLLBAR_WIDTH, BND_SCROLLBAR_HEIGHT);
  287. }
  288. void draw(NVGcontext *vg);
  289. void move(float delta);
  290. void onDragStart();
  291. void onDragMove(Vec mouseRel);
  292. void onDragEnd();
  293. };
  294. // Handles a container with scrollbars
  295. struct ScrollWidget : OpaqueWidget {
  296. Widget *container;
  297. ScrollBar *hScrollBar;
  298. ScrollBar *vScrollBar;
  299. ScrollWidget();
  300. void step();
  301. void draw(NVGcontext *vg);
  302. Widget *onScroll(Vec pos, Vec scrollRel);
  303. };
  304. struct TextField : OpaqueWidget {
  305. std::string text;
  306. int begin = 0;
  307. int end = 0;
  308. TextField() {
  309. box.size.y = BND_WIDGET_HEIGHT;
  310. }
  311. void draw(NVGcontext *vg);
  312. Widget *onMouseDown(Vec pos, int button);
  313. void onText(int codepoint);
  314. void onKey(int scancode);
  315. void onSelect();
  316. };
  317. struct PasswordField : TextField {
  318. void draw(NVGcontext *vg);
  319. };
  320. struct Tooltip : Widget {
  321. void step();
  322. void draw(NVGcontext *vg);
  323. };
  324. struct Scene : OpaqueWidget {
  325. Widget *overlay = NULL;
  326. void setOverlay(Widget *w);
  327. void step();
  328. };
  329. ////////////////////
  330. // globals
  331. ////////////////////
  332. extern Vec gMousePos;
  333. extern Widget *gHoveredWidget;
  334. extern Widget *gDraggedWidget;
  335. extern Widget *gDragHoveredWidget;
  336. extern Widget *gSelectedWidget;
  337. extern int gGuiFrame;
  338. extern Scene *gScene;
  339. } // namespace rack