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.

203 lines
6.4KB

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