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.

413 lines
9.6KB

  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. 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, `scene` 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 scene to the framebuffer in the next step() */
  164. bool dirty = true;
  165. /** A margin in pixels around the scene in the framebuffer
  166. This prevents cutting the rendered SVG off on the box edges.
  167. */
  168. int margin = 0;
  169. /** The root object in the framebuffer scene
  170. The FramebufferWidget owns the pointer
  171. */
  172. Widget *scene = NULL;
  173. struct Internal;
  174. Internal *internal;
  175. FramebufferWidget();
  176. ~FramebufferWidget();
  177. void setScene(Widget *w) {
  178. scene = w;
  179. }
  180. void step();
  181. void draw(NVGcontext *vg);
  182. };
  183. struct QuantityWidget : virtual Widget {
  184. float value = 0.0;
  185. float minValue = 0.0;
  186. float maxValue = 1.0;
  187. float defaultValue = 0.0;
  188. std::string label;
  189. /** Include a space character if you want a space after the number, e.g. " Hz" */
  190. std::string unit;
  191. /** The decimal place to round for displaying values.
  192. A precision of 2 will display as "1.00" for example.
  193. */
  194. int precision = 2;
  195. QuantityWidget();
  196. void setValue(float value);
  197. void setLimits(float minValue, float maxValue);
  198. void setDefaultValue(float defaultValue);
  199. /** Generates the display value */
  200. std::string getText();
  201. };
  202. ////////////////////
  203. // GUI widgets
  204. ////////////////////
  205. struct Label : Widget {
  206. std::string text;
  207. Label() {
  208. box.size.y = BND_WIDGET_HEIGHT;
  209. }
  210. void draw(NVGcontext *vg);
  211. };
  212. // Deletes itself from parent when clicked
  213. struct MenuOverlay : OpaqueWidget {
  214. void step();
  215. Widget *onScroll(Vec pos, Vec scrollRel) {
  216. return this;
  217. }
  218. void onDragDrop(Widget *origin);
  219. };
  220. struct Menu : OpaqueWidget {
  221. Menu() {
  222. box.size = Vec(0, 0);
  223. }
  224. // Resizes menu and calls addChild()
  225. void pushChild(Widget *child);
  226. void draw(NVGcontext *vg);
  227. };
  228. struct MenuEntry : OpaqueWidget {
  229. std::string text;
  230. MenuEntry() {
  231. box.size = Vec(0, BND_WIDGET_HEIGHT);
  232. }
  233. float computeMinWidth(NVGcontext *vg);
  234. };
  235. struct MenuLabel : MenuEntry {
  236. void draw(NVGcontext *vg);
  237. };
  238. struct MenuItem : MenuEntry {
  239. BNDwidgetState state = BND_DEFAULT;
  240. void draw(NVGcontext *vg);
  241. void onMouseEnter();
  242. void onMouseLeave() ;
  243. void onDragDrop(Widget *origin);
  244. };
  245. struct Button : OpaqueWidget {
  246. std::string text;
  247. BNDwidgetState state = BND_DEFAULT;
  248. Button() {
  249. box.size.y = BND_WIDGET_HEIGHT;
  250. }
  251. void draw(NVGcontext *vg);
  252. void onMouseEnter();
  253. void onMouseLeave();
  254. void onDragStart();
  255. void onDragEnd();
  256. void onDragDrop(Widget *origin);
  257. };
  258. struct ChoiceButton : Button {
  259. void draw(NVGcontext *vg);
  260. };
  261. struct RadioButton : OpaqueWidget, QuantityWidget {
  262. BNDwidgetState state = BND_DEFAULT;
  263. RadioButton() {
  264. box.size.y = BND_WIDGET_HEIGHT;
  265. }
  266. void draw(NVGcontext *vg);
  267. void onMouseEnter();
  268. void onMouseLeave();
  269. void onDragDrop(Widget *origin);
  270. };
  271. struct Slider : OpaqueWidget, QuantityWidget {
  272. BNDwidgetState state = BND_DEFAULT;
  273. Slider() {
  274. box.size.y = BND_WIDGET_HEIGHT;
  275. }
  276. void draw(NVGcontext *vg);
  277. void onDragStart();
  278. void onDragMove(Vec mouseRel);
  279. void onDragEnd();
  280. };
  281. struct ScrollBar : OpaqueWidget {
  282. enum { VERTICAL, HORIZONTAL } orientation;
  283. float containerOffset = 0.0;
  284. float containerSize = 0.0;
  285. BNDwidgetState state = BND_DEFAULT;
  286. ScrollBar() {
  287. box.size = Vec(BND_SCROLLBAR_WIDTH, BND_SCROLLBAR_HEIGHT);
  288. }
  289. void draw(NVGcontext *vg);
  290. void move(float delta);
  291. void onDragStart();
  292. void onDragMove(Vec mouseRel);
  293. void onDragEnd();
  294. };
  295. // Handles a container with scrollbars
  296. struct ScrollWidget : OpaqueWidget {
  297. Widget *container;
  298. ScrollBar *hScrollBar;
  299. ScrollBar *vScrollBar;
  300. ScrollWidget();
  301. void step();
  302. void draw(NVGcontext *vg);
  303. Widget *onScroll(Vec pos, Vec scrollRel);
  304. };
  305. struct TextField : OpaqueWidget {
  306. std::string text;
  307. std::string placeholder;
  308. int begin = 0;
  309. int end = 0;
  310. TextField() {
  311. box.size.y = BND_WIDGET_HEIGHT;
  312. }
  313. void draw(NVGcontext *vg);
  314. Widget *onMouseDown(Vec pos, int button);
  315. void onText(int codepoint);
  316. void onKey(int scancode);
  317. void onSelect();
  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. void step();
  336. };
  337. ////////////////////
  338. // globals
  339. ////////////////////
  340. extern Vec gMousePos;
  341. extern Widget *gHoveredWidget;
  342. extern Widget *gDraggedWidget;
  343. extern Widget *gDragHoveredWidget;
  344. extern Widget *gSelectedWidget;
  345. extern int gGuiFrame;
  346. extern Scene *gScene;
  347. } // namespace rack