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.

263 lines
5.8KB

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