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.

209 lines
6.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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 (new ColourSelectorComp (this, canResetToDefault),
  73. getScreenBounds(), nullptr);
  74. }
  75. class ColourSelectorComp : public Component
  76. {
  77. public:
  78. ColourSelectorComp (ColourEditorComponent* owner_,
  79. const bool canReset)
  80. : owner (owner_),
  81. defaultButton ("Reset to Default")
  82. {
  83. addAndMakeVisible (selector);
  84. selector.setName ("Colour");
  85. selector.setCurrentColour (owner->getColour());
  86. selector.addChangeListener (owner);
  87. if (canReset)
  88. {
  89. addAndMakeVisible (defaultButton);
  90. defaultButton.onClick = [this]
  91. {
  92. owner->resetToDefault();
  93. owner->refresh();
  94. selector.setCurrentColour (owner->getColour());
  95. };
  96. }
  97. setSize (300, 400);
  98. }
  99. void resized() override
  100. {
  101. if (defaultButton.isVisible())
  102. {
  103. selector.setBounds (0, 0, getWidth(), getHeight() - 30);
  104. defaultButton.changeWidthToFitText (22);
  105. defaultButton.setTopLeftPosition (10, getHeight() - 26);
  106. }
  107. else
  108. {
  109. selector.setBounds (getLocalBounds());
  110. }
  111. }
  112. private:
  113. class ColourSelectorWithSwatches : public ColourSelector
  114. {
  115. public:
  116. ColourSelectorWithSwatches()
  117. {
  118. }
  119. int getNumSwatches() const override
  120. {
  121. return getAppSettings().swatchColours.size();
  122. }
  123. Colour getSwatchColour (int index) const override
  124. {
  125. return getAppSettings().swatchColours [index];
  126. }
  127. void setSwatchColour (int index, const Colour& newColour) override
  128. {
  129. getAppSettings().swatchColours.set (index, newColour);
  130. }
  131. };
  132. ColourEditorComponent* owner;
  133. ColourSelectorWithSwatches selector;
  134. TextButton defaultButton;
  135. };
  136. private:
  137. void changeListenerCallback (ChangeBroadcaster* source) override
  138. {
  139. const ColourSelector* const cs = (const ColourSelector*) source;
  140. if (cs->getCurrentColour() != getColour())
  141. setColour (cs->getCurrentColour());
  142. }
  143. Colour colour;
  144. bool canResetToDefault;
  145. };
  146. class ColourPropEditorComponent : public ColourEditorComponent
  147. {
  148. JucerColourPropertyComponent* const owner;
  149. public:
  150. ColourPropEditorComponent (JucerColourPropertyComponent* const owner_,
  151. const bool canReset)
  152. : ColourEditorComponent (canReset),
  153. owner (owner_)
  154. {}
  155. void setColour (Colour newColour) override
  156. {
  157. owner->setColour (newColour);
  158. }
  159. Colour getColour() const override
  160. {
  161. return owner->getColour();
  162. }
  163. void resetToDefault() override
  164. {
  165. owner->resetToDefault();
  166. }
  167. };
  168. std::unique_ptr<ColourPropEditorComponent> colourPropEditor;
  169. };