The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
6.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. //==============================================================================
  21. class JucerColourPropertyComponent : public PropertyComponent
  22. {
  23. public:
  24. JucerColourPropertyComponent (const String& name,
  25. const bool canReset)
  26. : PropertyComponent (name)
  27. {
  28. colourPropEditor.reset (new ColourPropEditorComponent (this, canReset));
  29. addAndMakeVisible (colourPropEditor.get());
  30. }
  31. virtual void setColour (Colour newColour) = 0;
  32. virtual Colour getColour() const = 0;
  33. virtual void resetToDefault() = 0;
  34. void refresh() override
  35. {
  36. ((ColourPropEditorComponent*) getChildComponent (0))->refresh();
  37. }
  38. class ColourEditorComponent : public Component,
  39. public ChangeListener
  40. {
  41. public:
  42. ColourEditorComponent (const bool canReset)
  43. : canResetToDefault (canReset)
  44. {
  45. }
  46. void paint (Graphics& g) override
  47. {
  48. g.fillAll (Colours::grey);
  49. g.fillCheckerBoard (getLocalBounds().reduced (2, 2).toFloat(),
  50. 10.0f, 10.0f,
  51. Colour (0xffdddddd).overlaidWith (colour),
  52. Colour (0xffffffff).overlaidWith (colour));
  53. g.setColour (Colours::white.overlaidWith (colour).contrasting());
  54. g.setFont (Font (getHeight() * 0.6f, Font::bold));
  55. g.drawFittedText (colour.toDisplayString (true),
  56. 2, 1, getWidth() - 4, getHeight() - 1,
  57. Justification::centred, 1);
  58. }
  59. virtual void setColour (Colour newColour) = 0;
  60. virtual void resetToDefault() = 0;
  61. virtual Colour getColour() const = 0;
  62. void refresh()
  63. {
  64. const Colour col (getColour());
  65. if (col != colour)
  66. {
  67. colour = col;
  68. repaint();
  69. }
  70. }
  71. void mouseDown (const MouseEvent&) override
  72. {
  73. CallOutBox::launchAsynchronously (new ColourSelectorComp (this, canResetToDefault),
  74. getScreenBounds(), nullptr);
  75. }
  76. void changeListenerCallback (ChangeBroadcaster* source) override
  77. {
  78. const ColourSelector* const cs = (const ColourSelector*) source;
  79. if (cs->getCurrentColour() != getColour())
  80. setColour (cs->getCurrentColour());
  81. }
  82. class ColourSelectorComp : public Component
  83. {
  84. public:
  85. ColourSelectorComp (ColourEditorComponent* owner_,
  86. const bool canReset)
  87. : owner (owner_),
  88. defaultButton ("Reset to Default")
  89. {
  90. addAndMakeVisible (selector);
  91. selector.setName ("Colour");
  92. selector.setCurrentColour (owner->getColour());
  93. selector.addChangeListener (owner);
  94. if (canReset)
  95. {
  96. addAndMakeVisible (defaultButton);
  97. defaultButton.onClick = [this]
  98. {
  99. owner->resetToDefault();
  100. owner->refresh();
  101. selector.setCurrentColour (owner->getColour());
  102. };
  103. }
  104. setSize (300, 400);
  105. }
  106. void resized() override
  107. {
  108. if (defaultButton.isVisible())
  109. {
  110. selector.setBounds (0, 0, getWidth(), getHeight() - 30);
  111. defaultButton.changeWidthToFitText (22);
  112. defaultButton.setTopLeftPosition (10, getHeight() - 26);
  113. }
  114. else
  115. {
  116. selector.setBounds (getLocalBounds());
  117. }
  118. }
  119. private:
  120. class ColourSelectorWithSwatches : public ColourSelector
  121. {
  122. public:
  123. ColourSelectorWithSwatches()
  124. {
  125. }
  126. int getNumSwatches() const override
  127. {
  128. return getAppSettings().swatchColours.size();
  129. }
  130. Colour getSwatchColour (int index) const override
  131. {
  132. return getAppSettings().swatchColours [index];
  133. }
  134. void setSwatchColour (int index, const Colour& newColour) override
  135. {
  136. getAppSettings().swatchColours.set (index, newColour);
  137. }
  138. };
  139. ColourEditorComponent* owner;
  140. ColourSelectorWithSwatches selector;
  141. TextButton defaultButton;
  142. };
  143. private:
  144. Colour colour;
  145. bool canResetToDefault;
  146. };
  147. class ColourPropEditorComponent : public ColourEditorComponent
  148. {
  149. JucerColourPropertyComponent* const owner;
  150. public:
  151. ColourPropEditorComponent (JucerColourPropertyComponent* const owner_,
  152. const bool canReset)
  153. : ColourEditorComponent (canReset),
  154. owner (owner_)
  155. {}
  156. void setColour (Colour newColour) override
  157. {
  158. owner->setColour (newColour);
  159. }
  160. Colour getColour() const override
  161. {
  162. return owner->getColour();
  163. }
  164. void resetToDefault() override
  165. {
  166. owner->resetToDefault();
  167. }
  168. };
  169. std::unique_ptr<ColourPropEditorComponent> colourPropEditor;
  170. };