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
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. const float yScale = 1.0f / (getHeight() - edge * 2);
  168. const Rectangle<int> clip (g.getClipBounds());
  169. for (int y = jmin (clip.getBottom(), getHeight() - edge); --y >= jmax (edge, clip.getY());)
  170. {
  171. g.setColour (Colour ((y - edge) * yScale, 1.0f, 1.0f, 1.0f));
  172. g.fillRect (edge, y, getWidth() - edge * 2, 1);
  173. }
  174. }
  175. void resized()
  176. {
  177. marker.setBounds (0, roundToInt ((getHeight() - edge * 2) * h), getWidth(), edge * 2);
  178. }
  179. void mouseDown (const MouseEvent& e)
  180. {
  181. mouseDrag (e);
  182. }
  183. void mouseDrag (const MouseEvent& e)
  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)
  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&)
  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 (const 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. }