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.

202 lines
6.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #pragma once
  14. //==============================================================================
  15. class JucerColourPropertyComponent : public PropertyComponent
  16. {
  17. public:
  18. JucerColourPropertyComponent (const String& name,
  19. const bool canReset)
  20. : PropertyComponent (name)
  21. {
  22. colourPropEditor.reset (new ColourPropEditorComponent (this, canReset));
  23. addAndMakeVisible (colourPropEditor.get());
  24. }
  25. virtual void setColour (Colour newColour) = 0;
  26. virtual Colour getColour() const = 0;
  27. virtual void resetToDefault() = 0;
  28. void refresh() override
  29. {
  30. ((ColourPropEditorComponent*) getChildComponent (0))->refresh();
  31. }
  32. class ColourEditorComponent : public Component,
  33. public ChangeListener
  34. {
  35. public:
  36. ColourEditorComponent (const bool canReset)
  37. : canResetToDefault (canReset)
  38. {
  39. }
  40. void paint (Graphics& g) override
  41. {
  42. g.fillAll (Colours::grey);
  43. g.fillCheckerBoard (getLocalBounds().reduced (2, 2).toFloat(),
  44. 10.0f, 10.0f,
  45. Colour (0xffdddddd).overlaidWith (colour),
  46. Colour (0xffffffff).overlaidWith (colour));
  47. g.setColour (Colours::white.overlaidWith (colour).contrasting());
  48. g.setFont (Font (getHeight() * 0.6f, Font::bold));
  49. g.drawFittedText (colour.toDisplayString (true),
  50. 2, 1, getWidth() - 4, getHeight() - 1,
  51. Justification::centred, 1);
  52. }
  53. virtual void setColour (Colour newColour) = 0;
  54. virtual void resetToDefault() = 0;
  55. virtual Colour getColour() const = 0;
  56. void refresh()
  57. {
  58. const Colour col (getColour());
  59. if (col != colour)
  60. {
  61. colour = col;
  62. repaint();
  63. }
  64. }
  65. void mouseDown (const MouseEvent&) override
  66. {
  67. CallOutBox::launchAsynchronously (new ColourSelectorComp (this, canResetToDefault),
  68. getScreenBounds(), nullptr);
  69. }
  70. void changeListenerCallback (ChangeBroadcaster* source) override
  71. {
  72. const ColourSelector* const cs = (const ColourSelector*) source;
  73. if (cs->getCurrentColour() != getColour())
  74. setColour (cs->getCurrentColour());
  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. Colour colour;
  139. bool canResetToDefault;
  140. };
  141. class ColourPropEditorComponent : public ColourEditorComponent
  142. {
  143. JucerColourPropertyComponent* const owner;
  144. public:
  145. ColourPropEditorComponent (JucerColourPropertyComponent* const owner_,
  146. const bool canReset)
  147. : ColourEditorComponent (canReset),
  148. owner (owner_)
  149. {}
  150. void setColour (Colour newColour) override
  151. {
  152. owner->setColour (newColour);
  153. }
  154. Colour getColour() const override
  155. {
  156. return owner->getColour();
  157. }
  158. void resetToDefault() override
  159. {
  160. owner->resetToDefault();
  161. }
  162. };
  163. std::unique_ptr<ColourPropEditorComponent> colourPropEditor;
  164. };