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.

107 lines
3.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCETICE project - Copyright 2009 by Lucio Asnaghi.
  4. JUCETICE is based around the JUCE library - "Jules' Utility Class Extensions"
  5. Copyright 2007 by Julian Storer.
  6. ------------------------------------------------------------------------------
  7. JUCE and JUCETICE can be redistributed and/or modified under the terms of
  8. the GNU General Public License, as published by the Free Software Foundation;
  9. either version 2 of the License, or (at your option) any later version.
  10. JUCE and JUCETICE are distributed in the hope that they will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with JUCE and JUCETICE; if not, visit www.gnu.org/licenses or write to
  16. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. Boston, MA 02111-1307 USA
  18. ==============================================================================
  19. */
  20. BEGIN_JUCE_NAMESPACE
  21. //==============================================================================
  22. ImageKnob::ImageKnob (const String& sliderName)
  23. : ParameterSlider (sliderName),
  24. defaultValue (0.5f),
  25. stitchedImage (0),
  26. stitchedOrientation (ImageKnob::Vertical),
  27. numberOfSubImages (0)
  28. {
  29. setSliderStyle (Slider::Rotary);
  30. }
  31. ImageKnob::~ImageKnob()
  32. {
  33. }
  34. //==============================================================================
  35. void ImageKnob::setStitchedImage (Image newImage,
  36. StitchOrientation newOrientation,
  37. const int newNumberOfSubImages)
  38. {
  39. jassert (!newImage.isNull());
  40. jassert (newNumberOfSubImages > 0);
  41. stitchedImage = newImage;
  42. stitchedImage.duplicateIfShared();
  43. stitchedOrientation = newOrientation;
  44. numberOfSubImages = newNumberOfSubImages;
  45. }
  46. void ImageKnob::setDefaultValue (const float newDefault)
  47. {
  48. defaultValue = newDefault;
  49. }
  50. //==============================================================================
  51. void ImageKnob::paint (Graphics& g)
  52. {
  53. jassert (stitchedImage.isValid()); // you must first assign a stitchedImage to be
  54. // drawn before showing your knob !
  55. const bool sliderEnabled = isEnabled();
  56. // const bool hightlight = isMouseOverOrDragging();
  57. int imageWidth, imageHeight;
  58. int sourceX, sourceY, offsetX, offsetY;
  59. int indexImage = roundFloatToInt ((numberOfSubImages - 1) * valueToProportionOfLength (getValue()));
  60. if (stitchedOrientation == Horizontal)
  61. {
  62. imageWidth = stitchedImage.getWidth() / numberOfSubImages;
  63. imageHeight = stitchedImage.getHeight();
  64. offsetX = indexImage * imageWidth;
  65. offsetY = 0;
  66. }
  67. else
  68. {
  69. imageWidth = stitchedImage.getWidth();
  70. imageHeight = stitchedImage.getHeight() / numberOfSubImages;
  71. offsetX = 0;
  72. offsetY = indexImage * imageHeight;
  73. }
  74. sourceX = (getWidth () - imageWidth) / 2;
  75. sourceY = (getHeight () - imageHeight) / 2;
  76. if (! sliderEnabled)
  77. g.setOpacity (0.6f);
  78. else
  79. g.setOpacity (1.0f);
  80. g.drawImage (stitchedImage,
  81. sourceX, sourceY, imageWidth, imageHeight,
  82. offsetX, offsetY, imageWidth, imageHeight);
  83. }
  84. END_JUCE_NAMESPACE