1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-09 23:34:20 +00:00
JUCE/extras/Projucer/Source/ComponentEditor/Properties/jucer_ColourPropertyComponent.h
2024-04-18 14:16:00 +01:00

218 lines
7.3 KiB
C++

/*
==============================================================================
This file is part of the JUCE framework.
Copyright (c) Raw Material Software Limited
JUCE is an open source framework subject to commercial or open source
licensing.
By downloading, installing, or using the JUCE framework, or combining the
JUCE framework with any other source code, object code, content or any other
copyrightable work, you agree to the terms of the JUCE End User Licence
Agreement, and all incorporated terms including the JUCE Privacy Policy and
the JUCE Website Terms of Service, as applicable, which will bind you. If you
do not agree to the terms of these agreements, we will not license the JUCE
framework to you, and you must discontinue the installation or download
process and cease use of the JUCE framework.
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
Or:
You may also use this code under the terms of the AGPLv3:
https://www.gnu.org/licenses/agpl-3.0.en.html
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
==============================================================================
*/
#pragma once
//==============================================================================
class JucerColourPropertyComponent : public PropertyComponent
{
public:
JucerColourPropertyComponent (const String& name,
const bool canReset)
: PropertyComponent (name)
{
colourPropEditor.reset (new ColourPropEditorComponent (this, canReset));
addAndMakeVisible (colourPropEditor.get());
}
virtual void setColour (Colour newColour) = 0;
virtual Colour getColour() const = 0;
virtual void resetToDefault() = 0;
void refresh() override
{
((ColourPropEditorComponent*) getChildComponent (0))->refresh();
}
class ColourEditorComponent : public Component,
private ChangeListener
{
public:
ColourEditorComponent (const bool canReset)
: canResetToDefault (canReset)
{
}
void paint (Graphics& g) override
{
g.fillAll (Colours::grey);
g.fillCheckerBoard (getLocalBounds().reduced (2, 2).toFloat(),
10.0f, 10.0f,
Colour (0xffdddddd).overlaidWith (colour),
Colour (0xffffffff).overlaidWith (colour));
g.setColour (Colours::white.overlaidWith (colour).contrasting());
g.setFont (FontOptions ((float) getHeight() * 0.6f, Font::bold));
g.drawFittedText (colour.toDisplayString (true),
2, 1, getWidth() - 4, getHeight() - 1,
Justification::centred, 1);
}
virtual void setColour (Colour newColour) = 0;
virtual void resetToDefault() = 0;
virtual Colour getColour() const = 0;
void refresh()
{
const Colour col (getColour());
if (col != colour)
{
colour = col;
repaint();
}
}
void mouseDown (const MouseEvent&) override
{
CallOutBox::launchAsynchronously (std::make_unique<ColourSelectorComp> (this, canResetToDefault),
getScreenBounds(),
nullptr);
}
class ColourSelectorComp : public Component
{
public:
ColourSelectorComp (ColourEditorComponent* owner_,
const bool canReset)
: owner (owner_),
defaultButton ("Reset to Default")
{
addAndMakeVisible (selector);
selector.setName ("Colour");
selector.setCurrentColour (owner->getColour());
selector.addChangeListener (owner);
if (canReset)
{
addAndMakeVisible (defaultButton);
defaultButton.onClick = [this]
{
owner->resetToDefault();
owner->refresh();
selector.setCurrentColour (owner->getColour());
};
}
setSize (300, 400);
}
void resized() override
{
if (defaultButton.isVisible())
{
selector.setBounds (0, 0, getWidth(), getHeight() - 30);
defaultButton.changeWidthToFitText (22);
defaultButton.setTopLeftPosition (10, getHeight() - 26);
}
else
{
selector.setBounds (getLocalBounds());
}
}
private:
class ColourSelectorWithSwatches : public ColourSelector
{
public:
ColourSelectorWithSwatches()
{
}
int getNumSwatches() const override
{
return getAppSettings().swatchColours.size();
}
Colour getSwatchColour (int index) const override
{
return getAppSettings().swatchColours [index];
}
void setSwatchColour (int index, const Colour& newColour) override
{
getAppSettings().swatchColours.set (index, newColour);
}
};
ColourEditorComponent* owner;
ColourSelectorWithSwatches selector;
TextButton defaultButton;
};
private:
void changeListenerCallback (ChangeBroadcaster* source) override
{
const ColourSelector* const cs = (const ColourSelector*) source;
if (cs->getCurrentColour() != getColour())
setColour (cs->getCurrentColour());
}
Colour colour;
bool canResetToDefault;
};
class ColourPropEditorComponent : public ColourEditorComponent
{
JucerColourPropertyComponent* const owner;
public:
ColourPropEditorComponent (JucerColourPropertyComponent* const owner_,
const bool canReset)
: ColourEditorComponent (canReset),
owner (owner_)
{}
void setColour (Colour newColour) override
{
owner->setColour (newColour);
}
Colour getColour() const override
{
return owner->getColour();
}
void resetToDefault() override
{
owner->resetToDefault();
}
};
std::unique_ptr<ColourPropEditorComponent> colourPropEditor;
};