Skip to content

Commit

Permalink
Add InstrIOComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
FangCunWuChang committed Jan 26, 2024
1 parent a340c3c commit cd0b9e7
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 5 deletions.
52 changes: 47 additions & 5 deletions src/ui/component/InstrComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,50 @@ InstrComponent::InstrComponent() {
this->bypassButton->setMouseCursor(juce::MouseCursor::PointingHandCursor);
this->bypassButton->onClick = [this] { this->bypass(); };
this->addAndMakeVisible(this->bypassButton.get());

/** IO Component */
this->input = std::make_unique<InstrIOComponent>(true);
this->addAndMakeVisible(this->input.get());
this->output = std::make_unique<InstrIOComponent>(false);
this->addAndMakeVisible(this->output.get());
}

void InstrComponent::resized() {
/** Size */
auto screenSize = utils::getScreenSize(this);
int paddingHeight = screenSize.getHeight() * 0.005;
int paddingWidth = screenSize.getWidth() * 0.005;
int splitWidth = screenSize.getWidth() * 0.005;
int splitWidth = screenSize.getWidth() * 0.0025;

int buttonHeight = this->getHeight() - paddingHeight * 2;
int ioHideWidth = screenSize.getWidth() * 0.075;

int left = paddingWidth, right = (this->getWidth() - paddingWidth);
bool ioShown = this->getWidth() > ioHideWidth;

/** Input Comp */
if (ioShown) {
juce::Rectangle<int> inputRect(
left, paddingHeight,
buttonHeight, buttonHeight);
this->input->setBounds(inputRect);
left += (buttonHeight + splitWidth);
}
this->input->setVisible(ioShown);

/** Output Comp */
if (ioShown) {
juce::Rectangle<int> outputRect(
right - buttonHeight,
paddingHeight, buttonHeight, buttonHeight);
this->output->setBounds(outputRect);
right -= (buttonHeight + splitWidth);
}
this->output->setVisible(ioShown);

/** Bypass Button */
juce::Rectangle<int> bypassRect(
this->getWidth() - paddingWidth - buttonHeight,
right - buttonHeight,
paddingHeight, buttonHeight, buttonHeight);
this->bypassButton->setBounds(bypassRect);
}
Expand All @@ -57,10 +87,13 @@ void InstrComponent::paint(juce::Graphics& g) {
auto screenSize = utils::getScreenSize(this);
int paddingHeight = screenSize.getHeight() * 0.0025;
int paddingWidth = screenSize.getWidth() * 0.01;
int splitWidth = screenSize.getWidth() * 0.005;
int splitWidth = screenSize.getWidth() * 0.0025;

int buttonHeight = this->getHeight() - paddingHeight * 2;
float textHeight = buttonHeight * 0.8;
int ioHideWidth = screenSize.getWidth() * 0.075;

bool ioShown = this->getWidth() > ioHideWidth;

/** Color */
auto& laf = this->getLookAndFeel();
Expand All @@ -79,9 +112,15 @@ void InstrComponent::paint(juce::Graphics& g) {
g.fillAll();

/** Text */
int textLeft = paddingWidth;
int textRight = this->getWidth() - paddingWidth - buttonHeight - splitWidth;
if (ioShown) {
textLeft += (buttonHeight + splitWidth);
textRight -= (buttonHeight + splitWidth);
}
juce::Rectangle<int> textRect(
paddingWidth, paddingHeight,
this->getWidth() - paddingWidth * 2 - buttonHeight - splitWidth,
textLeft, paddingHeight,
textRight - textLeft,
buttonHeight);
g.setColour(textColor);
g.setFont(textFont);
Expand All @@ -98,6 +137,9 @@ void InstrComponent::update(int index) {
this->bypassButton->setToggleState(!quickAPI::getInstrBypass(index),
juce::NotificationType::dontSendNotification);

this->input->update(index);
this->output->update(index);

this->repaint();

this->setTooltip(this->createToolTip());
Expand Down
4 changes: 4 additions & 0 deletions src/ui/component/InstrComponent.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <JuceHeader.h>
#include "InstrIOComponent.h"

class InstrComponent final
: public juce::Component,
Expand All @@ -24,6 +25,9 @@ class InstrComponent final
std::unique_ptr<juce::Drawable> bypassIconOn = nullptr;
std::unique_ptr<juce::DrawableButton> bypassButton = nullptr;

std::unique_ptr<InstrIOComponent> input = nullptr;
std::unique_ptr<InstrIOComponent> output = nullptr;

void bypass();
void editorShow();

Expand Down
53 changes: 53 additions & 0 deletions src/ui/component/InstrIOComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "InstrIOComponent.h"
#include "../Utils.h"

InstrIOComponent::InstrIOComponent(bool isInput)
: isInput(isInput) {
this->setMouseCursor(juce::MouseCursor::PointingHandCursor);
}

void InstrIOComponent::paint(juce::Graphics& g) {
/** Size */
auto screenSize = utils::getScreenSize(this);
float pointHeight = screenSize.getHeight() * 0.01;

/** Color */
juce::Colour colorOn = this->isInput
? juce::Colours::blue
:juce::Colours::lightgreen;
juce::Colour colorOff = this->isInput
? juce::Colours::indianred
: juce::Colours::yellow;

/** Center */
auto centerPoint = this->getLocalBounds().getCentre();

/** Draw Point */
if (this->linked) {
juce::Path path;
path.startNewSubPath(centerPoint.getX(), centerPoint.getY() - pointHeight / 2);
path.lineTo(centerPoint.getX() + pointHeight / 2, centerPoint.getY());
path.lineTo(centerPoint.getX(), centerPoint.getY() + pointHeight / 2);
path.lineTo(centerPoint.getX() - pointHeight / 2, centerPoint.getY());
path.closeSubPath();

g.setColour(colorOn);
g.fillPath(path);
}
else {
juce::Rectangle<float> pointRect(
centerPoint.getX() - pointHeight / 2, centerPoint.getY() - pointHeight / 2,
pointHeight, pointHeight);

g.setColour(colorOff);
g.fillEllipse(pointRect);
}
}

void InstrIOComponent::update(int index) {
this->index = index;
if (index > -1) {
/** TODO */
this->repaint();
}
}
20 changes: 20 additions & 0 deletions src/ui/component/InstrIOComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <JuceHeader.h>

class InstrIOComponent final : public juce::Component {
public:
InstrIOComponent() = delete;
InstrIOComponent(bool isInput);

void paint(juce::Graphics& g) override;

void update(int index);

private:
const bool isInput;
int index = -1;
bool linked = false;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(InstrIOComponent)
};

0 comments on commit cd0b9e7

Please sign in to comment.