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.

269 lines
5.9KB

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