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.

212 lines
6.7KB

  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);
  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. public Button::Listener
  84. {
  85. public:
  86. ColourSelectorComp (ColourEditorComponent* owner_,
  87. const bool canReset)
  88. : owner (owner_),
  89. defaultButton ("Reset to Default")
  90. {
  91. addAndMakeVisible (selector);
  92. selector.setName ("Colour");
  93. selector.setCurrentColour (owner->getColour());
  94. selector.addChangeListener (owner);
  95. if (canReset)
  96. {
  97. addAndMakeVisible (defaultButton);
  98. defaultButton.addListener (this);
  99. }
  100. setSize (300, 400);
  101. }
  102. void resized() override
  103. {
  104. if (defaultButton.isVisible())
  105. {
  106. selector.setBounds (0, 0, getWidth(), getHeight() - 30);
  107. defaultButton.changeWidthToFitText (22);
  108. defaultButton.setTopLeftPosition (10, getHeight() - 26);
  109. }
  110. else
  111. {
  112. selector.setBounds (getLocalBounds());
  113. }
  114. }
  115. void buttonClicked (Button*) override
  116. {
  117. owner->resetToDefault();
  118. owner->refresh();
  119. selector.setCurrentColour (owner->getColour());
  120. }
  121. private:
  122. class ColourSelectorWithSwatches : public ColourSelector
  123. {
  124. public:
  125. ColourSelectorWithSwatches()
  126. {
  127. }
  128. int getNumSwatches() const override
  129. {
  130. return getAppSettings().swatchColours.size();
  131. }
  132. Colour getSwatchColour (int index) const override
  133. {
  134. return getAppSettings().swatchColours [index];
  135. }
  136. void setSwatchColour (int index, const Colour& newColour) override
  137. {
  138. getAppSettings().swatchColours.set (index, newColour);
  139. }
  140. };
  141. ColourEditorComponent* owner;
  142. ColourSelectorWithSwatches selector;
  143. TextButton defaultButton;
  144. };
  145. private:
  146. Colour colour;
  147. bool canResetToDefault;
  148. };
  149. class ColourPropEditorComponent : public ColourEditorComponent
  150. {
  151. JucerColourPropertyComponent* const owner;
  152. public:
  153. ColourPropEditorComponent (JucerColourPropertyComponent* const owner_,
  154. const bool canReset)
  155. : ColourEditorComponent (canReset),
  156. owner (owner_)
  157. {}
  158. void setColour (Colour newColour) override
  159. {
  160. owner->setColour (newColour);
  161. }
  162. Colour getColour() const override
  163. {
  164. return owner->getColour();
  165. }
  166. void resetToDefault() override
  167. {
  168. owner->resetToDefault();
  169. }
  170. };
  171. ScopedPointer<ColourPropEditorComponent> colourPropEditor;
  172. };