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.

211 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. class JucerColourPropertyComponent : public PropertyComponent
  21. {
  22. public:
  23. JucerColourPropertyComponent (const String& name,
  24. const bool canReset)
  25. : PropertyComponent (name)
  26. {
  27. colourPropEditor = new ColourPropEditorComponent (this, canReset);
  28. addAndMakeVisible (colourPropEditor);
  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. public 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),
  49. 10, 10,
  50. Colour (0xffdddddd).overlaidWith (colour),
  51. Colour (0xffffffff).overlaidWith (colour));
  52. g.setColour (Colours::white.overlaidWith (colour).contrasting());
  53. g.setFont (Font (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. void changeListenerCallback (ChangeBroadcaster* source) override
  76. {
  77. const ColourSelector* const cs = (const ColourSelector*) source;
  78. if (cs->getCurrentColour() != getColour())
  79. setColour (cs->getCurrentColour());
  80. }
  81. class ColourSelectorComp : public Component,
  82. public ButtonListener
  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.addListener (this);
  98. }
  99. setSize (300, 400);
  100. }
  101. void resized() override
  102. {
  103. if (defaultButton.isVisible())
  104. {
  105. selector.setBounds (0, 0, getWidth(), getHeight() - 30);
  106. defaultButton.changeWidthToFitText (22);
  107. defaultButton.setTopLeftPosition (10, getHeight() - 26);
  108. }
  109. else
  110. {
  111. selector.setBounds (getLocalBounds());
  112. }
  113. }
  114. void buttonClicked (Button*) override
  115. {
  116. owner->resetToDefault();
  117. owner->refresh();
  118. selector.setCurrentColour (owner->getColour());
  119. }
  120. private:
  121. class ColourSelectorWithSwatches : public ColourSelector
  122. {
  123. public:
  124. ColourSelectorWithSwatches()
  125. {
  126. }
  127. int getNumSwatches() const override
  128. {
  129. return getAppSettings().swatchColours.size();
  130. }
  131. Colour getSwatchColour (int index) const override
  132. {
  133. return getAppSettings().swatchColours [index];
  134. }
  135. void setSwatchColour (int index, const Colour& newColour) override
  136. {
  137. getAppSettings().swatchColours.set (index, newColour);
  138. }
  139. };
  140. ColourEditorComponent* owner;
  141. ColourSelectorWithSwatches selector;
  142. TextButton defaultButton;
  143. };
  144. private:
  145. Colour colour;
  146. bool canResetToDefault;
  147. };
  148. class ColourPropEditorComponent : public ColourEditorComponent
  149. {
  150. JucerColourPropertyComponent* const owner;
  151. public:
  152. ColourPropEditorComponent (JucerColourPropertyComponent* const owner_,
  153. const bool canReset)
  154. : ColourEditorComponent (canReset),
  155. owner (owner_)
  156. {}
  157. void setColour (Colour newColour) override
  158. {
  159. owner->setColour (newColour);
  160. }
  161. Colour getColour() const override
  162. {
  163. return owner->getColour();
  164. }
  165. void resetToDefault() override
  166. {
  167. owner->resetToDefault();
  168. }
  169. };
  170. ScopedPointer<ColourPropEditorComponent> colourPropEditor;
  171. };