osci-render/Source/components/SyphonInputSelectorComponent.h

75 wiersze
2.9 KiB
C
Czysty Zwykły widok Historia

#pragma once
#include <JuceHeader.h>
2025-05-03 11:35:51 +00:00
#include "../modules/juce_sharedtexture/SharedTexture.h"
class SyphonInputSelectorComponent : public juce::Component, private juce::Button::Listener {
public:
2025-05-03 11:35:51 +00:00
SyphonInputSelectorComponent(SharedTextureManager& manager, std::function<void(const juce::String&, const juce::String&)> onConnect, std::function<void()> onDisconnect, juce::String currentSource = "")
: sharedTextureManager(manager), onConnectCallback(onConnect), onDisconnectCallback(onDisconnect), currentSourceName(currentSource) {
addAndMakeVisible(sourceLabel);
sourceLabel.setText("Syphon/Spout Source:", juce::dontSendNotification);
addAndMakeVisible(sourceDropdown);
sourceDropdown.onChange = [this] {
selectedSource = sourceDropdown.getText();
};
addAndMakeVisible(connectButton);
2025-05-03 11:35:51 +00:00
connectButton.setButtonText("Connect");
connectButton.addListener(this);
refreshSources();
2025-05-03 11:35:51 +00:00
// Auto-select first item if no current source specified
if (!currentSourceName.isEmpty()) {
sourceDropdown.setText(currentSourceName, juce::dontSendNotification);
2025-05-03 11:35:51 +00:00
} else if (sourceDropdown.getNumItems() > 0) {
sourceDropdown.setSelectedItemIndex(0, juce::sendNotificationSync);
}
}
void refreshSources() {
sourceDropdown.clear();
auto sources = sharedTextureManager.getAvailableSenders();
for (const auto& s : sources)
sourceDropdown.addItem(s, sourceDropdown.getNumItems() + 1);
}
void resized() override {
auto area = getLocalBounds().reduced(10);
sourceLabel.setBounds(area.removeFromTop(24));
sourceDropdown.setBounds(area.removeFromTop(28));
2025-05-03 11:35:51 +00:00
// Make the button not take up the full width
auto buttonArea = area.removeFromTop(40).reduced(0, 8);
connectButton.setBounds(buttonArea.withSizeKeepingCentre(120, buttonArea.getHeight()));
}
void buttonClicked(juce::Button* b) override {
2025-05-03 11:35:51 +00:00
auto selected = sourceDropdown.getText();
if (selected.isNotEmpty()) {
// Syphon: "ServerName - AppName"
auto parts = juce::StringArray::fromTokens(selected, "-", "");
juce::String server = parts[0].trim();
juce::String app = parts.size() > 1 ? parts[1].trim() : juce::String();
onConnectCallback(server, app);
if (auto* window = findParentComponentOfClass<juce::DialogWindow>())
window->exitModalState(0);
}
}
private:
SharedTextureManager& sharedTextureManager;
std::function<void(const juce::String&, const juce::String&)> onConnectCallback;
std::function<void()> onDisconnectCallback;
juce::String currentSourceName;
juce::String selectedSource;
juce::Label sourceLabel;
juce::ComboBox sourceDropdown;
juce::TextButton connectButton;
};