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.

447 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 bool onFocus() {return false;}
  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) override;
  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) override {return NULL;}
  128. Widget *onMouseUp(Vec pos, int button) override {return NULL;}
  129. Widget *onMouseMove(Vec pos, Vec mouseRel) override {return NULL;}
  130. Widget *onScroll(Vec pos, Vec scrollRel) override {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) override {
  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) override {
  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) override {
  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) override {
  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) override;
  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) override;
  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() override.
  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() override */
  186. bool dirty = true;
  187. /** The root object in the framebuffer scene
  188. The FramebufferWidget owns the pointer
  189. */
  190. struct Internal;
  191. Internal *internal;
  192. FramebufferWidget();
  193. ~FramebufferWidget();
  194. void draw(NVGcontext *vg) override;
  195. int getImageHandle();
  196. };
  197. struct QuantityWidget : virtual Widget {
  198. float value = 0.0;
  199. float minValue = 0.0;
  200. float maxValue = 1.0;
  201. float defaultValue = 0.0;
  202. std::string label;
  203. /** Include a space character if you want a space after the number, e.g. " Hz" */
  204. std::string unit;
  205. /** The decimal place to round for displaying values.
  206. A precision of 2 will display as "1.00" for example.
  207. */
  208. int precision = 2;
  209. QuantityWidget();
  210. void setValue(float value);
  211. void setLimits(float minValue, float maxValue);
  212. void setDefaultValue(float defaultValue);
  213. /** Generates the display value */
  214. std::string getText();
  215. };
  216. ////////////////////
  217. // GUI widgets
  218. ////////////////////
  219. struct Label : Widget {
  220. std::string text;
  221. Label() {
  222. box.size.y = BND_WIDGET_HEIGHT;
  223. }
  224. void draw(NVGcontext *vg) override;
  225. };
  226. // Deletes itself from parent when clicked
  227. struct MenuOverlay : OpaqueWidget {
  228. void onDragDrop(Widget *origin) override;
  229. bool onScrollOpaque(Vec scrollRel) override {return true;}
  230. Widget *onHoverKey(Vec pos, int key) override;
  231. };
  232. struct MenuEntry;
  233. struct Menu : OpaqueWidget {
  234. Menu *parentMenu = NULL;
  235. Menu *childMenu = NULL;
  236. /** The entry which created the child menu */
  237. MenuEntry *activeEntry = NULL;
  238. Menu() {
  239. box.size = Vec(0, 0);
  240. }
  241. ~Menu();
  242. // Resizes menu and calls addChild()
  243. void pushChild(Widget *child);
  244. void setChildMenu(Menu *menu);
  245. void step() override;
  246. void draw(NVGcontext *vg) override;
  247. bool onScrollOpaque(Vec scrollRel) override;
  248. };
  249. struct MenuEntry : OpaqueWidget {
  250. std::string text;
  251. std::string rightText;
  252. MenuEntry() {
  253. box.size = Vec(0, BND_WIDGET_HEIGHT);
  254. }
  255. void step() override;
  256. };
  257. struct MenuLabel : MenuEntry {
  258. void draw(NVGcontext *vg) override;
  259. };
  260. struct MenuItem : MenuEntry {
  261. void draw(NVGcontext *vg) override;
  262. virtual Menu *createChildMenu() {return NULL;}
  263. void onMouseEnter() override;
  264. void onDragDrop(Widget *origin) override;
  265. };
  266. struct Button : OpaqueWidget {
  267. std::string text;
  268. BNDwidgetState state = BND_DEFAULT;
  269. Button() {
  270. box.size.y = BND_WIDGET_HEIGHT;
  271. }
  272. void draw(NVGcontext *vg) override;
  273. void onMouseEnter() override;
  274. void onMouseLeave() override;
  275. void onDragStart() override;
  276. void onDragEnd() override;
  277. void onDragDrop(Widget *origin) override;
  278. };
  279. struct ChoiceButton : Button {
  280. void draw(NVGcontext *vg) override;
  281. };
  282. struct RadioButton : OpaqueWidget, QuantityWidget {
  283. BNDwidgetState state = BND_DEFAULT;
  284. RadioButton() {
  285. box.size.y = BND_WIDGET_HEIGHT;
  286. }
  287. void draw(NVGcontext *vg) override;
  288. void onMouseEnter() override;
  289. void onMouseLeave() override;
  290. void onDragDrop(Widget *origin) override;
  291. };
  292. struct Slider : OpaqueWidget, QuantityWidget {
  293. BNDwidgetState state = BND_DEFAULT;
  294. Slider() {
  295. box.size.y = BND_WIDGET_HEIGHT;
  296. }
  297. void draw(NVGcontext *vg) override;
  298. void onDragStart() override;
  299. void onDragMove(Vec mouseRel) override;
  300. void onDragEnd() override;
  301. void onMouseDownOpaque(int button) override;
  302. };
  303. /** Parent must be a ScrollWidget */
  304. struct ScrollBar : OpaqueWidget {
  305. enum { VERTICAL, HORIZONTAL } orientation;
  306. BNDwidgetState state = BND_DEFAULT;
  307. ScrollBar() {
  308. box.size = Vec(BND_SCROLLBAR_WIDTH, BND_SCROLLBAR_HEIGHT);
  309. }
  310. void draw(NVGcontext *vg) override;
  311. void onDragStart() override;
  312. void onDragMove(Vec mouseRel) override;
  313. void onDragEnd() override;
  314. };
  315. /** Handles a container with ScrollBar */
  316. struct ScrollWidget : OpaqueWidget {
  317. Widget *container;
  318. ScrollBar *horizontalScrollBar;
  319. ScrollBar *verticalScrollBar;
  320. Vec offset;
  321. ScrollWidget();
  322. void step() override;
  323. bool onScrollOpaque(Vec scrollRel) override;
  324. };
  325. struct ZoomWidget : Widget {
  326. float zoom = 1.0;
  327. void draw(NVGcontext *vg) override;
  328. Widget *onMouseDown(Vec pos, int button) override;
  329. Widget *onMouseUp(Vec pos, int button) override;
  330. Widget *onMouseMove(Vec pos, Vec mouseRel) override;
  331. Widget *onHoverKey(Vec pos, int key) override;
  332. Widget *onScroll(Vec pos, Vec scrollRel) override;
  333. };
  334. struct TextField : OpaqueWidget {
  335. std::string text;
  336. std::string placeholder;
  337. bool multiline = false;
  338. int begin = 0;
  339. int end = 0;
  340. TextField() {
  341. box.size.y = BND_WIDGET_HEIGHT;
  342. }
  343. void draw(NVGcontext *vg) override;
  344. Widget *onMouseDown(Vec pos, int button) override;
  345. bool onFocusText(int codepoint) override;
  346. bool onFocusKey(int key) override;
  347. bool onFocus() override;
  348. void insertText(std::string newText);
  349. virtual void onTextChange() {}
  350. };
  351. struct PasswordField : TextField {
  352. void draw(NVGcontext *vg) override;
  353. };
  354. struct ProgressBar : TransparentWidget, QuantityWidget {
  355. ProgressBar() {
  356. box.size.y = BND_WIDGET_HEIGHT;
  357. }
  358. void draw(NVGcontext *vg) override;
  359. };
  360. struct Tooltip : Widget {
  361. void step() override;
  362. void draw(NVGcontext *vg) override;
  363. };
  364. struct Scene : OpaqueWidget {
  365. Widget *overlay = NULL;
  366. void setOverlay(Widget *w);
  367. Menu *createMenu();
  368. void step() override;
  369. };
  370. ////////////////////
  371. // globals
  372. ////////////////////
  373. extern Widget *gHoveredWidget;
  374. extern Widget *gDraggedWidget;
  375. extern Widget *gDragHoveredWidget;
  376. extern Widget *gFocusedWidget;
  377. extern Scene *gScene;
  378. } // namespace rack