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.

453 lines
11KB

  1. #pragma once
  2. #include <list>
  3. #include <memory>
  4. #include "../ext/nanovg/src/nanovg.h"
  5. #include "../ext/oui-blendish/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. bool visible = true;
  43. virtual ~Widget();
  44. Vec getAbsolutePos();
  45. Rect getChildrenBoundingBox();
  46. template <class T>
  47. T *getAncestorOfType() {
  48. if (!parent) return NULL;
  49. T *p = dynamic_cast<T*>(parent);
  50. if (p) return p;
  51. return parent->getAncestorOfType<T>();
  52. }
  53. template <class T>
  54. T *getFirstDescendantOfType() {
  55. for (Widget *child : children) {
  56. T *c = dynamic_cast<T*>(child);
  57. if (c) return c;
  58. c = child->getFirstDescendantOfType<T>();
  59. if (c) return c;
  60. }
  61. return NULL;
  62. }
  63. /** Adds widget to list of children.
  64. Gives ownership of widget to this widget instance.
  65. */
  66. void addChild(Widget *widget);
  67. /** Removes widget from list of children if it exists.
  68. Does not delete widget but transfers ownership to caller
  69. Silently fails if widget is not a child
  70. */
  71. void removeChild(Widget *widget);
  72. void clearChildren();
  73. /** Recursively finalizes event start/end pairs as needed */
  74. void finalizeEvents();
  75. /** Advances the module by one frame */
  76. virtual void step();
  77. /** Draws to NanoVG context */
  78. virtual void draw(NVGcontext *vg);
  79. // Events
  80. /** Called when a mouse button is pressed over this widget
  81. 0 for left, 1 for right, 2 for middle.
  82. Return `this` to accept the event.
  83. Return NULL to reject the event and pass it to the widget behind this one.
  84. */
  85. virtual Widget *onMouseDown(Vec pos, int button);
  86. virtual Widget *onMouseUp(Vec pos, int button);
  87. /** Called on every frame, even if mouseRel = Vec(0, 0) */
  88. virtual Widget *onMouseMove(Vec pos, Vec mouseRel);
  89. virtual Widget *onHoverKey(Vec pos, int key);
  90. /** Called when this widget begins responding to `onMouseMove` events */
  91. virtual void onMouseEnter() {}
  92. /** Called when another widget begins responding to `onMouseMove` events */
  93. virtual void onMouseLeave() {}
  94. virtual void onFocus() {}
  95. virtual void onDefocus() {}
  96. virtual bool onFocusText(int codepoint) {return false;}
  97. virtual bool onFocusKey(int key) {return false;}
  98. virtual Widget *onScroll(Vec pos, Vec scrollRel);
  99. /** Called when a widget responds to `onMouseDown` for a left button press */
  100. virtual void onDragStart() {}
  101. /** Called when the left button is released and this widget is being dragged */
  102. virtual void onDragEnd() {}
  103. /** Called when a widget responds to `onMouseMove` and is being dragged */
  104. virtual void onDragMove(Vec mouseRel) {}
  105. /** Called when a widget responds to `onMouseUp` for a left button release and a widget is being dragged */
  106. virtual void onDragEnter(Widget *origin) {}
  107. virtual void onDragLeave(Widget *origin) {}
  108. virtual void onDragDrop(Widget *origin) {}
  109. virtual void onAction() {}
  110. virtual void onChange() {}
  111. };
  112. struct TransformWidget : Widget {
  113. /** The transformation matrix */
  114. float transform[6];
  115. TransformWidget();
  116. void identity();
  117. void translate(Vec delta);
  118. void rotate(float angle);
  119. void scale(Vec s);
  120. void draw(NVGcontext *vg);
  121. };
  122. ////////////////////
  123. // Trait widgets
  124. ////////////////////
  125. /** Widget that does not respond to events */
  126. struct TransparentWidget : virtual Widget {
  127. Widget *onMouseDown(Vec pos, int button) {return NULL;}
  128. Widget *onMouseUp(Vec pos, int button) {return NULL;}
  129. Widget *onMouseMove(Vec pos, Vec mouseRel) {return NULL;}
  130. Widget *onScroll(Vec pos, Vec scrollRel) {return NULL;}
  131. };
  132. /** Widget that automatically responds to all mouse events but gives a chance for children to respond instead */
  133. struct OpaqueWidget : virtual Widget {
  134. Widget *onMouseDown(Vec pos, int button) {
  135. Widget *w = Widget::onMouseDown(pos, button);
  136. if (w) return w;
  137. onMouseDownOpaque(button);
  138. return this;
  139. }
  140. Widget *onMouseUp(Vec pos, int button) {
  141. Widget *w = Widget::onMouseUp(pos, button);
  142. if (w) return w;
  143. onMouseUpOpaque(button);
  144. return this;
  145. }
  146. Widget *onMouseMove(Vec pos, Vec mouseRel) {
  147. Widget *w = Widget::onMouseMove(pos, mouseRel);
  148. if (w) return w;
  149. onMouseMoveOpaque(mouseRel);
  150. return this;
  151. }
  152. Widget *onScroll(Vec pos, Vec scrollRel) {
  153. Widget *w = Widget::onScroll(pos, scrollRel);
  154. if (w) return w;
  155. if (onScrollOpaque(scrollRel))
  156. return this;
  157. return NULL;
  158. }
  159. /** "High level" events called by the above lower level events.
  160. Use these if you don't care about the clicked position.
  161. */
  162. virtual void onMouseDownOpaque(int button) {}
  163. virtual void onMouseUpOpaque(int button) {}
  164. virtual void onMouseMoveOpaque(Vec mouseRel) {}
  165. virtual bool onScrollOpaque(Vec scrollRel) {return false;}
  166. };
  167. struct SpriteWidget : virtual Widget {
  168. Vec spriteOffset;
  169. Vec spriteSize;
  170. std::shared_ptr<Image> spriteImage;
  171. int index = 0;
  172. void draw(NVGcontext *vg);
  173. };
  174. struct SVGWidget : virtual Widget {
  175. std::shared_ptr<SVG> svg;
  176. /** Sets the box size to the svg image size */
  177. void wrap();
  178. void draw(NVGcontext *vg);
  179. };
  180. /** Caches a widget's draw() result to a framebuffer so it is called less frequently
  181. When `dirty` is true, its children will be re-rendered on the next call to step().
  182. Events are not passed to the underlying scene.
  183. */
  184. struct FramebufferWidget : virtual Widget {
  185. /** Set this to true to re-render the children to the framebuffer in the next step() */
  186. bool dirty = true;
  187. /** A margin in pixels around the children in the framebuffer
  188. This prevents cutting the rendered SVG off on the box edges.
  189. */
  190. float oversample = 2.0;
  191. /** The root object in the framebuffer scene
  192. The FramebufferWidget owns the pointer
  193. */
  194. struct Internal;
  195. Internal *internal;
  196. FramebufferWidget();
  197. ~FramebufferWidget();
  198. void step();
  199. void draw(NVGcontext *vg);
  200. int getImageHandle();
  201. };
  202. struct QuantityWidget : virtual Widget {
  203. float value = 0.0;
  204. float minValue = 0.0;
  205. float maxValue = 1.0;
  206. float defaultValue = 0.0;
  207. std::string label;
  208. /** Include a space character if you want a space after the number, e.g. " Hz" */
  209. std::string unit;
  210. /** The decimal place to round for displaying values.
  211. A precision of 2 will display as "1.00" for example.
  212. */
  213. int precision = 2;
  214. QuantityWidget();
  215. void setValue(float value);
  216. void setLimits(float minValue, float maxValue);
  217. void setDefaultValue(float defaultValue);
  218. /** Generates the display value */
  219. std::string getText();
  220. };
  221. ////////////////////
  222. // GUI widgets
  223. ////////////////////
  224. struct Label : Widget {
  225. std::string text;
  226. Label() {
  227. box.size.y = BND_WIDGET_HEIGHT;
  228. }
  229. void draw(NVGcontext *vg);
  230. };
  231. // Deletes itself from parent when clicked
  232. struct MenuOverlay : OpaqueWidget {
  233. void onDragDrop(Widget *origin);
  234. bool onScrollOpaque(Vec scrollRel) {return true;}
  235. Widget *onHoverKey(Vec pos, int key);
  236. };
  237. struct Menu : OpaqueWidget {
  238. Menu *parentMenu = NULL;
  239. Menu *childMenu = NULL;
  240. Menu() {
  241. box.size = Vec(0, 0);
  242. }
  243. ~Menu();
  244. // Resizes menu and calls addChild()
  245. void pushChild(Widget *child);
  246. void setChildMenu(Menu *menu);
  247. void fit();
  248. void step();
  249. void draw(NVGcontext *vg);
  250. bool onScrollOpaque(Vec scrollRel);
  251. };
  252. struct MenuEntry : OpaqueWidget {
  253. std::string text;
  254. std::string rightText;
  255. MenuEntry() {
  256. box.size = Vec(0, BND_WIDGET_HEIGHT);
  257. }
  258. virtual float computeMinWidth(NVGcontext *vg);
  259. };
  260. struct MenuLabel : MenuEntry {
  261. void draw(NVGcontext *vg);
  262. };
  263. struct MenuItem : MenuEntry {
  264. BNDwidgetState state = BND_DEFAULT;
  265. float computeMinWidth(NVGcontext *vg);
  266. void draw(NVGcontext *vg);
  267. virtual Menu *createChildMenu() {return NULL;}
  268. void onMouseEnter();
  269. void onMouseLeave();
  270. void onDragDrop(Widget *origin);
  271. };
  272. struct Button : OpaqueWidget {
  273. std::string text;
  274. BNDwidgetState state = BND_DEFAULT;
  275. Button() {
  276. box.size.y = BND_WIDGET_HEIGHT;
  277. }
  278. void draw(NVGcontext *vg);
  279. void onMouseEnter();
  280. void onMouseLeave();
  281. void onDragStart();
  282. void onDragEnd();
  283. void onDragDrop(Widget *origin);
  284. };
  285. struct ChoiceButton : Button {
  286. void draw(NVGcontext *vg);
  287. };
  288. struct RadioButton : OpaqueWidget, QuantityWidget {
  289. BNDwidgetState state = BND_DEFAULT;
  290. RadioButton() {
  291. box.size.y = BND_WIDGET_HEIGHT;
  292. }
  293. void draw(NVGcontext *vg);
  294. void onMouseEnter();
  295. void onMouseLeave();
  296. void onDragDrop(Widget *origin);
  297. };
  298. struct Slider : OpaqueWidget, QuantityWidget {
  299. BNDwidgetState state = BND_DEFAULT;
  300. Slider() {
  301. box.size.y = BND_WIDGET_HEIGHT;
  302. }
  303. void draw(NVGcontext *vg);
  304. void onDragStart();
  305. void onDragMove(Vec mouseRel);
  306. void onDragEnd();
  307. };
  308. /** Parent must be a ScrollWidget */
  309. struct ScrollBar : OpaqueWidget {
  310. enum { VERTICAL, HORIZONTAL } orientation;
  311. BNDwidgetState state = BND_DEFAULT;
  312. ScrollBar() {
  313. box.size = Vec(BND_SCROLLBAR_WIDTH, BND_SCROLLBAR_HEIGHT);
  314. }
  315. void draw(NVGcontext *vg);
  316. void onDragStart();
  317. void onDragMove(Vec mouseRel);
  318. void onDragEnd();
  319. };
  320. /** Handles a container with ScrollBar */
  321. struct ScrollWidget : OpaqueWidget {
  322. Widget *container;
  323. ScrollBar *horizontalScrollBar;
  324. ScrollBar *verticalScrollBar;
  325. Vec offset;
  326. ScrollWidget();
  327. void step();
  328. bool onScrollOpaque(Vec scrollRel);
  329. };
  330. struct ZoomWidget : Widget {
  331. float zoom = 1.0;
  332. void draw(NVGcontext *vg);
  333. Widget *onMouseDown(Vec pos, int button);
  334. Widget *onMouseUp(Vec pos, int button);
  335. Widget *onMouseMove(Vec pos, Vec mouseRel);
  336. Widget *onHoverKey(Vec pos, int key);
  337. Widget *onScroll(Vec pos, Vec scrollRel);
  338. };
  339. struct TextField : OpaqueWidget {
  340. std::string text;
  341. std::string placeholder;
  342. int begin = 0;
  343. int end = 0;
  344. TextField() {
  345. box.size.y = BND_WIDGET_HEIGHT;
  346. }
  347. void draw(NVGcontext *vg);
  348. Widget *onMouseDown(Vec pos, int button);
  349. bool onFocusText(int codepoint);
  350. bool onFocusKey(int scancode);
  351. void onFocus();
  352. void insertText(std::string newText);
  353. };
  354. struct PasswordField : TextField {
  355. void draw(NVGcontext *vg);
  356. };
  357. struct ProgressBar : TransparentWidget, QuantityWidget {
  358. ProgressBar() {
  359. box.size.y = BND_WIDGET_HEIGHT;
  360. }
  361. void draw(NVGcontext *vg);
  362. };
  363. struct Tooltip : Widget {
  364. void step();
  365. void draw(NVGcontext *vg);
  366. };
  367. struct Scene : OpaqueWidget {
  368. Widget *overlay = NULL;
  369. void setOverlay(Widget *w);
  370. Menu *createMenu();
  371. void step();
  372. };
  373. ////////////////////
  374. // globals
  375. ////////////////////
  376. extern Vec gMousePos;
  377. extern Widget *gHoveredWidget;
  378. extern Widget *gDraggedWidget;
  379. extern Widget *gDragHoveredWidget;
  380. extern Widget *gFocusedWidget;
  381. extern int gGuiFrame;
  382. extern Scene *gScene;
  383. } // namespace rack