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 5.6KB

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