#include "MainComponent.h"
//==============================================================================
MainComponent::MainComponent()
{
// Make sure you set the size of the component after
// you add any child components.
setSize (800, 600);
// Some platforms require permissions to open input channels so request that here
if (juce::RuntimePermissions::isRequired (juce::RuntimePermissions::recordAudio)
&& ! juce::RuntimePermissions::isGranted (juce::RuntimePermissions::recordAudio))
{
juce::RuntimePermissions::request (juce::RuntimePermissions::recordAudio,
[&] (bool granted) { setAudioChannels (granted ? 2 : 0, 2); });
}
else
{
// Specify the number of input and output channels that we want to open
setAudioChannels (2, 2);
}
partition1.setSize(getWidth() / 3, getHeight());
partition2.setSize(getWidth() / 3, getHeight());
partition3.setSize(getWidth() / 3, getHeight());
viewport1.setViewedComponent(&partition1, false);
viewport2.setViewedComponent(&partition2, false);
viewport3.setViewedComponent(&partition3, false);
// Set viewport bounds and position
viewport1.setBounds(0, 0, getWidth() / 3, getHeight());
viewport2.setBounds(getWidth() / 3, 0, getWidth() / 3, getHeight());
viewport3.setBounds(2 * getWidth() / 3, 0, getWidth() / 3, getHeight());
// Add viewports to the main component
addAndMakeVisible(viewport1);
addAndMakeVisible(viewport2);
addAndMakeVisible(viewport3);
// Add buttons to the first partition
button1.setButtonText("Button 1");
button2.setButtonText("Button 2");
button3.setButtonText("Button 3");
// Set buttons bounds and position within the first partition
button1.setBounds(20, 20, 100, 30);
button2.setBounds(20, 70, 100, 30);
button3.setBounds(20, 120, 100, 30);
// Add content to partitions (buttons in this case)
partition1.addAndMakeVisible(button1);
partition1.addAndMakeVisible(button2);
partition1.addAndMakeVisible(button3);
button1.addMouseListener(this, true);
}
MainComponent::~MainComponent()
{
// This shuts down the audio device and clears the audio source.
shutdownAudio();
}
//==============================================================================
void MainComponent::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
{
// This function will be called when the audio device is started, or when
// its settings (i.e. sample rate, block size, etc) are changed.
// You can use this function to initialise any resources you might need,
// but be careful - it will be called on the audio thread, not the GUI thread.
// For more details, see the help for AudioProcessor::prepareToPlay()
}
void MainComponent::getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill)
{
// Your audio-processing code goes here!
// For more details, see the help for AudioProcessor::getNextAudioBlock()
// Right now we are not producing any data, in which case we need to clear the buffer
// (to prevent the output of random noise)
bufferToFill.clearActiveBufferRegion();
}
void MainComponent::releaseResources()
{
// This will be called when the audio device stops, or when it is being
// restarted due to a setting change.
// For more details, see the help for AudioProcessor::releaseResources()
}
//==============================================================================
void MainComponent::paint (juce::Graphics& g)
{
// (Our component is opaque, so we must completely fill the background with a solid colour)
g.fillAll(getLookAndFeel().findColour(juce::ResizableWindow::backgroundColourId));
// Draw the outer border
g.setColour(juce::Colours::grey);
g.drawRect(getLocalBounds(), 2); // Change the border thickness as needed
// Draw a border around each partition
drawBorder(g, partition1, juce::Colours::grey, 2.0f);
drawBorder(g, partition2, juce::Colours::grey, 2.0f);
drawBorder(g, partition3, juce::Colours::grey, 2.0f);
// You can add your drawing code here!
}
void MainComponent::resized()
{
viewport1.setBounds(0, 0, getWidth() / 3, getHeight());
viewport2.setBounds(getWidth() / 3, 0, getWidth() / 3, getHeight());
viewport3.setBounds(2 * getWidth() / 3, 0, getWidth() / 3, getHeight());
partition1.setBounds(viewport1.getBounds());
partition2.setBounds(viewport2.getBounds());
partition3.setBounds(viewport3.getBounds());
}
void MainComponent::buttonClicked(juce::Button* button)
{
if (button == &window)
{
firstWindow();
secondWindow();
thirdWindow();
}
}
void MainComponent::firstWindow(){
removeAllChildren();
addAndMakeVisible(button1);
addAndMakeVisible(button2);
addAndMakeVisible(button3);
}
void MainComponent::secondWindow(){
}
void MainComponent::thirdWindow(){
}
bool MainComponent::isInterestedInFileDrag(const juce::StringArray &files)
{
return true;
}
void MainComponent::filesDropped(const juce::StringArray& files, int x, int y)
{
if (partition2.getBounds().contains(x, y))
{
// Remove button1 from the main component and partition1
// Add button1 to partition2
partition2.addAndMakeVisible(button1);
addAndMakeVisible(button1); // Add button1 back to the main component
// Set the new bounds of button1 within partition2
juce::Rectangle<int> newBounds(20, 20, 200, 100);
newBounds = newBounds.constrainedWithin(partition2.getBounds());
button1.setBounds(newBounds);
}
}
void MainComponent::drawBorder(juce::Graphics& g, juce::Component& component, juce::Colour colour, float thickness)
{
g.setColour(colour);
g.drawRect(component.getBounds().toFloat(), thickness);
}
//void MainComponent::buttonClicked(const juce::MouseEvent& event)
//{
// // Check which button was clicked
// if (event.eventComponent == &button1)
// {
// // Handle button1 click
// }
// else if (event.eventComponent == &button2)
// {
// // Handle button2 click
// }
// else if (event.eventComponent == &button3)
// {
// // Handle button3 click
// }
//}
void MainComponent::mouseDown(const juce::MouseEvent& event)
{
if (event.eventComponent == &button1)
{
isDragging = true;
initialBounds = button1.getBounds();
initialMousePos = event.getPosition();
isInPartition2 = partition2.getBounds().contains(event.getPosition());
}
}
void MainComponent::mouseDrag(const juce::MouseEvent& event)
{
if (isDragging)
{
// Update the button bounds based on the mouse position
int newX = initialBounds.getX() + event.getPosition().x - initialMousePos.x;
int newY = initialBounds.getY() + event.getPosition().y - initialMousePos.y;
juce::Rectangle<int> newBounds(newX, newY, button1.getWidth(), button1.getHeight());
// Check if the button is inside partition2
bool isCurrentlyInPartition2 = partition2.getBounds().contains(event.getPosition());
button1.setBounds(newBounds);
// Make button1 visible in partition 1 regardless of its position
if (isCurrentlyInPartition2 && !isInPartition2)
{
newBounds = partition2.getBounds();
button1.setButtonText("Button1");
partition2.addAndMakeVisible(button1); // Add button1 to partition 2
button1.toFront(false); // Bring button1 to the front of partition 2
isInPartition2 = true;
}
else if (!isCurrentlyInPartition2 && isInPartition2)
{
// Button is leaving partition 2, revert to initial size
isInPartition2 = false;
}
button1.setBounds(newBounds);
}
}
void MainComponent::mouseUp(const juce::MouseEvent& event)
{
isDragging = false;
}
I have a window with 3 equal partitions. All my buttons are present in partition1.
So what I want to achieve is that when I drag a button it should expand in partition 2 (something like drag and drop) keeping the original button in place, so that I can drag the buttons onto partition 2 any number of times. In the current code it is expanding but the expansion in not clear enough. Also the button1 disappears from the first partition which I don’t want