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.

418 lines
10.0KB

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