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.

234 lines
7.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. #include "../JuceLibraryCode/JuceHeader.h"
  21. #include "OSCLogListBox.h"
  22. //==============================================================================
  23. class MainContentComponent : public Component,
  24. private Button::Listener,
  25. private OSCReceiver::Listener<OSCReceiver::MessageLoopCallback>
  26. {
  27. public:
  28. //==============================================================================
  29. MainContentComponent()
  30. : portNumberLabel (new Label),
  31. portNumberField (new Label),
  32. connectButton (new TextButton ("Connect")),
  33. clearButton (new TextButton ("Clear")),
  34. connectionStatusLabel (new Label),
  35. oscLogListBox (new OSCLogListBox),
  36. oscReceiver (new OSCReceiver),
  37. currentPortNumber (-1)
  38. {
  39. setSize (700, 400);
  40. portNumberLabel->setText ("UDP Port Number: ", dontSendNotification);
  41. portNumberLabel->setBounds (10, 18, 130, 25);
  42. addAndMakeVisible (portNumberLabel);
  43. portNumberField->setText ("9001", dontSendNotification);
  44. portNumberField->setEditable (true, true, true);
  45. portNumberField->setBounds (140, 18, 50, 25);
  46. addAndMakeVisible (portNumberField);
  47. connectButton->setBounds (210, 18, 100, 25);
  48. addAndMakeVisible (connectButton);
  49. connectButton->addListener (this);
  50. clearButton->setBounds (320, 18, 60, 25);
  51. addAndMakeVisible (clearButton);
  52. clearButton->addListener (this);
  53. connectionStatusLabel->setBounds (450, 18, 240, 25);
  54. updateConnectionStatusLabel();
  55. addAndMakeVisible (connectionStatusLabel);
  56. oscLogListBox->setBounds (0, 60, 700, 340);
  57. addAndMakeVisible (oscLogListBox);
  58. oscReceiver->addListener (this);
  59. oscReceiver->registerFormatErrorHandler (
  60. [this] (const char* data, int dataSize)
  61. {
  62. oscLogListBox->addInvalidOSCPacket (data, dataSize);
  63. }
  64. );
  65. }
  66. private:
  67. //==============================================================================
  68. ScopedPointer<Label> portNumberLabel;
  69. ScopedPointer<Label> portNumberField;
  70. ScopedPointer<TextButton> connectButton;
  71. ScopedPointer<TextButton> clearButton;
  72. ScopedPointer<Label> connectionStatusLabel;
  73. ScopedPointer<OSCLogListBox> oscLogListBox;
  74. ScopedPointer<OSCReceiver> oscReceiver;
  75. int currentPortNumber;
  76. //==============================================================================
  77. void buttonClicked (Button* b) override
  78. {
  79. if (b == connectButton)
  80. connectButtonClicked();
  81. else if (b == clearButton)
  82. clearButtonClicked();
  83. }
  84. //==============================================================================
  85. void connectButtonClicked()
  86. {
  87. if (! isConnected())
  88. connect();
  89. else
  90. disconnect();
  91. updateConnectionStatusLabel();
  92. }
  93. //==============================================================================
  94. void clearButtonClicked()
  95. {
  96. oscLogListBox->clear();
  97. }
  98. //==============================================================================
  99. void oscMessageReceived (const OSCMessage& message) override
  100. {
  101. oscLogListBox->addOSCMessage (message);
  102. }
  103. void oscBundleReceived (const OSCBundle& bundle) override
  104. {
  105. oscLogListBox->addOSCBundle (bundle);
  106. }
  107. //==============================================================================
  108. void connect()
  109. {
  110. int portToConnect = portNumberField->getText().getIntValue();
  111. if (! isValidOscPort (portToConnect))
  112. {
  113. handleInvalidPortNumberEntered();
  114. return;
  115. }
  116. if (oscReceiver->connect (portToConnect))
  117. {
  118. currentPortNumber = portToConnect;
  119. connectButton->setButtonText ("Disconnect");
  120. }
  121. else
  122. {
  123. handleConnectError (portToConnect);
  124. }
  125. }
  126. //==============================================================================
  127. void disconnect()
  128. {
  129. if (oscReceiver->disconnect())
  130. {
  131. currentPortNumber = -1;
  132. connectButton->setButtonText ("Connect");
  133. }
  134. else
  135. {
  136. handleDisconnectError();
  137. }
  138. }
  139. //==============================================================================
  140. void handleConnectError (int failedPort)
  141. {
  142. AlertWindow::showMessageBoxAsync (
  143. AlertWindow::WarningIcon,
  144. "OSC Connection error",
  145. "Error: could not connect to port " + String (failedPort),
  146. "OK");
  147. }
  148. //==============================================================================
  149. void handleDisconnectError()
  150. {
  151. AlertWindow::showMessageBoxAsync (
  152. AlertWindow::WarningIcon,
  153. "Unknown error",
  154. "An unknown error occured while trying to disconnect from UPD port.",
  155. "OK");
  156. }
  157. //==============================================================================
  158. void handleInvalidPortNumberEntered()
  159. {
  160. AlertWindow::showMessageBoxAsync (
  161. AlertWindow::WarningIcon,
  162. "Invalid port number",
  163. "Error: you have entered an invalid UDP port number.",
  164. "OK");
  165. }
  166. //==============================================================================
  167. bool isConnected()
  168. {
  169. return currentPortNumber != -1;
  170. }
  171. //==============================================================================
  172. bool isValidOscPort (int port)
  173. {
  174. return port > 0 && port < 65536;
  175. }
  176. //==============================================================================
  177. void updateConnectionStatusLabel()
  178. {
  179. String text = "Status: ";
  180. if (isConnected())
  181. text += "Connected to UDP port " + String (currentPortNumber);
  182. else
  183. text += "Disconnected";
  184. Colour textColour = isConnected() ? Colours::green : Colours::red;
  185. connectionStatusLabel->setText (text, dontSendNotification);
  186. connectionStatusLabel->setFont (Font (15.00f, Font::bold));
  187. connectionStatusLabel->setColour (Label::textColourId, textColour);
  188. connectionStatusLabel->setJustificationType (Justification::centredRight);
  189. }
  190. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
  191. };