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.

414 lines
9.7KB

  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. /** Adds widget to list of children.
  54. Gives ownership of widget to this widget instance.
  55. */
  56. void addChild(Widget *widget);
  57. /** Removes widget from list of children if it exists.
  58. Does not delete widget but transfers ownership to caller
  59. Silently fails if widget is not a child
  60. */
  61. void removeChild(Widget *widget);
  62. void clearChildren();
  63. /** Advances the module by one frame */
  64. virtual void step();
  65. /** Draws to NanoVG context */
  66. virtual void draw(NVGcontext *vg);
  67. // Events
  68. /** Called when a mouse button is pressed over this widget
  69. 0 for left, 1 for right, 2 for middle.
  70. Return `this` to accept the event.
  71. Return NULL to reject the event and pass it to the widget behind this one.
  72. */
  73. virtual Widget *onMouseDown(Vec pos, int button);
  74. virtual Widget *onMouseUp(Vec pos, int button);
  75. /** Called on every frame, even if mouseRel = Vec(0, 0) */
  76. virtual Widget *onMouseMove(Vec pos, Vec mouseRel);
  77. virtual Widget *onHoverKey(Vec pos, int key);
  78. /** Called when this widget begins responding to `onMouseMove` events */
  79. virtual void onMouseEnter() {}
  80. /** Called when another widget begins responding to `onMouseMove` events */
  81. virtual void onMouseLeave() {}
  82. virtual void onSelect() {}
  83. virtual void onDeselect() {}
  84. virtual bool onText(int codepoint) {return false;}
  85. virtual bool onKey(int key) {return false;}
  86. virtual Widget *onScroll(Vec pos, Vec scrollRel);
  87. /** Called when a widget responds to `onMouseDown` for a left button press */
  88. virtual void onDragStart() {}
  89. /** Called when the left button is released and this widget is being dragged */
  90. virtual void onDragEnd() {}
  91. /** Called when a widget responds to `onMouseMove` and is being dragged */
  92. virtual void onDragMove(Vec mouseRel) {}
  93. /** Called when a widget responds to `onMouseUp` for a left button release and a widget is being dragged */
  94. virtual void onDragEnter(Widget *origin) {}
  95. virtual void onDragLeave(Widget *origin) {}
  96. virtual void onDragDrop(Widget *origin) {}
  97. virtual void onAction() {}
  98. virtual void onChange() {}
  99. };
  100. struct TransformWidget : Widget {
  101. /** The transformation matrix */
  102. float transform[6];
  103. TransformWidget();
  104. void identity();
  105. void translate(Vec delta);
  106. void rotate(float angle);
  107. void scale(Vec s);
  108. void draw(NVGcontext *vg);
  109. };
  110. ////////////////////
  111. // Trait widgets
  112. ////////////////////
  113. /** Widget that does not respond to events */
  114. struct TransparentWidget : virtual Widget {
  115. Widget *onMouseDown(Vec pos, int button) {return NULL;}
  116. Widget *onMouseUp(Vec pos, int button) {return NULL;}
  117. Widget *onMouseMove(Vec pos, Vec mouseRel) {return NULL;}
  118. Widget *onScroll(Vec pos, Vec scrollRel) {return NULL;}
  119. };
  120. /** Widget that itself responds to mouse events */
  121. struct OpaqueWidget : virtual Widget {
  122. Widget *onMouseDown(Vec pos, int button) {
  123. Widget *w = Widget::onMouseDown(pos, button);
  124. if (w) return w;
  125. onMouseDown(button);
  126. return this;
  127. }
  128. Widget *onMouseUp(Vec pos, int button) {
  129. Widget *w = Widget::onMouseUp(pos, button);
  130. if (w) return w;
  131. onMouseUp(button);
  132. return this;
  133. }
  134. Widget *onMouseMove(Vec pos, Vec mouseRel) {
  135. Widget *w = Widget::onMouseMove(pos, mouseRel);
  136. if (w) return w;
  137. onMouseMove(mouseRel);
  138. return this;
  139. }
  140. /** "High level" events called by the above lower level events.
  141. Use these if you don't care about the clicked position.
  142. */
  143. virtual void onMouseDown(int button) {}
  144. virtual void onMouseUp(int button) {}
  145. virtual void onMouseMove(Vec mouseRel) {}
  146. };
  147. struct SpriteWidget : virtual Widget {
  148. Vec spriteOffset;
  149. Vec spriteSize;
  150. std::shared_ptr<Image> spriteImage;
  151. int index = 0;
  152. void draw(NVGcontext *vg);
  153. };
  154. struct SVGWidget : virtual Widget {
  155. std::shared_ptr<SVG> svg;
  156. /** Sets the box size to the svg image size */
  157. void wrap();
  158. void draw(NVGcontext *vg);
  159. };
  160. /** Caches a widget's draw() result to a framebuffer so it is called less frequently
  161. When `dirty` is true, its children will be re-rendered on the next call to step().
  162. Events are not passed to the underlying scene.
  163. */
  164. struct FramebufferWidget : virtual Widget {
  165. /** Set this to true to re-render the children to the framebuffer in the next step() */
  166. bool dirty = true;
  167. /** A margin in pixels around the children in the framebuffer
  168. This prevents cutting the rendered SVG off on the box edges.
  169. */
  170. Vec padding;
  171. /** The root object in the framebuffer scene
  172. The FramebufferWidget owns the pointer
  173. */
  174. struct Internal;
  175. Internal *internal;
  176. FramebufferWidget();
  177. ~FramebufferWidget();
  178. void step();
  179. void draw(NVGcontext *vg);
  180. int getImageHandle();
  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 decimal 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. std::string placeholder;
  307. int begin = 0;
  308. int end = 0;
  309. TextField() {
  310. box.size.y = BND_WIDGET_HEIGHT;
  311. }
  312. void draw(NVGcontext *vg);
  313. Widget *onMouseDown(Vec pos, int button);
  314. bool onText(int codepoint);
  315. bool onKey(int scancode);
  316. void onSelect();
  317. void insertText(std::string newText);
  318. };
  319. struct PasswordField : TextField {
  320. void draw(NVGcontext *vg);
  321. };
  322. struct ProgressBar : TransparentWidget, QuantityWidget {
  323. ProgressBar() {
  324. box.size.y = BND_WIDGET_HEIGHT;
  325. }
  326. void draw(NVGcontext *vg);
  327. };
  328. struct Tooltip : Widget {
  329. void step();
  330. void draw(NVGcontext *vg);
  331. };
  332. struct Scene : OpaqueWidget {
  333. Widget *overlay = NULL;
  334. void setOverlay(Widget *w);
  335. Menu *createMenu();
  336. void step();
  337. };
  338. ////////////////////
  339. // globals
  340. ////////////////////
  341. extern Vec gMousePos;
  342. extern Widget *gHoveredWidget;
  343. extern Widget *gDraggedWidget;
  344. extern Widget *gDragHoveredWidget;
  345. extern Widget *gSelectedWidget;
  346. extern int gGuiFrame;
  347. extern Scene *gScene;
  348. } // namespace rack