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.

ui.hpp 6.0KB

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #pragma once
  2. #include "widgets.hpp"
  3. #include "blendish.h"
  4. #define CHECKMARK_STRING "✔"
  5. #define CHECKMARK(_cond) ((_cond) ? CHECKMARK_STRING : "")
  6. namespace rack {
  7. ////////////////////
  8. // Layouts (layouts.cpp)
  9. ////////////////////
  10. /** Positions children in a row/column based on their widths/heights */
  11. struct SequentialLayout : VirtualWidget {
  12. enum Orientation {
  13. HORIZONTAL_ORIENTATION,
  14. VERTICAL_ORIENTATION,
  15. };
  16. Orientation orientation = HORIZONTAL_ORIENTATION;
  17. enum Alignment {
  18. LEFT_ALIGNMENT,
  19. CENTER_ALIGNMENT,
  20. RIGHT_ALIGNMENT,
  21. };
  22. Alignment alignment = LEFT_ALIGNMENT;
  23. /** Space between adjacent elements */
  24. float spacing = 0.0;
  25. void step() override;
  26. };
  27. ////////////////////
  28. // Blendish UI elements
  29. ////////////////////
  30. struct Label : VirtualWidget {
  31. std::string text;
  32. float fontSize;
  33. NVGcolor color;
  34. enum Alignment {
  35. LEFT_ALIGNMENT,
  36. CENTER_ALIGNMENT,
  37. RIGHT_ALIGNMENT,
  38. };
  39. Alignment alignment = LEFT_ALIGNMENT;
  40. Label();
  41. void draw(NVGcontext *vg) override;
  42. };
  43. struct List : OpaqueWidget {
  44. void step() override;
  45. };
  46. /** Deletes itself from parent when clicked */
  47. struct MenuOverlay : OpaqueWidget {
  48. void step() override;
  49. void onMouseDown(EventMouseDown &e) override;
  50. void onHoverKey(EventHoverKey &e) override;
  51. };
  52. struct MenuEntry;
  53. struct Menu : OpaqueWidget {
  54. Menu *parentMenu = NULL;
  55. Menu *childMenu = NULL;
  56. /** The entry which created the child menu */
  57. MenuEntry *activeEntry = NULL;
  58. Menu() {
  59. box.size = Vec(0, 0);
  60. }
  61. ~Menu();
  62. /** Deprecated. Just use addChild(child) instead */
  63. void pushChild(Widget *child) DEPRECATED {
  64. addChild(child);
  65. }
  66. void setChildMenu(Menu *menu);
  67. void step() override;
  68. void draw(NVGcontext *vg) override;
  69. void onScroll(EventScroll &e) override;
  70. };
  71. struct MenuEntry : OpaqueWidget {
  72. MenuEntry() {
  73. box.size = Vec(0, BND_WIDGET_HEIGHT);
  74. }
  75. template <typename T = MenuEntry>
  76. static T *create() {
  77. T *o = Widget::create<T>(Vec());
  78. return o;
  79. }
  80. };
  81. struct MenuSeparator : MenuEntry {
  82. MenuSeparator();
  83. void draw(NVGcontext *vg) override;
  84. };
  85. struct MenuLabel : MenuEntry {
  86. std::string text;
  87. void draw(NVGcontext *vg) override;
  88. void step() override;
  89. template <typename T = MenuLabel>
  90. static T *create(std::string text) {
  91. T *o = MenuEntry::create<T>();
  92. o->text = text;
  93. return o;
  94. }
  95. };
  96. struct MenuItem : MenuEntry {
  97. std::string text;
  98. std::string rightText;
  99. void draw(NVGcontext *vg) override;
  100. void step() override;
  101. virtual Menu *createChildMenu() {return NULL;}
  102. void onMouseEnter(EventMouseEnter &e) override;
  103. void onDragDrop(EventDragDrop &e) override;
  104. template <typename T = MenuItem>
  105. static T *create(std::string text, std::string rightText = "") {
  106. T *o = MenuEntry::create<T>();
  107. o->text = text;
  108. o->rightText = rightText;
  109. return o;
  110. }
  111. };
  112. struct TooltipOverlay : TransparentWidget {
  113. };
  114. struct WindowOverlay : OpaqueWidget {
  115. };
  116. struct WindowWidget : OpaqueWidget {
  117. std::string title;
  118. void draw(NVGcontext *vg) override;
  119. void onDragMove(EventDragMove &e) override;
  120. };
  121. struct Button : OpaqueWidget {
  122. std::string text;
  123. BNDwidgetState state = BND_DEFAULT;
  124. Button() {
  125. box.size.y = BND_WIDGET_HEIGHT;
  126. }
  127. void draw(NVGcontext *vg) override;
  128. void onMouseEnter(EventMouseEnter &e) override;
  129. void onMouseLeave(EventMouseLeave &e) override;
  130. void onDragStart(EventDragStart &e) override;
  131. void onDragEnd(EventDragEnd &e) override;
  132. void onDragDrop(EventDragDrop &e) override;
  133. };
  134. struct IconButton : Button {
  135. SVGWidget *sw;
  136. IconButton();
  137. void setSVG(std::shared_ptr<SVG> svg);
  138. };
  139. struct ChoiceButton : Button {
  140. void draw(NVGcontext *vg) override;
  141. };
  142. struct RadioButton : OpaqueWidget, QuantityWidget {
  143. BNDwidgetState state = BND_DEFAULT;
  144. RadioButton() {
  145. box.size.y = BND_WIDGET_HEIGHT;
  146. }
  147. void draw(NVGcontext *vg) override;
  148. void onMouseEnter(EventMouseEnter &e) override;
  149. void onMouseLeave(EventMouseLeave &e) override;
  150. void onDragDrop(EventDragDrop &e) override;
  151. };
  152. struct Slider : OpaqueWidget, QuantityWidget {
  153. BNDwidgetState state = BND_DEFAULT;
  154. Slider() {
  155. box.size.y = BND_WIDGET_HEIGHT;
  156. }
  157. void draw(NVGcontext *vg) override;
  158. void onDragStart(EventDragStart &e) override;
  159. void onDragMove(EventDragMove &e) override;
  160. void onDragEnd(EventDragEnd &e) override;
  161. void onMouseDown(EventMouseDown &e) override;
  162. };
  163. struct ScrollBar;
  164. /** Handles a container with ScrollBar */
  165. struct ScrollWidget : OpaqueWidget {
  166. Widget *container;
  167. ScrollBar *horizontalScrollBar;
  168. ScrollBar *verticalScrollBar;
  169. Vec offset;
  170. ScrollWidget();
  171. void scrollTo(Rect r);
  172. void draw(NVGcontext *vg) override;
  173. void step() override;
  174. void onMouseMove(EventMouseMove &e) override;
  175. void onScroll(EventScroll &e) override;
  176. void onHoverKey(EventHoverKey &e) override;
  177. };
  178. struct TextField : OpaqueWidget {
  179. std::string text;
  180. std::string placeholder;
  181. bool multiline = false;
  182. /** The index of the text cursor */
  183. int cursor = 0;
  184. /** The index of the other end of the selection.
  185. If nothing is selected, this is equal to `cursor`.
  186. */
  187. int selection = 0;
  188. TextField() {
  189. box.size.y = BND_WIDGET_HEIGHT;
  190. }
  191. void draw(NVGcontext *vg) override;
  192. void onMouseDown(EventMouseDown &e) override;
  193. void onMouseMove(EventMouseMove &e) override;
  194. void onFocus(EventFocus &e) override;
  195. void onText(EventText &e) override;
  196. void onKey(EventKey &e) override;
  197. /** Inserts text at the cursor, replacing the selection if necessary */
  198. void insertText(std::string text);
  199. /** Replaces the entire text */
  200. void setText(std::string text);
  201. virtual int getTextPosition(Vec mousePos);
  202. virtual void onTextChange() {}
  203. };
  204. struct PasswordField : TextField {
  205. void draw(NVGcontext *vg) override;
  206. };
  207. struct ProgressBar : QuantityWidget {
  208. ProgressBar() {
  209. box.size.y = BND_WIDGET_HEIGHT;
  210. }
  211. void draw(NVGcontext *vg) override;
  212. };
  213. struct Tooltip : VirtualWidget {
  214. std::string text;
  215. Tooltip();
  216. void draw(NVGcontext *vg) override;
  217. };
  218. struct Scene : OpaqueWidget {
  219. Widget *overlay = NULL;
  220. /** Takes ownership of `w` */
  221. void setOverlay(Widget *w);
  222. Menu *createMenu();
  223. void step() override;
  224. };
  225. ////////////////////
  226. // globals
  227. ////////////////////
  228. extern Scene *gScene;
  229. } // namespace rack