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.

411 lines
9.6KB

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