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.

221 lines
5.2KB

  1. #pragma once
  2. #include "widgets/OpaqueWidget.hpp"
  3. #include "ui/common.hpp"
  4. #include "event.hpp"
  5. namespace rack {
  6. struct TextField : OpaqueWidget {
  7. std::string text;
  8. std::string placeholder;
  9. bool multiline = false;
  10. /** The index of the text cursor */
  11. int cursor = 0;
  12. /** The index of the other end of the selection.
  13. If nothing is selected, this is equal to `cursor`.
  14. */
  15. int selection = 0;
  16. TextField() {
  17. box.size.y = BND_WIDGET_HEIGHT;
  18. }
  19. void draw(NVGcontext *vg) override {
  20. nvgScissor(vg, 0, 0, box.size.x, box.size.y);
  21. BNDwidgetState state;
  22. if (this == event::gContext->selectedWidget)
  23. state = BND_ACTIVE;
  24. else if (this == event::gContext->hoveredWidget)
  25. state = BND_HOVER;
  26. else
  27. state = BND_DEFAULT;
  28. int begin = std::min(cursor, selection);
  29. int end = std::max(cursor, selection);
  30. bndTextField(vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE, state, -1, text.c_str(), begin, end);
  31. // Draw placeholder text
  32. if (text.empty() && state != BND_ACTIVE) {
  33. bndIconLabelCaret(vg, 0.0, 0.0, box.size.x, box.size.y, -1, bndGetTheme()->textFieldTheme.itemColor, 13, placeholder.c_str(), bndGetTheme()->textFieldTheme.itemColor, 0, -1);
  34. }
  35. nvgResetScissor(vg);
  36. }
  37. void onButton(event::Button &e) override {
  38. if (e.action == GLFW_PRESS && e.button == GLFW_MOUSE_BUTTON_LEFT) {
  39. cursor = selection = getTextPosition(e.pos);
  40. }
  41. OpaqueWidget::onButton(e);
  42. }
  43. void onHover(event::Hover &e) override {
  44. if (this == event::gContext->draggedWidget) {
  45. int pos = getTextPosition(e.pos);
  46. if (pos != selection) {
  47. cursor = pos;
  48. }
  49. }
  50. OpaqueWidget::onHover(e);
  51. }
  52. void onEnter(event::Enter &e) override {
  53. e.target = this;
  54. }
  55. void onSelectText(event::SelectText &e) override {
  56. if (e.codepoint < 128) {
  57. std::string newText(1, (char) e.codepoint);
  58. insertText(newText);
  59. }
  60. e.target = this;
  61. }
  62. void onSelectKey(event::SelectKey &e) override {
  63. switch (e.key) {
  64. case GLFW_KEY_BACKSPACE: {
  65. if (cursor == selection) {
  66. cursor--;
  67. if (cursor >= 0) {
  68. text.erase(cursor, 1);
  69. event::Change eChange;
  70. onChange(eChange);
  71. }
  72. selection = cursor;
  73. }
  74. else {
  75. int begin = std::min(cursor, selection);
  76. text.erase(begin, std::abs(selection - cursor));
  77. event::Change eChange;
  78. onChange(eChange);
  79. cursor = selection = begin;
  80. }
  81. } break;
  82. case GLFW_KEY_DELETE: {
  83. if (cursor == selection) {
  84. text.erase(cursor, 1);
  85. event::Change eChange;
  86. onChange(eChange);
  87. }
  88. else {
  89. int begin = std::min(cursor, selection);
  90. text.erase(begin, std::abs(selection - cursor));
  91. event::Change eChange;
  92. onChange(eChange);
  93. cursor = selection = begin;
  94. }
  95. } break;
  96. case GLFW_KEY_LEFT: {
  97. if (windowIsModPressed()) {
  98. while (--cursor > 0) {
  99. if (text[cursor] == ' ')
  100. break;
  101. }
  102. }
  103. else {
  104. cursor--;
  105. }
  106. if (!windowIsShiftPressed()) {
  107. selection = cursor;
  108. }
  109. } break;
  110. case GLFW_KEY_RIGHT: {
  111. if (windowIsModPressed()) {
  112. while (++cursor < (int) text.size()) {
  113. if (text[cursor] == ' ')
  114. break;
  115. }
  116. }
  117. else {
  118. cursor++;
  119. }
  120. if (!windowIsShiftPressed()) {
  121. selection = cursor;
  122. }
  123. } break;
  124. case GLFW_KEY_HOME: {
  125. selection = cursor = 0;
  126. } break;
  127. case GLFW_KEY_END: {
  128. selection = cursor = text.size();
  129. } break;
  130. case GLFW_KEY_V: {
  131. if (windowIsModPressed()) {
  132. const char *newText = glfwGetClipboardString(gWindow);
  133. if (newText)
  134. insertText(newText);
  135. }
  136. } break;
  137. case GLFW_KEY_X: {
  138. if (windowIsModPressed()) {
  139. if (cursor != selection) {
  140. int begin = std::min(cursor, selection);
  141. std::string selectedText = text.substr(begin, std::abs(selection - cursor));
  142. glfwSetClipboardString(gWindow, selectedText.c_str());
  143. insertText("");
  144. }
  145. }
  146. } break;
  147. case GLFW_KEY_C: {
  148. if (windowIsModPressed()) {
  149. if (cursor != selection) {
  150. int begin = std::min(cursor, selection);
  151. std::string selectedText = text.substr(begin, std::abs(selection - cursor));
  152. glfwSetClipboardString(gWindow, selectedText.c_str());
  153. }
  154. }
  155. } break;
  156. case GLFW_KEY_A: {
  157. if (windowIsModPressed()) {
  158. selection = 0;
  159. cursor = text.size();
  160. }
  161. } break;
  162. case GLFW_KEY_ENTER: {
  163. if (multiline) {
  164. insertText("\n");
  165. }
  166. else {
  167. event::Action eAction;
  168. onAction(eAction);
  169. }
  170. } break;
  171. }
  172. cursor = clamp(cursor, 0, (int) text.size());
  173. selection = clamp(selection, 0, (int) text.size());
  174. e.target = this;
  175. }
  176. /** Inserts text at the cursor, replacing the selection if necessary */
  177. void insertText(std::string text) {
  178. if (cursor != selection) {
  179. int begin = std::min(cursor, selection);
  180. this->text.erase(begin, std::abs(selection - cursor));
  181. cursor = selection = begin;
  182. }
  183. this->text.insert(cursor, text);
  184. cursor += text.size();
  185. selection = cursor;
  186. event::Change eChange;
  187. onChange(eChange);
  188. }
  189. /** Replaces the entire text */
  190. void setText(std::string text) {
  191. this->text = text;
  192. selection = cursor = text.size();
  193. event::Change eChange;
  194. onChange(eChange);
  195. }
  196. virtual int getTextPosition(Vec mousePos) {
  197. return bndTextFieldTextPosition(gVg, 0.0, 0.0, box.size.x, box.size.y, -1, text.c_str(), mousePos.x, mousePos.y);
  198. }
  199. };
  200. } // namespace rack