The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

220 lines
6.9KB

  1. /*
  2. ==============================================================================
  3. This file was auto-generated!
  4. ==============================================================================
  5. */
  6. #ifndef MAINCOMPONENT_H_INCLUDED
  7. #define MAINCOMPONENT_H_INCLUDED
  8. #include "../JuceLibraryCode/JuceHeader.h"
  9. #include "OSCLogListBox.h"
  10. //==============================================================================
  11. class MainContentComponent : public Component,
  12. private Button::Listener,
  13. private OSCReceiver::Listener<OSCReceiver::MessageLoopCallback>
  14. {
  15. public:
  16. //==============================================================================
  17. MainContentComponent()
  18. : portNumberLabel (new Label),
  19. portNumberField (new Label),
  20. connectButton (new TextButton ("Connect")),
  21. clearButton (new TextButton ("Clear")),
  22. connectionStatusLabel (new Label),
  23. oscLogListBox (new OSCLogListBox),
  24. oscReceiver (new OSCReceiver),
  25. currentPortNumber (-1)
  26. {
  27. setSize (700, 400);
  28. portNumberLabel->setText ("UDP Port Number: ", dontSendNotification);
  29. portNumberLabel->setBounds (10, 18, 130, 25);
  30. addAndMakeVisible (portNumberLabel);
  31. portNumberField->setText ("9001", dontSendNotification);
  32. portNumberField->setEditable (true, true, true);
  33. portNumberField->setBounds (140, 18, 50, 25);
  34. addAndMakeVisible (portNumberField);
  35. connectButton->setBounds (210, 18, 100, 25);
  36. addAndMakeVisible (connectButton);
  37. connectButton->addListener (this);
  38. clearButton->setBounds (320, 18, 60, 25);
  39. addAndMakeVisible (clearButton);
  40. clearButton->addListener (this);
  41. connectionStatusLabel->setBounds (450, 18, 240, 25);
  42. updateConnectionStatusLabel();
  43. addAndMakeVisible (connectionStatusLabel);
  44. oscLogListBox->setBounds (0, 60, 700, 340);
  45. addAndMakeVisible (oscLogListBox);
  46. oscReceiver->addListener (this);
  47. oscReceiver->registerFormatErrorHandler (
  48. [this] (const char* data, int dataSize)
  49. {
  50. oscLogListBox->addInvalidOSCPacket (data, dataSize);
  51. }
  52. );
  53. }
  54. private:
  55. //==============================================================================
  56. ScopedPointer<Label> portNumberLabel;
  57. ScopedPointer<Label> portNumberField;
  58. ScopedPointer<TextButton> connectButton;
  59. ScopedPointer<TextButton> clearButton;
  60. ScopedPointer<Label> connectionStatusLabel;
  61. ScopedPointer<OSCLogListBox> oscLogListBox;
  62. ScopedPointer<OSCReceiver> oscReceiver;
  63. int currentPortNumber;
  64. //==============================================================================
  65. void buttonClicked (Button* b) override
  66. {
  67. if (b == connectButton)
  68. connectButtonClicked();
  69. else if (b == clearButton)
  70. clearButtonClicked();
  71. }
  72. //==============================================================================
  73. void connectButtonClicked()
  74. {
  75. if (! isConnected())
  76. connect();
  77. else
  78. disconnect();
  79. updateConnectionStatusLabel();
  80. }
  81. //==============================================================================
  82. void clearButtonClicked()
  83. {
  84. oscLogListBox->clear();
  85. }
  86. //==============================================================================
  87. void oscMessageReceived (const OSCMessage& message) override
  88. {
  89. oscLogListBox->addOSCMessage (message);
  90. }
  91. void oscBundleReceived (const OSCBundle& bundle) override
  92. {
  93. oscLogListBox->addOSCBundle (bundle);
  94. }
  95. //==============================================================================
  96. void connect()
  97. {
  98. int portToConnect = portNumberField->getText().getIntValue();
  99. if (! isValidOscPort (portToConnect))
  100. {
  101. handleInvalidPortNumberEntered();
  102. return;
  103. }
  104. if (oscReceiver->connect (portToConnect))
  105. {
  106. currentPortNumber = portToConnect;
  107. connectButton->setButtonText ("Disconnect");
  108. }
  109. else
  110. {
  111. handleConnectError (portToConnect);
  112. }
  113. }
  114. //==============================================================================
  115. void disconnect()
  116. {
  117. if (oscReceiver->disconnect())
  118. {
  119. currentPortNumber = -1;
  120. connectButton->setButtonText ("Connect");
  121. }
  122. else
  123. {
  124. handleDisconnectError();
  125. }
  126. }
  127. //==============================================================================
  128. void handleConnectError (int failedPort)
  129. {
  130. AlertWindow::showMessageBoxAsync (
  131. AlertWindow::WarningIcon,
  132. "OSC Connection error",
  133. "Error: could not connect to port " + String (failedPort),
  134. "OK");
  135. }
  136. //==============================================================================
  137. void handleDisconnectError()
  138. {
  139. AlertWindow::showMessageBoxAsync (
  140. AlertWindow::WarningIcon,
  141. "Unknown error",
  142. "An unknown error occured while trying to disconnect from UPD port.",
  143. "OK");
  144. }
  145. //==============================================================================
  146. void handleInvalidPortNumberEntered()
  147. {
  148. AlertWindow::showMessageBoxAsync (
  149. AlertWindow::WarningIcon,
  150. "Invalid port number",
  151. "Error: you have entered an invalid UDP port number.",
  152. "OK");
  153. }
  154. //==============================================================================
  155. bool isConnected()
  156. {
  157. return currentPortNumber != -1;
  158. }
  159. //==============================================================================
  160. bool isValidOscPort (int port)
  161. {
  162. return port > 0 && port < 65536;
  163. }
  164. //==============================================================================
  165. void updateConnectionStatusLabel()
  166. {
  167. String text = "Status: ";
  168. if (isConnected())
  169. text += "Connected to UDP port " + String (currentPortNumber);
  170. else
  171. text += "Disconnected";
  172. Colour textColour = isConnected() ? Colours::green : Colours::red;
  173. connectionStatusLabel->setText (text, dontSendNotification);
  174. connectionStatusLabel->setFont (Font (15.00f, Font::bold));
  175. connectionStatusLabel->setColour (Label::textColourId, textColour);
  176. connectionStatusLabel->setJustificationType (Justification::centredRight);
  177. }
  178. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
  179. };
  180. #endif // MAINCOMPONENT_H_INCLUDED