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.

widgets.hpp 8.7KB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. virtual ~Widget();
  43. Vec getAbsolutePos();
  44. Rect getChildrenBoundingBox();
  45. template <class T>
  46. T *getAncestorOfType() {
  47. if (!parent) return NULL;
  48. T *p = dynamic_cast<T*>(parent);
  49. if (p) return p;
  50. return parent->getAncestorOfType<T>();
  51. }
  52. /** Adds widget to list of children.
  53. Gives ownership of widget to this widget instance.
  54. */
  55. void addChild(Widget *widget);
  56. /** Removes widget from list of children if it exists.
  57. Does not delete widget but transfers ownership to caller
  58. Silently fails if widget is not a child
  59. */
  60. void removeChild(Widget *widget);
  61. void clearChildren();
  62. /** Advances the module by one frame */
  63. virtual void step();
  64. /** Draws to NanoVG context */
  65. virtual void draw(NVGcontext *vg);
  66. // Events
  67. /** Called when a mouse button is pressed over this widget
  68. 0 for left, 1 for right, 2 for middle.
  69. Return `this` to accept the event.
  70. Return NULL to reject the event and pass it to the widget behind this one.
  71. */
  72. virtual Widget *onMouseDown(Vec pos, int button);
  73. virtual Widget *onMouseUp(Vec pos, int button);
  74. virtual Widget *onMouseMove(Vec pos, Vec mouseRel);
  75. /** Called when this widget begins responding to `onMouseMove` events */
  76. virtual void onMouseEnter() {}
  77. /** Called when another widget begins responding to `onMouseMove` events */
  78. virtual void onMouseLeave() {}
  79. virtual Widget *onScroll(Vec pos, Vec scrollRel);
  80. /** Called when a widget responds to `onMouseDown` for a left button press */
  81. virtual void onDragStart() {}
  82. /** Called when the left button is released and this widget is being dragged */
  83. virtual void onDragEnd() {}
  84. /** Called when a widget responds to `onMouseMove` and is being dragged */
  85. virtual void onDragMove(Vec mouseRel) {}
  86. /** Called when a widget responds to `onMouseUp` for a left button release and a widget is being dragged */
  87. virtual void onDragEnter(Widget *origin) {}
  88. virtual void onDragLeave(Widget *origin) {}
  89. virtual void onDragDrop(Widget *origin) {}
  90. virtual void onAction() {}
  91. virtual void onChange() {}
  92. };
  93. struct TransformWidget : Widget {
  94. /** The transformation matrix */
  95. float transform[6];
  96. TransformWidget();
  97. void reset();
  98. void translate(Vec delta);
  99. void rotate(float angle);
  100. void scale(Vec s);
  101. void draw(NVGcontext *vg);
  102. };
  103. ////////////////////
  104. // Trait widgets
  105. ////////////////////
  106. /** Widget that does not respond to events */
  107. struct TransparentWidget : virtual Widget {
  108. Widget *onMouseDown(Vec pos, int button) {return NULL;}
  109. Widget *onMouseUp(Vec pos, int button) {return NULL;}
  110. Widget *onMouseMove(Vec pos, Vec mouseRel) {return NULL;}
  111. Widget *onScroll(Vec pos, Vec scrollRel) {return NULL;}
  112. };
  113. /** Widget that itself responds to mouse events */
  114. struct OpaqueWidget : virtual Widget {
  115. Widget *onMouseDown(Vec pos, int button) {
  116. Widget *w = Widget::onMouseDown(pos, button);
  117. if (w) return w;
  118. onMouseDown(button);
  119. return this;
  120. }
  121. Widget *onMouseUp(Vec pos, int button) {
  122. Widget *w = Widget::onMouseUp(pos, button);
  123. if (w) return w;
  124. onMouseUp(button);
  125. return this;
  126. }
  127. Widget *onMouseMove(Vec pos, Vec mouseRel) {
  128. Widget *w = Widget::onMouseMove(pos, mouseRel);
  129. if (w) return w;
  130. onMouseMove(mouseRel);
  131. return this;
  132. }
  133. /** "High level" events called by the above lower level events.
  134. Use these if you don't care about the clicked position.
  135. */
  136. virtual void onMouseDown(int button) {}
  137. virtual void onMouseUp(int button) {}
  138. virtual void onMouseMove(Vec mouseRel) {}
  139. };
  140. struct SpriteWidget : virtual Widget {
  141. Vec spriteOffset;
  142. Vec spriteSize;
  143. std::shared_ptr<Image> spriteImage;
  144. int index = 0;
  145. void draw(NVGcontext *vg);
  146. };
  147. struct SVGWidget : virtual Widget {
  148. std::shared_ptr<SVG> svg;
  149. /** Sets the box size to the svg page */
  150. void step();
  151. void draw(NVGcontext *vg);
  152. };
  153. /** Caches a widget's draw() result to a framebuffer so it is called less frequently
  154. When `dirty` is true, `scene` will be re-rendered on the next call to step().
  155. Events are not passed to the underlying scene.
  156. */
  157. struct FramebufferWidget : virtual Widget {
  158. bool dirty = true;
  159. /** The root object in the framebuffer scene
  160. Its position is ignored for now, fixed at (0, 0)
  161. The FramebufferWidget owns the pointer
  162. */
  163. Widget *scene = NULL;
  164. struct Internal;
  165. Internal *internal;
  166. FramebufferWidget();
  167. ~FramebufferWidget();
  168. void step();
  169. void draw(NVGcontext *vg);
  170. };
  171. struct QuantityWidget : virtual Widget {
  172. float value = 0.0;
  173. float minValue = 0.0;
  174. float maxValue = 1.0;
  175. float defaultValue = 0.0;
  176. std::string label;
  177. /** Include a space character if you want a space after the number, e.g. " Hz" */
  178. std::string unit;
  179. /** The digit place to round for displaying values.
  180. A precision of -2 will display as "1.00" for example.
  181. */
  182. int precision = -2;
  183. QuantityWidget();
  184. void setValue(float value);
  185. void setLimits(float minValue, float maxValue);
  186. void setDefaultValue(float defaultValue);
  187. /** Generates the display value */
  188. std::string getText();
  189. };
  190. ////////////////////
  191. // GUI widgets
  192. ////////////////////
  193. struct Label : Widget {
  194. std::string text;
  195. Label() {
  196. box.size.y = BND_WIDGET_HEIGHT;
  197. }
  198. void draw(NVGcontext *vg);
  199. };
  200. // Deletes itself from parent when clicked
  201. struct MenuOverlay : OpaqueWidget {
  202. void step();
  203. Widget *onScroll(Vec pos, Vec scrollRel) {
  204. return this;
  205. }
  206. void onDragDrop(Widget *origin);
  207. };
  208. struct Menu : OpaqueWidget {
  209. Menu() {
  210. box.size = Vec(0, 0);
  211. }
  212. // Resizes menu and calls addChild()
  213. void pushChild(Widget *child);
  214. void draw(NVGcontext *vg);
  215. };
  216. struct MenuEntry : OpaqueWidget {
  217. std::string text;
  218. MenuEntry() {
  219. box.size = Vec(0, BND_WIDGET_HEIGHT);
  220. }
  221. float computeMinWidth(NVGcontext *vg);
  222. };
  223. struct MenuLabel : MenuEntry {
  224. void draw(NVGcontext *vg);
  225. };
  226. struct MenuItem : MenuEntry {
  227. BNDwidgetState state = BND_DEFAULT;
  228. void draw(NVGcontext *vg);
  229. void onMouseEnter();
  230. void onMouseLeave() ;
  231. void onDragDrop(Widget *origin);
  232. };
  233. struct Button : OpaqueWidget {
  234. std::string text;
  235. BNDwidgetState state = BND_DEFAULT;
  236. Button() {
  237. box.size.y = BND_WIDGET_HEIGHT;
  238. }
  239. void draw(NVGcontext *vg);
  240. void onMouseEnter();
  241. void onMouseLeave();
  242. void onDragStart();
  243. void onDragEnd();
  244. void onDragDrop(Widget *origin);
  245. };
  246. struct ChoiceButton : Button {
  247. void draw(NVGcontext *vg);
  248. };
  249. struct RadioButton : OpaqueWidget, QuantityWidget {
  250. BNDwidgetState state = BND_DEFAULT;
  251. RadioButton() {
  252. box.size.y = BND_WIDGET_HEIGHT;
  253. }
  254. void draw(NVGcontext *vg);
  255. void onMouseEnter();
  256. void onMouseLeave();
  257. void onDragDrop(Widget *origin);
  258. };
  259. struct Slider : OpaqueWidget, QuantityWidget {
  260. BNDwidgetState state = BND_DEFAULT;
  261. Slider() {
  262. box.size.y = BND_WIDGET_HEIGHT;
  263. }
  264. void draw(NVGcontext *vg);
  265. void onDragStart();
  266. void onDragMove(Vec mouseRel);
  267. void onDragEnd();
  268. };
  269. struct ScrollBar : OpaqueWidget {
  270. enum { VERTICAL, HORIZONTAL } orientation;
  271. float containerOffset = 0.0;
  272. float containerSize = 0.0;
  273. BNDwidgetState state = BND_DEFAULT;
  274. ScrollBar() {
  275. box.size = Vec(BND_SCROLLBAR_WIDTH, BND_SCROLLBAR_HEIGHT);
  276. }
  277. void draw(NVGcontext *vg);
  278. void move(float delta);
  279. void onDragStart();
  280. void onDragMove(Vec mouseRel);
  281. void onDragEnd();
  282. };
  283. // Handles a container with scrollbars
  284. struct ScrollWidget : OpaqueWidget {
  285. Widget *container;
  286. ScrollBar *hScrollBar;
  287. ScrollBar *vScrollBar;
  288. ScrollWidget();
  289. void step();
  290. void draw(NVGcontext *vg);
  291. Widget *onScroll(Vec pos, Vec scrollRel);
  292. };
  293. struct Tooltip : Widget {
  294. void step();
  295. void draw(NVGcontext *vg);
  296. };
  297. struct Scene : OpaqueWidget {
  298. Widget *overlay = NULL;
  299. void setOverlay(Widget *w);
  300. void step();
  301. };
  302. ////////////////////
  303. // globals
  304. ////////////////////
  305. extern Vec gMousePos;
  306. extern Widget *gHoveredWidget;
  307. extern Widget *gDraggedWidget;
  308. extern Widget *gDragHoveredWidget;
  309. extern Widget *gSelectedWidget;
  310. extern int gGuiFrame;
  311. extern Scene *gScene;
  312. } // namespace rack