From 7d132e741287881a651458ebed2e9acf0664993b Mon Sep 17 00:00:00 2001 From: Timothy Schoen Date: Mon, 9 Dec 2024 21:35:21 +0100 Subject: [PATCH] Merged eyedropper improvements by Alex, improved inspector colour swatch --- Source/Components/ColourPicker.h | 38 +++++++++++++++++-------- Source/Components/PropertiesPanel.h | 44 ++++++++++++++++++----------- 2 files changed, 54 insertions(+), 28 deletions(-) diff --git a/Source/Components/ColourPicker.h b/Source/Components/ColourPicker.h index 56c9e2bad..8e20e437d 100644 --- a/Source/Components/ColourPicker.h +++ b/Source/Components/ColourPicker.h @@ -14,6 +14,7 @@ class Eyedropper : public Timer , public MouseListener { class EyedropperDisplayComponnent : public Component { Colour colour; + Image pixelImage; public: std::function onClick = []() { }; @@ -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() @@ -42,25 +43,40 @@ class Eyedropper : public Timer onClick(); } - void setColour(Colour& c) + void setROI(Image& image, Point position) { - colour = c; + pixelImage = image.getClippedImage(Rectangle(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(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); } }; @@ -113,7 +129,6 @@ class Eyedropper : public Timer private: void setColour(Colour colour) { - colourDisplayer.setColour(colour); currentColour = colour; } @@ -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) { diff --git a/Source/Components/PropertiesPanel.h b/Source/Components/PropertiesPanel.h index a4e91beda..001c081d3 100644 --- a/Source/Components/PropertiesPanel.h +++ b/Source/Components/PropertiesPanel.h @@ -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); @@ -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(0, 0, 4, 1)); + editor->setJustification(Justification::centred); + editor->setInputRestrictions(7, "#0123456789ABCDEFabcdef"); + editor->applyColourToAllText(Colour::fromString(currentColour.toString()).contrasting(0.95f)); }; hexValueEditor.onEditorHide = [this]() { @@ -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 @@ -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(), 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(), _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: @@ -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);