Skip to content

Commit

Permalink
Merged eyedropper improvements by Alex, improved inspector colour swatch
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyschoen committed Dec 9, 2024
1 parent 7bb5156 commit 7d132e7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
38 changes: 27 additions & 11 deletions Source/Components/ColourPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Eyedropper : public Timer
, public MouseListener {
class EyedropperDisplayComponnent : public Component {
Colour colour;
Image pixelImage;

public:
std::function<void()> onClick = []() { };
Expand All @@ -23,8 +24,8 @@ class Eyedropper : public Timer
setVisible(true);
setAlwaysOnTop(true);
setInterceptsMouseClicks(true, true);
setSize(50, 50);
setMouseCursor(MouseCursor::CrosshairCursor);
setSize(130, 130);
setMouseCursor(MouseCursor::NoCursor);
}

void show()
Expand All @@ -42,25 +43,40 @@ class Eyedropper : public Timer
onClick();
}

void setColour(Colour& c)
void setROI(Image& image, Point<int> position)
{
colour = c;
pixelImage = image.getClippedImage(Rectangle<int>(0, 0, 11, 11).withCentre(position)).rescaled(getWidth() - 8 * 2, getHeight() - 8 * 2, Graphics::ResamplingQuality::lowResamplingQuality);
colour = image.getPixelAt(position.x, position.y);
repaint();
}

void paint(Graphics& g) override
{
auto bounds = getLocalBounds().toFloat().withTrimmedTop(20).withTrimmedLeft(20).reduced(8);
auto bounds = getLocalBounds().toFloat().reduced(8);

Path shadowPath;
shadowPath.addEllipse(bounds.reduced(2));
shadowPath.addEllipse(bounds);
StackShadow::renderDropShadow(hash("eyedropper"), g, shadowPath, Colours::black.withAlpha(0.85f), 8, { 0, 1 }, 0);

g.setColour(colour);
g.fillEllipse(bounds);
auto circleBounds = bounds;

g.reduceClipRegion(shadowPath);

g.drawImage(pixelImage, bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(), 0, 0, bounds.getWidth(), bounds.getHeight());

auto const rectSize = bounds.getWidth() / 12.0f;
auto highlightRect = Rectangle<int>(0, 0, rectSize, rectSize);
highlightRect.setCentre(bounds.getCentreX(), bounds.getCentreY());

// Draw dark background for pixel highlight
g.setColour(Colours::black);
g.drawRect(highlightRect.expanded(2, 2), 3.0f);

// Draw the pixel highlight
g.setColour(Colour::greyLevel(0.9f));
g.drawEllipse(bounds, 2.0f);
g.drawRect(highlightRect.expanded(1, 1), 1.0f);

g.drawEllipse(circleBounds, 2.0f);
}
};

Expand Down Expand Up @@ -113,7 +129,6 @@ class Eyedropper : public Timer
private:
void setColour(Colour colour)
{
colourDisplayer.setColour(colour);
currentColour = colour;
}

Expand All @@ -129,7 +144,8 @@ class Eyedropper : public Timer
}

componentImage = topLevel->createComponentSnapshot(topLevel->getLocalBounds(), false, 1.0f);
colourDisplayer.setTopLeftPosition(topLevel->localPointToGlobal(position).translated(-20, -20));
colourDisplayer.setCentrePosition(topLevel->localPointToGlobal(position));
colourDisplayer.setROI(componentImage, position);
setColour(componentImage.getPixelAt(position.x, position.y));

if(mouseOverSurface) {
Expand Down
44 changes: 27 additions & 17 deletions Source/Components/PropertiesPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ class PropertiesPanel : public Component {

if(isDown || isOver) {
// Add some alpha to make it look good on any background...
g.setColour(findColour(PlugDataColour::sidebarActiveBackgroundColourId).contrasting(isOver ? 0.1f : 0.15f).withAlpha(0.25f));
g.setColour(findColour(PlugDataColour::sidebarActiveBackgroundColourId).contrasting(isOver ? 0.125f : 0.2f).withAlpha(0.25f));
g.fillRoundedRectangle(buttonBounds, Corners::defaultCornerRadius);
}
auto textColour = findColour(PlugDataColour::panelTextColourId);
Expand Down Expand Up @@ -642,7 +642,11 @@ class PropertiesPanel : public Component {
hexValueEditor.setFont(Fonts::getCurrentFont().withHeight(13.5f));

hexValueEditor.onEditorShow = [this](){
hexValueEditor.getCurrentTextEditor()->setInputRestrictions(7, "#0123456789ABCDEFabcdef");
auto* editor = hexValueEditor.getCurrentTextEditor();
editor->setBorder(BorderSize<int>(0, 0, 4, 1));
editor->setJustification(Justification::centred);
editor->setInputRestrictions(7, "#0123456789ABCDEFabcdef");
editor->applyColourToAllText(Colour::fromString(currentColour.toString()).contrasting(0.95f));
};

hexValueEditor.onEditorHide = [this]() {
Expand Down Expand Up @@ -679,9 +683,9 @@ class PropertiesPanel : public Component {

void resized() override
{
auto bounds = getLocalBounds().removeFromRight(getWidth() / (2 - hideLabel)).reduced(4);
swatchComponent.setBounds(bounds);
hexValueEditor.setBounds(bounds);
auto bounds = getLocalBounds().removeFromRight(getWidth() / (2 - hideLabel));
swatchComponent.setBounds(bounds.reduced(4));
hexValueEditor.setBounds(bounds.reduced(1));
}

void valueChanged(Value& v) override
Expand All @@ -692,22 +696,29 @@ class PropertiesPanel : public Component {
}
}

void mouseUp(MouseEvent const& e) override
void mouseDown(MouseEvent const& e) override
{
if(e.getNumberOfClicks() == 2)
{
hexValueEditor.showEditor();
}
else {
auto pickerBounds = getScreenBounds().expanded(5);
ColourPicker::getInstance().show(findParentComponentOfClass<PluginEditor>(), getTopLevelComponent(), false, Colour::fromString(currentColour.toString()), pickerBounds, [_this = SafePointer(this)](Colour c) {
if(hexValueEditor.isBeingEdited() && e.getNumberOfClicks() > 1) return;

Timer::callAfterDelay(250, [_this = SafePointer(this)](){
if(!_this || _this->hexValueEditor.isBeingEdited() || ColourPicker::getInstance().isShowing()) return;

auto pickerBounds = _this->getScreenBounds().expanded(5);

ColourPicker::getInstance().show(_this->findParentComponentOfClass<PluginEditor>(), _this->getTopLevelComponent(), false, Colour::fromString(_this->currentColour.toString()), pickerBounds, [_this](Colour c) {
if (!_this)
return;

_this->currentColour = c.toString();
_this->repaint();
});
}
});
}

void mouseDoubleClick (MouseEvent const& e) override
{
if(hexValueEditor.isBeingEdited() || ColourPicker::getInstance().isShowing()) return;
hexValueEditor.showEditor();
}

private:
Expand Down Expand Up @@ -768,10 +779,9 @@ class PropertiesPanel : public Component {
{

currentColour.referTo(value);
setWantsKeyboardFocus(true);

currentColour.addListener(this);

setWantsKeyboardFocus(false);

addAndMakeVisible(hexValueEditor);
hexValueEditor.getProperties().set("NoOutline", true);
hexValueEditor.getProperties().set("NoBackground", true);
Expand Down

0 comments on commit 7d132e7

Please sign in to comment.