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.

216 lines
6.8KB

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