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.

568 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& owner_, float& h_, float& s_, float& v_, const int edgeSize)
  60. : owner (owner_), h (h_), s (s_), v (v_), 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 w = (float) getWidth();
  140. const float h = (float) getHeight();
  141. Path p;
  142. p.addTriangle (1.0f, 1.0f,
  143. w * 0.3f, h * 0.5f,
  144. 1.0f, h - 1.0f);
  145. p.addTriangle (w - 1.0f, 1.0f,
  146. w * 0.7f, h * 0.5f,
  147. w - 1.0f, h - 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& owner_, float& h_, float& s_, float& v_, const int edgeSize)
  161. : owner (owner_), h (h_), s (s_), v (v_), 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& owner_, int index_)
  205. : owner (owner_), index (index_)
  206. {
  207. }
  208. void paint (Graphics& g)
  209. {
  210. const Colour colour (owner.getSwatchColour (index));
  211. g.fillCheckerBoard (getLocalBounds(), 6, 6,
  212. Colour (0xffdddddd).overlaidWith (colour),
  213. Colour (0xffffffff).overlaidWith (colour));
  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 flags_,
  253. const int edgeGap_,
  254. const int gapAroundColourSpaceComponent)
  255. : colour (Colours::white),
  256. flags (flags_),
  257. edgeGap (edgeGap_)
  258. {
  259. // not much point having a selector with no components in it!
  260. jassert ((flags_ & (showColourAtTop | showSliders | showColourspace)) != 0);
  261. updateHSV();
  262. if ((flags & showSliders) != 0)
  263. {
  264. addAndMakeVisible (sliders[0] = new ColourComponentSlider (TRANS ("red")));
  265. addAndMakeVisible (sliders[1] = new ColourComponentSlider (TRANS ("green")));
  266. addAndMakeVisible (sliders[2] = new ColourComponentSlider (TRANS ("blue")));
  267. addChildComponent (sliders[3] = new ColourComponentSlider (TRANS ("alpha")));
  268. sliders[3]->setVisible ((flags & showAlphaChannel) != 0);
  269. for (int i = 4; --i >= 0;)
  270. sliders[i]->addListener (this);
  271. }
  272. if ((flags & showColourspace) != 0)
  273. {
  274. addAndMakeVisible (colourSpace = new ColourSpaceView (*this, h, s, v, gapAroundColourSpaceComponent));
  275. addAndMakeVisible (hueSelector = new HueSelectorComp (*this, h, s, v, gapAroundColourSpaceComponent));
  276. }
  277. update();
  278. }
  279. ColourSelector::~ColourSelector()
  280. {
  281. dispatchPendingMessages();
  282. swatchComponents.clear();
  283. }
  284. //==============================================================================
  285. Colour ColourSelector::getCurrentColour() const
  286. {
  287. return ((flags & showAlphaChannel) != 0) ? colour : colour.withAlpha ((uint8) 0xff);
  288. }
  289. void ColourSelector::setCurrentColour (const Colour& c)
  290. {
  291. if (c != colour)
  292. {
  293. colour = ((flags & showAlphaChannel) != 0) ? c : c.withAlpha ((uint8) 0xff);
  294. updateHSV();
  295. update();
  296. }
  297. }
  298. void ColourSelector::setHue (float newH)
  299. {
  300. newH = jlimit (0.0f, 1.0f, newH);
  301. if (h != newH)
  302. {
  303. h = newH;
  304. colour = Colour (h, s, v, colour.getFloatAlpha());
  305. update();
  306. }
  307. }
  308. void ColourSelector::setSV (float newS, float newV)
  309. {
  310. newS = jlimit (0.0f, 1.0f, newS);
  311. newV = jlimit (0.0f, 1.0f, newV);
  312. if (s != newS || v != newV)
  313. {
  314. s = newS;
  315. v = newV;
  316. colour = Colour (h, s, v, colour.getFloatAlpha());
  317. update();
  318. }
  319. }
  320. //==============================================================================
  321. void ColourSelector::updateHSV()
  322. {
  323. colour.getHSB (h, s, v);
  324. }
  325. void ColourSelector::update()
  326. {
  327. if (sliders[0] != nullptr)
  328. {
  329. sliders[0]->setValue ((int) colour.getRed());
  330. sliders[1]->setValue ((int) colour.getGreen());
  331. sliders[2]->setValue ((int) colour.getBlue());
  332. sliders[3]->setValue ((int) colour.getAlpha());
  333. }
  334. if (colourSpace != nullptr)
  335. {
  336. colourSpace->updateIfNeeded();
  337. hueSelector->updateIfNeeded();
  338. }
  339. if ((flags & showColourAtTop) != 0)
  340. repaint (previewArea);
  341. sendChangeMessage();
  342. }
  343. //==============================================================================
  344. void ColourSelector::paint (Graphics& g)
  345. {
  346. g.fillAll (findColour (backgroundColourId));
  347. if ((flags & showColourAtTop) != 0)
  348. {
  349. const Colour currentColour (getCurrentColour());
  350. g.fillCheckerBoard (previewArea, 10, 10,
  351. Colour (0xffdddddd).overlaidWith (currentColour),
  352. Colour (0xffffffff).overlaidWith (currentColour));
  353. g.setColour (Colours::white.overlaidWith (currentColour).contrasting());
  354. g.setFont (Font (14.0f, Font::bold));
  355. g.drawText (currentColour.toDisplayString ((flags & showAlphaChannel) != 0),
  356. previewArea, Justification::centred, false);
  357. }
  358. if ((flags & showSliders) != 0)
  359. {
  360. g.setColour (findColour (labelTextColourId));
  361. g.setFont (11.0f);
  362. for (int i = 4; --i >= 0;)
  363. {
  364. if (sliders[i]->isVisible())
  365. g.drawText (sliders[i]->getName() + ":",
  366. 0, sliders[i]->getY(),
  367. sliders[i]->getX() - 8, sliders[i]->getHeight(),
  368. Justification::centredRight, false);
  369. }
  370. }
  371. }
  372. void ColourSelector::resized()
  373. {
  374. const int swatchesPerRow = 8;
  375. const int swatchHeight = 22;
  376. const int numSliders = ((flags & showAlphaChannel) != 0) ? 4 : 3;
  377. const int numSwatches = getNumSwatches();
  378. const int swatchSpace = numSwatches > 0 ? edgeGap + swatchHeight * ((numSwatches + 7) / swatchesPerRow) : 0;
  379. const int sliderSpace = ((flags & showSliders) != 0) ? jmin (22 * numSliders + edgeGap, proportionOfHeight (0.3f)) : 0;
  380. const int topSpace = ((flags & showColourAtTop) != 0) ? jmin (30 + edgeGap * 2, proportionOfHeight (0.2f)) : edgeGap;
  381. previewArea.setBounds (edgeGap, edgeGap, getWidth() - edgeGap * 2, topSpace - edgeGap * 2);
  382. int y = topSpace;
  383. if ((flags & showColourspace) != 0)
  384. {
  385. const int hueWidth = jmin (50, proportionOfWidth (0.15f));
  386. colourSpace->setBounds (edgeGap, y,
  387. getWidth() - hueWidth - edgeGap - 4,
  388. getHeight() - topSpace - sliderSpace - swatchSpace - edgeGap);
  389. hueSelector->setBounds (colourSpace->getRight() + 4, y,
  390. getWidth() - edgeGap - (colourSpace->getRight() + 4),
  391. colourSpace->getHeight());
  392. y = getHeight() - sliderSpace - swatchSpace - edgeGap;
  393. }
  394. if ((flags & showSliders) != 0)
  395. {
  396. const int sliderHeight = jmax (4, sliderSpace / numSliders);
  397. for (int i = 0; i < numSliders; ++i)
  398. {
  399. sliders[i]->setBounds (proportionOfWidth (0.2f), y,
  400. proportionOfWidth (0.72f), sliderHeight - 2);
  401. y += sliderHeight;
  402. }
  403. }
  404. if (numSwatches > 0)
  405. {
  406. const int startX = 8;
  407. const int xGap = 4;
  408. const int yGap = 4;
  409. const int swatchWidth = (getWidth() - startX * 2) / swatchesPerRow;
  410. y += edgeGap;
  411. if (swatchComponents.size() != numSwatches)
  412. {
  413. swatchComponents.clear();
  414. for (int i = 0; i < numSwatches; ++i)
  415. {
  416. SwatchComponent* const sc = new SwatchComponent (*this, i);
  417. swatchComponents.add (sc);
  418. addAndMakeVisible (sc);
  419. }
  420. }
  421. int x = startX;
  422. for (int i = 0; i < swatchComponents.size(); ++i)
  423. {
  424. SwatchComponent* const sc = swatchComponents.getUnchecked(i);
  425. sc->setBounds (x + xGap / 2,
  426. y + yGap / 2,
  427. swatchWidth - xGap,
  428. swatchHeight - yGap);
  429. if (((i + 1) % swatchesPerRow) == 0)
  430. {
  431. x = startX;
  432. y += swatchHeight;
  433. }
  434. else
  435. {
  436. x += swatchWidth;
  437. }
  438. }
  439. }
  440. }
  441. void ColourSelector::sliderValueChanged (Slider*)
  442. {
  443. if (sliders[0] != nullptr)
  444. setCurrentColour (Colour ((uint8) sliders[0]->getValue(),
  445. (uint8) sliders[1]->getValue(),
  446. (uint8) sliders[2]->getValue(),
  447. (uint8) sliders[3]->getValue()));
  448. }
  449. //==============================================================================
  450. int ColourSelector::getNumSwatches() const
  451. {
  452. return 0;
  453. }
  454. Colour ColourSelector::getSwatchColour (const int) const
  455. {
  456. jassertfalse; // if you've overridden getNumSwatches(), you also need to implement this method
  457. return Colours::black;
  458. }
  459. void ColourSelector::setSwatchColour (const int, const Colour&) const
  460. {
  461. jassertfalse; // if you've overridden getNumSwatches(), you also need to implement this method
  462. }