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.

218 lines
5.9KB

  1. #ifndef TSOSCCONFIGWIDGET_HPP
  2. #define TSOSCCONFIGWIDGET_HPP
  3. #include "rack.hpp"
  4. using namespace rack;
  5. #ifdef USE_VST2
  6. #define plugin "trowaSoft"
  7. #endif // USE_VST2
  8. #include "componentlibrary.hpp"
  9. #include "widgets.hpp"
  10. #include "dsp/digital.hpp"
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "TSTextField.hpp"
  14. #include "trowaSoftComponents.hpp"
  15. #include "TSOSCCommon.hpp"
  16. //#include "TSSequencerModuleBase.hpp"
  17. #define TSOSC_NUM_TXTFIELDS 3
  18. #define TSOSC_STATUS_COLOR nvgRGB(0x00, 0xff, 0xff)
  19. // Button to chose OSC client from drop down.
  20. struct TSOSCClientSelectBtn : ChoiceButton {
  21. // The selected value.
  22. OSCClient selectedOSCClient;
  23. bool visible = false;
  24. std::shared_ptr<Font> font;
  25. Vec textOffset;
  26. NVGcolor color;
  27. // Font size
  28. float fontSize;
  29. std::string displayStr;
  30. int borderWidth = 0;
  31. NVGcolor borderColor;
  32. NVGcolor backgroundColor;
  33. TSOSCClientSelectBtn();
  34. void step() override;
  35. void onAction(EventAction &e) override;
  36. // Draw if visible
  37. void draw(NVGcontext *vg) override;
  38. };
  39. // An OSC client option in dropdown.
  40. struct TSOSCClientItem : MenuItem {
  41. OSCClient oscClient;
  42. TSOSCClientSelectBtn* parentButton;
  43. TSOSCClientItem(TSOSCClientSelectBtn* parent)
  44. {
  45. parentButton = parent;
  46. return;
  47. }
  48. void onAction(EventAction &e) override;
  49. };
  50. struct TSOSCConfigWidget : OpaqueWidget
  51. {
  52. // For looping, array of txt boxes
  53. TSTextField* textBoxes[4];
  54. TSTextField* tbIpAddress;
  55. TSTextField* tbTxPort;
  56. TSTextField* tbRxPort;
  57. // Text field for the name space.
  58. TSTextField* tbNamespace;
  59. // Client select/drop down.
  60. TSOSCClientSelectBtn* btnClientSelect;
  61. // Reference to module.
  62. Module* module;
  63. // Save (Enable/Disable) button
  64. TS_PadBtn* btnSave;
  65. // If the btn should be Enable (true) or Disable (false).
  66. bool btnActionEnable = true;
  67. // Auto-reconnect toggle (auto-reconnect on loading).
  68. TS_ScreenCheckBox* ckAutoReconnect;
  69. SchmittTrigger autoReconnectTrigger;
  70. std::string errorMsg;
  71. std::string successMsg;
  72. // The current status
  73. std::string statusMsg;
  74. // The current status (on the bottom).
  75. std::string statusMsg2;
  76. bool visible;
  77. std::shared_ptr<Font> font;
  78. float fontSize = 12;
  79. NVGcolor backgroundColor = nvgRGB(0x20, 0x20, 0x20);
  80. NVGcolor borderColor = nvgRGB(0x10, 0x10, 0x10);
  81. NVGcolor textColor = nvgRGB(0xee, 0xee, 0xee);
  82. NVGcolor errorColor = nvgRGB(0xee, 0x00, 0x00);
  83. NVGcolor successColor = nvgRGB(0x00, 0xee, 0x00);
  84. NVGcolor statusColor = TSOSC_STATUS_COLOR;
  85. bool showClientSelect = true;
  86. bool showNamespace = false;
  87. int xNamespace = -1;
  88. int numTextFields = 3;
  89. // Callback/event for when a VALID form is submitted.
  90. void(*formSubmitted)();
  91. TSOSCConfigWidget(Module* mod, int btnSaveId, int btnAutoReconnectId, OSCClient selectedClient);
  92. TSOSCConfigWidget(Module* mod, int btnSaveId, int btnAutoReconnectId, OSCClient selectedClient, std::string ipAddress, uint16_t txPort, uint16_t rxPort);
  93. //------------------------------------------------------------------------------------------------
  94. // TSOSCConfigWidget()
  95. // @mod: (IN) Module.
  96. // @btnSaveId: (IN) ParamId for saving settngs.
  97. // @btnAutoReconnectId: (IN) ParamId for setting auto-reconnect.
  98. // @ipAddress: (IN) Ip Address.
  99. // @txPort: (IN) Output port.
  100. // @rxPort: (IN) Input port.
  101. // @showClient: (IN) Show client dropdown.
  102. // @selectedClient: (IN) Selected client (if @showClient is true).
  103. // @showNamespace: (IN) Show namespace text field.
  104. // @oscNamespace; (IN) The current OSC namespace (if @showNamespace is true).
  105. //------------------------------------------------------------------------------------------------
  106. TSOSCConfigWidget(Module* mod, int btnSaveId, int btnAutoReconnectId, std::string ipAddress, uint16_t txPort, uint16_t rxPort, bool showClient, OSCClient selectedClient, bool showNamespace, std::string oscNamespace);
  107. // If visible, check for btn submit
  108. void step() override;
  109. // Draw if visible
  110. void draw(NVGcontext *vg) override;
  111. // Callback for tabbing between our text boxes.
  112. void onTabField(int id);
  113. // Callback for shift-tabbing between our text boxes.
  114. void onShiftTabField(int id);
  115. // Sets the values
  116. void setValues(std::string ipAddress, uint16_t txPort, uint16_t rxPort)
  117. {
  118. tbIpAddress->text = ipAddress;
  119. tbTxPort->text = std::to_string(txPort);
  120. tbRxPort->text = std::to_string(rxPort);
  121. return;
  122. }
  123. // Sets the values
  124. void setValues(std::string ipAddress, uint16_t txPort, uint16_t rxPort, std::string oscNamespace)
  125. {
  126. setValues(ipAddress, txPort, rxPort);
  127. tbNamespace->text = oscNamespace;
  128. return;
  129. }
  130. // Sets the visibility
  131. void setVisible(bool isVisible) {
  132. visible = isVisible;
  133. tbIpAddress->visible = isVisible;
  134. tbTxPort->visible = isVisible;
  135. tbRxPort->visible = isVisible;
  136. btnClientSelect->visible = isVisible;
  137. ckAutoReconnect->visible = isVisible;
  138. return;
  139. }
  140. // Get the selected OSC Client.
  141. OSCClient getSelectedClient() {
  142. return btnClientSelect->selectedOSCClient;
  143. }
  144. // Set the selected OSC Client.
  145. void setSelectedClient(OSCClient client) {
  146. btnClientSelect->selectedOSCClient = client;
  147. }
  148. // Get the input ip address.
  149. std::string getIpAddress() { return tbIpAddress->text; }
  150. bool isValidIpAddress()
  151. {
  152. return tbIpAddress->isValid();
  153. }
  154. bool isValidPort(std::string port) {
  155. bool isValid = false;
  156. try
  157. {
  158. if (port.length() > 0)
  159. {
  160. int portNumber = std::atoi(port.c_str());
  161. isValid = portNumber > -1 && portNumber <= 0xFFFF;
  162. }
  163. }
  164. catch (const std::exception& ex)
  165. {
  166. isValid = false;
  167. }
  168. return isValid;
  169. }
  170. // If the Tx port is valid.
  171. bool isValidTxPort()
  172. {
  173. return tbTxPort->isValid() && isValidPort(tbTxPort->text);
  174. }
  175. // If the Rx port is valid.
  176. bool isValidRxPort()
  177. {
  178. return tbRxPort->isValid() && isValidPort(tbRxPort->text);
  179. }
  180. // Get the output port.
  181. uint16_t getTxPort() {
  182. if (tbTxPort->text.length() > 0)
  183. return std::atoi(tbTxPort->text.c_str());
  184. else
  185. return 0;
  186. }
  187. // Get the input port.
  188. uint16_t getRxPort() {
  189. if (tbRxPort->text.length() > 0)
  190. return std::atoi(tbRxPort->text.c_str());
  191. else
  192. return 0;
  193. }
  194. };
  195. #endif // ! TSOSCCONFIGWIDGET_HPP