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