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.

567 lines
17KB

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