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.

258 lines
5.7KB

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