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.

416 lines
9.8KB

  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. std::string rightText;
  230. MenuEntry() {
  231. box.size = Vec(0, BND_WIDGET_HEIGHT);
  232. }
  233. virtual 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. float computeMinWidth(NVGcontext *vg);
  241. void draw(NVGcontext *vg);
  242. void onMouseEnter();
  243. void onMouseLeave() ;
  244. void onDragDrop(Widget *origin);
  245. };
  246. struct Button : OpaqueWidget {
  247. std::string text;
  248. BNDwidgetState state = BND_DEFAULT;
  249. Button() {
  250. box.size.y = BND_WIDGET_HEIGHT;
  251. }
  252. void draw(NVGcontext *vg);
  253. void onMouseEnter();
  254. void onMouseLeave();
  255. void onDragStart();
  256. void onDragEnd();
  257. void onDragDrop(Widget *origin);
  258. };
  259. struct ChoiceButton : Button {
  260. void draw(NVGcontext *vg);
  261. };
  262. struct RadioButton : OpaqueWidget, QuantityWidget {
  263. BNDwidgetState state = BND_DEFAULT;
  264. RadioButton() {
  265. box.size.y = BND_WIDGET_HEIGHT;
  266. }
  267. void draw(NVGcontext *vg);
  268. void onMouseEnter();
  269. void onMouseLeave();
  270. void onDragDrop(Widget *origin);
  271. };
  272. struct Slider : OpaqueWidget, QuantityWidget {
  273. BNDwidgetState state = BND_DEFAULT;
  274. Slider() {
  275. box.size.y = BND_WIDGET_HEIGHT;
  276. }
  277. void draw(NVGcontext *vg);
  278. void onDragStart();
  279. void onDragMove(Vec mouseRel);
  280. void onDragEnd();
  281. };
  282. struct ScrollBar : OpaqueWidget {
  283. enum { VERTICAL, HORIZONTAL } orientation;
  284. float containerOffset = 0.0;
  285. float containerSize = 0.0;
  286. BNDwidgetState state = BND_DEFAULT;
  287. ScrollBar() {
  288. box.size = Vec(BND_SCROLLBAR_WIDTH, BND_SCROLLBAR_HEIGHT);
  289. }
  290. void draw(NVGcontext *vg);
  291. void move(float delta);
  292. void onDragStart();
  293. void onDragMove(Vec mouseRel);
  294. void onDragEnd();
  295. };
  296. // Handles a container with scrollbars
  297. struct ScrollWidget : OpaqueWidget {
  298. Widget *container;
  299. ScrollBar *hScrollBar;
  300. ScrollBar *vScrollBar;
  301. ScrollWidget();
  302. void step();
  303. void draw(NVGcontext *vg);
  304. Widget *onScroll(Vec pos, Vec scrollRel);
  305. };
  306. struct TextField : OpaqueWidget {
  307. std::string text;
  308. std::string placeholder;
  309. int begin = 0;
  310. int end = 0;
  311. TextField() {
  312. box.size.y = BND_WIDGET_HEIGHT;
  313. }
  314. void draw(NVGcontext *vg);
  315. Widget *onMouseDown(Vec pos, int button);
  316. bool onText(int codepoint);
  317. bool onKey(int scancode);
  318. void onSelect();
  319. void insertText(std::string newText);
  320. };
  321. struct PasswordField : TextField {
  322. void draw(NVGcontext *vg);
  323. };
  324. struct ProgressBar : TransparentWidget, QuantityWidget {
  325. ProgressBar() {
  326. box.size.y = BND_WIDGET_HEIGHT;
  327. }
  328. void draw(NVGcontext *vg);
  329. };
  330. struct Tooltip : Widget {
  331. void step();
  332. void draw(NVGcontext *vg);
  333. };
  334. struct Scene : OpaqueWidget {
  335. Widget *overlay = NULL;
  336. void setOverlay(Widget *w);
  337. Menu *createMenu();
  338. void step();
  339. };
  340. ////////////////////
  341. // globals
  342. ////////////////////
  343. extern Vec gMousePos;
  344. extern Widget *gHoveredWidget;
  345. extern Widget *gDraggedWidget;
  346. extern Widget *gDragHoveredWidget;
  347. extern Widget *gSelectedWidget;
  348. extern int gGuiFrame;
  349. extern Scene *gScene;
  350. } // namespace rack