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.

275 lines
6.0KB

  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. DEPRECATED void pushChild(Widget *child) {
  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. FramebufferWidget *fw;
  136. SVGWidget *sw;
  137. IconButton();
  138. void setSVG(std::shared_ptr<SVG> svg);
  139. };
  140. struct ChoiceButton : Button {
  141. void draw(NVGcontext *vg) override;
  142. };
  143. struct RadioButton : OpaqueWidget, QuantityWidget {
  144. BNDwidgetState state = BND_DEFAULT;
  145. RadioButton() {
  146. box.size.y = BND_WIDGET_HEIGHT;
  147. }
  148. void draw(NVGcontext *vg) override;
  149. void onMouseEnter(EventMouseEnter &e) override;
  150. void onMouseLeave(EventMouseLeave &e) override;
  151. void onDragDrop(EventDragDrop &e) override;
  152. };
  153. struct Slider : OpaqueWidget, QuantityWidget {
  154. BNDwidgetState state = BND_DEFAULT;
  155. Slider() {
  156. box.size.y = BND_WIDGET_HEIGHT;
  157. }
  158. void draw(NVGcontext *vg) override;
  159. void onDragStart(EventDragStart &e) override;
  160. void onDragMove(EventDragMove &e) override;
  161. void onDragEnd(EventDragEnd &e) override;
  162. void onMouseDown(EventMouseDown &e) override;
  163. };
  164. struct ScrollBar;
  165. /** Handles a container with ScrollBar */
  166. struct ScrollWidget : OpaqueWidget {
  167. Widget *container;
  168. ScrollBar *horizontalScrollBar;
  169. ScrollBar *verticalScrollBar;
  170. Vec offset;
  171. ScrollWidget();
  172. void scrollTo(Rect r);
  173. void draw(NVGcontext *vg) override;
  174. void step() override;
  175. void onMouseMove(EventMouseMove &e) override;
  176. void onScroll(EventScroll &e) override;
  177. void onHoverKey(EventHoverKey &e) override;
  178. };
  179. struct TextField : OpaqueWidget {
  180. std::string text;
  181. std::string placeholder;
  182. bool multiline = false;
  183. /** The index of the text cursor */
  184. int cursor = 0;
  185. /** The index of the other end of the selection.
  186. If nothing is selected, this is equal to `cursor`.
  187. */
  188. int selection = 0;
  189. TextField() {
  190. box.size.y = BND_WIDGET_HEIGHT;
  191. }
  192. void draw(NVGcontext *vg) override;
  193. void onMouseDown(EventMouseDown &e) override;
  194. void onMouseMove(EventMouseMove &e) override;
  195. void onFocus(EventFocus &e) override;
  196. void onText(EventText &e) override;
  197. void onKey(EventKey &e) override;
  198. /** Inserts text at the cursor, replacing the selection if necessary */
  199. void insertText(std::string text);
  200. /** Replaces the entire text */
  201. void setText(std::string text);
  202. virtual int getTextPosition(Vec mousePos);
  203. virtual void onTextChange() {}
  204. };
  205. struct PasswordField : TextField {
  206. void draw(NVGcontext *vg) override;
  207. };
  208. struct ProgressBar : QuantityWidget {
  209. ProgressBar() {
  210. box.size.y = BND_WIDGET_HEIGHT;
  211. }
  212. void draw(NVGcontext *vg) override;
  213. };
  214. struct Tooltip : VirtualWidget {
  215. std::string text;
  216. Tooltip();
  217. void draw(NVGcontext *vg) override;
  218. };
  219. struct Scene : OpaqueWidget {
  220. Widget *overlay = NULL;
  221. /** Takes ownership of `w` */
  222. void setOverlay(Widget *w);
  223. Menu *createMenu();
  224. void step() override;
  225. };
  226. ////////////////////
  227. // globals
  228. ////////////////////
  229. extern Scene *gScene;
  230. } // namespace rack