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.

566 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class ColourSelector::ColourComponentSlider : public Slider
  18. {
  19. public:
  20. ColourComponentSlider (const String& name)
  21. : Slider (name)
  22. {
  23. setRange (0.0, 255.0, 1.0);
  24. }
  25. String getTextFromValue (double value)
  26. {
  27. return String::toHexString ((int) value).toUpperCase().paddedLeft ('0', 2);
  28. }
  29. double getValueFromText (const String& text)
  30. {
  31. return (double) text.getHexValue32();
  32. }
  33. private:
  34. JUCE_DECLARE_NON_COPYABLE (ColourComponentSlider)
  35. };
  36. //==============================================================================
  37. class ColourSelector::ColourSpaceMarker : public Component
  38. {
  39. public:
  40. ColourSpaceMarker()
  41. {
  42. setInterceptsMouseClicks (false, false);
  43. }
  44. void paint (Graphics& g) override
  45. {
  46. g.setColour (Colour::greyLevel (0.1f));
  47. g.drawEllipse (1.0f, 1.0f, getWidth() - 2.0f, getHeight() - 2.0f, 1.0f);
  48. g.setColour (Colour::greyLevel (0.9f));
  49. g.drawEllipse (2.0f, 2.0f, getWidth() - 4.0f, getHeight() - 4.0f, 1.0f);
  50. }
  51. private:
  52. JUCE_DECLARE_NON_COPYABLE (ColourSpaceMarker)
  53. };
  54. //==============================================================================
  55. class ColourSelector::ColourSpaceView : public Component
  56. {
  57. public:
  58. ColourSpaceView (ColourSelector& cs, float& hue, float& sat, float& val, const int edgeSize)
  59. : owner (cs), h (hue), s (sat), v (val), lastHue (0.0f), edge (edgeSize)
  60. {
  61. addAndMakeVisible (&marker);
  62. setMouseCursor (MouseCursor::CrosshairCursor);
  63. }
  64. void paint (Graphics& g) override
  65. {
  66. if (colours.isNull())
  67. {
  68. const int width = getWidth() / 2;
  69. const int height = getHeight() / 2;
  70. colours = Image (Image::RGB, width, height, false);
  71. Image::BitmapData pixels (colours, Image::BitmapData::writeOnly);
  72. for (int y = 0; y < height; ++y)
  73. {
  74. const float val = 1.0f - y / (float) height;
  75. for (int x = 0; x < width; ++x)
  76. {
  77. const float sat = x / (float) width;
  78. pixels.setPixelColour (x, y, Colour (h, sat, val, 1.0f));
  79. }
  80. }
  81. }
  82. g.setOpacity (1.0f);
  83. g.drawImage (colours, edge, edge, getWidth() - edge * 2, getHeight() - edge * 2,
  84. 0, 0, colours.getWidth(), colours.getHeight());
  85. }
  86. void mouseDown (const MouseEvent& e) override
  87. {
  88. mouseDrag (e);
  89. }
  90. void mouseDrag (const MouseEvent& e) override
  91. {
  92. const float sat = (e.x - edge) / (float) (getWidth() - edge * 2);
  93. const float val = 1.0f - (e.y - edge) / (float) (getHeight() - edge * 2);
  94. owner.setSV (sat, val);
  95. }
  96. void updateIfNeeded()
  97. {
  98. if (lastHue != h)
  99. {
  100. lastHue = h;
  101. colours = Image::null;
  102. repaint();
  103. }
  104. updateMarker();
  105. }
  106. void resized() override
  107. {
  108. colours = Image::null;
  109. updateMarker();
  110. }
  111. private:
  112. ColourSelector& owner;
  113. float& h;
  114. float& s;
  115. float& v;
  116. float lastHue;
  117. ColourSpaceMarker marker;
  118. const int edge;
  119. Image colours;
  120. void updateMarker()
  121. {
  122. marker.setBounds (roundToInt ((getWidth() - edge * 2) * s),
  123. roundToInt ((getHeight() - edge * 2) * (1.0f - v)),
  124. edge * 2, edge * 2);
  125. }
  126. JUCE_DECLARE_NON_COPYABLE (ColourSpaceView)
  127. };
  128. //==============================================================================
  129. class ColourSelector::HueSelectorMarker : public Component
  130. {
  131. public:
  132. HueSelectorMarker()
  133. {
  134. setInterceptsMouseClicks (false, false);
  135. }
  136. void paint (Graphics& g) override
  137. {
  138. const float cw = (float) getWidth();
  139. const float ch = (float) getHeight();
  140. Path p;
  141. p.addTriangle (1.0f, 1.0f,
  142. cw * 0.3f, ch * 0.5f,
  143. 1.0f, ch - 1.0f);
  144. p.addTriangle (cw - 1.0f, 1.0f,
  145. cw * 0.7f, ch * 0.5f,
  146. cw - 1.0f, ch - 1.0f);
  147. g.setColour (Colours::white.withAlpha (0.75f));
  148. g.fillPath (p);
  149. g.setColour (Colours::black.withAlpha (0.75f));
  150. g.strokePath (p, PathStrokeType (1.2f));
  151. }
  152. private:
  153. JUCE_DECLARE_NON_COPYABLE (HueSelectorMarker)
  154. };
  155. //==============================================================================
  156. class ColourSelector::HueSelectorComp : public Component
  157. {
  158. public:
  159. HueSelectorComp (ColourSelector& cs, float& hue, float& sat, float& val, const int edgeSize)
  160. : owner (cs), h (hue), s (sat), v (val), edge (edgeSize)
  161. {
  162. addAndMakeVisible (&marker);
  163. }
  164. void paint (Graphics& g) override
  165. {
  166. ColourGradient cg;
  167. cg.isRadial = false;
  168. cg.point1.setXY (0.0f, (float) edge);
  169. cg.point2.setXY (0.0f, (float) (getHeight() - edge));
  170. for (float i = 0.0f; i <= 1.0f; i += 0.02f)
  171. cg.addColour (i, Colour (i, 1.0f, 1.0f, 1.0f));
  172. g.setGradientFill (cg);
  173. g.fillRect (getLocalBounds().reduced (edge));
  174. }
  175. void resized() override
  176. {
  177. marker.setBounds (0, roundToInt ((getHeight() - edge * 2) * h), getWidth(), edge * 2);
  178. }
  179. void mouseDown (const MouseEvent& e) override
  180. {
  181. mouseDrag (e);
  182. }
  183. void mouseDrag (const MouseEvent& e) override
  184. {
  185. owner.setHue ((e.y - edge) / (float) (getHeight() - edge * 2));
  186. }
  187. void updateIfNeeded()
  188. {
  189. resized();
  190. }
  191. private:
  192. ColourSelector& owner;
  193. float& h;
  194. float& s;
  195. float& v;
  196. HueSelectorMarker marker;
  197. const int edge;
  198. JUCE_DECLARE_NON_COPYABLE (HueSelectorComp)
  199. };
  200. //==============================================================================
  201. class ColourSelector::SwatchComponent : public Component
  202. {
  203. public:
  204. SwatchComponent (ColourSelector& cs, int itemIndex)
  205. : owner (cs), index (itemIndex)
  206. {
  207. }
  208. void paint (Graphics& g) override
  209. {
  210. const Colour c (owner.getSwatchColour (index));
  211. g.fillCheckerBoard (getLocalBounds(), 6, 6,
  212. Colour (0xffdddddd).overlaidWith (c),
  213. Colour (0xffffffff).overlaidWith (c));
  214. }
  215. void mouseDown (const MouseEvent&) override
  216. {
  217. PopupMenu m;
  218. m.addItem (1, TRANS("Use this swatch as the current colour"));
  219. m.addSeparator();
  220. m.addItem (2, TRANS("Set this swatch to the current colour"));
  221. m.showMenuAsync (PopupMenu::Options().withTargetComponent (this),
  222. ModalCallbackFunction::forComponent (menuStaticCallback, this));
  223. }
  224. private:
  225. ColourSelector& owner;
  226. const int index;
  227. static void menuStaticCallback (int result, SwatchComponent* comp)
  228. {
  229. if (comp != nullptr)
  230. {
  231. if (result == 1)
  232. comp->setColourFromSwatch();
  233. else if (result == 2)
  234. comp->setSwatchFromColour();
  235. }
  236. }
  237. void setColourFromSwatch()
  238. {
  239. owner.setCurrentColour (owner.getSwatchColour (index));
  240. }
  241. void setSwatchFromColour()
  242. {
  243. if (owner.getSwatchColour (index) != owner.getCurrentColour())
  244. {
  245. owner.setSwatchColour (index, owner.getCurrentColour());
  246. repaint();
  247. }
  248. }
  249. JUCE_DECLARE_NON_COPYABLE (SwatchComponent)
  250. };
  251. //==============================================================================
  252. ColourSelector::ColourSelector (const int sectionsToShow, const int edge, const int gapAroundColourSpaceComponent)
  253. : colour (Colours::white),
  254. flags (sectionsToShow),
  255. edgeGap (edge)
  256. {
  257. // not much point having a selector with no components in it!
  258. jassert ((flags & (showColourAtTop | showSliders | showColourspace)) != 0);
  259. updateHSV();
  260. if ((flags & showSliders) != 0)
  261. {
  262. addAndMakeVisible (sliders[0] = new ColourComponentSlider (TRANS ("red")));
  263. addAndMakeVisible (sliders[1] = new ColourComponentSlider (TRANS ("green")));
  264. addAndMakeVisible (sliders[2] = new ColourComponentSlider (TRANS ("blue")));
  265. addChildComponent (sliders[3] = new ColourComponentSlider (TRANS ("alpha")));
  266. sliders[3]->setVisible ((flags & showAlphaChannel) != 0);
  267. for (int i = 4; --i >= 0;)
  268. sliders[i]->addListener (this);
  269. }
  270. if ((flags & showColourspace) != 0)
  271. {
  272. addAndMakeVisible (colourSpace = new ColourSpaceView (*this, h, s, v, gapAroundColourSpaceComponent));
  273. addAndMakeVisible (hueSelector = new HueSelectorComp (*this, h, s, v, gapAroundColourSpaceComponent));
  274. }
  275. update();
  276. }
  277. ColourSelector::~ColourSelector()
  278. {
  279. dispatchPendingMessages();
  280. swatchComponents.clear();
  281. }
  282. //==============================================================================
  283. Colour ColourSelector::getCurrentColour() const
  284. {
  285. return ((flags & showAlphaChannel) != 0) ? colour : colour.withAlpha ((uint8) 0xff);
  286. }
  287. void ColourSelector::setCurrentColour (Colour c)
  288. {
  289. if (c != colour)
  290. {
  291. colour = ((flags & showAlphaChannel) != 0) ? c : c.withAlpha ((uint8) 0xff);
  292. updateHSV();
  293. update();
  294. }
  295. }
  296. void ColourSelector::setHue (float newH)
  297. {
  298. newH = jlimit (0.0f, 1.0f, newH);
  299. if (h != newH)
  300. {
  301. h = newH;
  302. colour = Colour (h, s, v, colour.getFloatAlpha());
  303. update();
  304. }
  305. }
  306. void ColourSelector::setSV (float newS, float newV)
  307. {
  308. newS = jlimit (0.0f, 1.0f, newS);
  309. newV = jlimit (0.0f, 1.0f, newV);
  310. if (s != newS || v != newV)
  311. {
  312. s = newS;
  313. v = newV;
  314. colour = Colour (h, s, v, colour.getFloatAlpha());
  315. update();
  316. }
  317. }
  318. //==============================================================================
  319. void ColourSelector::updateHSV()
  320. {
  321. colour.getHSB (h, s, v);
  322. }
  323. void ColourSelector::update()
  324. {
  325. if (sliders[0] != nullptr)
  326. {
  327. sliders[0]->setValue ((int) colour.getRed());
  328. sliders[1]->setValue ((int) colour.getGreen());
  329. sliders[2]->setValue ((int) colour.getBlue());
  330. sliders[3]->setValue ((int) colour.getAlpha());
  331. }
  332. if (colourSpace != nullptr)
  333. {
  334. colourSpace->updateIfNeeded();
  335. hueSelector->updateIfNeeded();
  336. }
  337. if ((flags & showColourAtTop) != 0)
  338. repaint (previewArea);
  339. sendChangeMessage();
  340. }
  341. //==============================================================================
  342. void ColourSelector::paint (Graphics& g)
  343. {
  344. g.fillAll (findColour (backgroundColourId));
  345. if ((flags & showColourAtTop) != 0)
  346. {
  347. const Colour currentColour (getCurrentColour());
  348. g.fillCheckerBoard (previewArea, 10, 10,
  349. Colour (0xffdddddd).overlaidWith (currentColour),
  350. Colour (0xffffffff).overlaidWith (currentColour));
  351. g.setColour (Colours::white.overlaidWith (currentColour).contrasting());
  352. g.setFont (Font (14.0f, Font::bold));
  353. g.drawText (currentColour.toDisplayString ((flags & showAlphaChannel) != 0),
  354. previewArea, Justification::centred, false);
  355. }
  356. if ((flags & showSliders) != 0)
  357. {
  358. g.setColour (findColour (labelTextColourId));
  359. g.setFont (11.0f);
  360. for (int i = 4; --i >= 0;)
  361. {
  362. if (sliders[i]->isVisible())
  363. g.drawText (sliders[i]->getName() + ":",
  364. 0, sliders[i]->getY(),
  365. sliders[i]->getX() - 8, sliders[i]->getHeight(),
  366. Justification::centredRight, false);
  367. }
  368. }
  369. }
  370. void ColourSelector::resized()
  371. {
  372. const int swatchesPerRow = 8;
  373. const int swatchHeight = 22;
  374. const int numSliders = ((flags & showAlphaChannel) != 0) ? 4 : 3;
  375. const int numSwatches = getNumSwatches();
  376. const int swatchSpace = numSwatches > 0 ? edgeGap + swatchHeight * ((numSwatches + 7) / swatchesPerRow) : 0;
  377. const int sliderSpace = ((flags & showSliders) != 0) ? jmin (22 * numSliders + edgeGap, proportionOfHeight (0.3f)) : 0;
  378. const int topSpace = ((flags & showColourAtTop) != 0) ? jmin (30 + edgeGap * 2, proportionOfHeight (0.2f)) : edgeGap;
  379. previewArea.setBounds (edgeGap, edgeGap, getWidth() - edgeGap * 2, topSpace - edgeGap * 2);
  380. int y = topSpace;
  381. if ((flags & showColourspace) != 0)
  382. {
  383. const int hueWidth = jmin (50, proportionOfWidth (0.15f));
  384. colourSpace->setBounds (edgeGap, y,
  385. getWidth() - hueWidth - edgeGap - 4,
  386. getHeight() - topSpace - sliderSpace - swatchSpace - edgeGap);
  387. hueSelector->setBounds (colourSpace->getRight() + 4, y,
  388. getWidth() - edgeGap - (colourSpace->getRight() + 4),
  389. colourSpace->getHeight());
  390. y = getHeight() - sliderSpace - swatchSpace - edgeGap;
  391. }
  392. if ((flags & showSliders) != 0)
  393. {
  394. const int sliderHeight = jmax (4, sliderSpace / numSliders);
  395. for (int i = 0; i < numSliders; ++i)
  396. {
  397. sliders[i]->setBounds (proportionOfWidth (0.2f), y,
  398. proportionOfWidth (0.72f), sliderHeight - 2);
  399. y += sliderHeight;
  400. }
  401. }
  402. if (numSwatches > 0)
  403. {
  404. const int startX = 8;
  405. const int xGap = 4;
  406. const int yGap = 4;
  407. const int swatchWidth = (getWidth() - startX * 2) / swatchesPerRow;
  408. y += edgeGap;
  409. if (swatchComponents.size() != numSwatches)
  410. {
  411. swatchComponents.clear();
  412. for (int i = 0; i < numSwatches; ++i)
  413. {
  414. SwatchComponent* const sc = new SwatchComponent (*this, i);
  415. swatchComponents.add (sc);
  416. addAndMakeVisible (sc);
  417. }
  418. }
  419. int x = startX;
  420. for (int i = 0; i < swatchComponents.size(); ++i)
  421. {
  422. SwatchComponent* const sc = swatchComponents.getUnchecked(i);
  423. sc->setBounds (x + xGap / 2,
  424. y + yGap / 2,
  425. swatchWidth - xGap,
  426. swatchHeight - yGap);
  427. if (((i + 1) % swatchesPerRow) == 0)
  428. {
  429. x = startX;
  430. y += swatchHeight;
  431. }
  432. else
  433. {
  434. x += swatchWidth;
  435. }
  436. }
  437. }
  438. }
  439. void ColourSelector::sliderValueChanged (Slider*)
  440. {
  441. if (sliders[0] != nullptr)
  442. setCurrentColour (Colour ((uint8) sliders[0]->getValue(),
  443. (uint8) sliders[1]->getValue(),
  444. (uint8) sliders[2]->getValue(),
  445. (uint8) sliders[3]->getValue()));
  446. }
  447. //==============================================================================
  448. int ColourSelector::getNumSwatches() const
  449. {
  450. return 0;
  451. }
  452. Colour ColourSelector::getSwatchColour (const int) const
  453. {
  454. jassertfalse; // if you've overridden getNumSwatches(), you also need to implement this method
  455. return Colours::black;
  456. }
  457. void ColourSelector::setSwatchColour (const int, const Colour&) const
  458. {
  459. jassertfalse; // if you've overridden getNumSwatches(), you also need to implement this method
  460. }