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.

353 lines
11KB

  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. Original code: Reuben a.k.a. Insertpizhere
  20. ==============================================================================
  21. */
  22. BEGIN_JUCE_NAMESPACE
  23. //==============================================================================
  24. DrawablePad::DrawablePad (const String& name)
  25. : Button (name),
  26. normalImage (0),
  27. overImage (0),
  28. downImage (0),
  29. disabledImage (0),
  30. normalImageOn (0),
  31. overImageOn (0),
  32. downImageOn (0),
  33. disabledImageOn (0)
  34. {
  35. setSize (200, 200);
  36. backgroundOff = Colour (0xffbbbbff);
  37. backgroundOn = Colour (0xff3333ff);
  38. Label = "Pad";
  39. Description = "x:0 y:0";
  40. showdot = false;
  41. showx = false;
  42. showy = false;
  43. hex = false;
  44. x = 0;
  45. y = 0;
  46. roundness = 0.4f;
  47. setMouseClickGrabsKeyboardFocus (false);
  48. }
  49. DrawablePad::~DrawablePad()
  50. {
  51. deleteImages();
  52. }
  53. //==============================================================================
  54. void DrawablePad::deleteImages()
  55. {
  56. deleteAndZero (normalImage);
  57. deleteAndZero (overImage);
  58. deleteAndZero (downImage);
  59. deleteAndZero (disabledImage);
  60. deleteAndZero (normalImageOn);
  61. deleteAndZero (overImageOn);
  62. deleteAndZero (downImageOn);
  63. deleteAndZero (disabledImageOn);
  64. }
  65. //==============================================================================
  66. void DrawablePad::setImages (const Drawable* normal,
  67. const Drawable* over,
  68. const Drawable* down,
  69. const Drawable* disabled,
  70. const Drawable* normalOn,
  71. const Drawable* overOn,
  72. const Drawable* downOn,
  73. const Drawable* disabledOn)
  74. {
  75. deleteImages();
  76. // jassert (normal != 0); // you really need to give it at least a normal image..
  77. if (normal != 0) normalImage = normal->createCopy();
  78. if (over != 0) overImage = over->createCopy();
  79. if (down != 0) downImage = down->createCopy();
  80. if (disabled != 0) disabledImage = disabled->createCopy();
  81. if (normalOn != 0) normalImageOn = normalOn->createCopy();
  82. if (overOn != 0) overImageOn = overOn->createCopy();
  83. if (downOn != 0) downImageOn = downOn->createCopy();
  84. if (disabledOn != 0) disabledImageOn = disabledOn->createCopy();
  85. repaint();
  86. }
  87. //==============================================================================
  88. void DrawablePad::setBackgroundColours (const Colour& toggledOffColour,
  89. const Colour& toggledOnColour)
  90. {
  91. if (backgroundOff != toggledOffColour)// || backgroundOn != toggledOnColour)
  92. {
  93. backgroundOff = toggledOffColour;
  94. backgroundOn = toggledOffColour.contrasting(0.5f).withMultipliedAlpha(2.0);
  95. repaint();
  96. }
  97. }
  98. const Colour& DrawablePad::getBackgroundColour() const throw()
  99. {
  100. return getToggleState() ? backgroundOn
  101. : backgroundOff;
  102. }
  103. //==============================================================================
  104. void DrawablePad::drawButtonBackground (Graphics& g,
  105. Button& button,
  106. const Colour& backgroundColour,
  107. bool isMouseOverButton,
  108. bool isButtonDown)
  109. {
  110. const int width = button.getWidth();
  111. const int height = button.getHeight();
  112. const float indent = 2.0f;
  113. const int cornerSize = jmin (roundFloatToInt (width * roundness),
  114. roundFloatToInt (height * roundness));
  115. Colour bc (backgroundColour);
  116. if (isMouseOverButton)
  117. {
  118. if (isButtonDown)
  119. bc = bc.brighter();
  120. else if (bc.getBrightness() > 0.5f)
  121. bc = bc.darker (0.1f);
  122. else
  123. bc = bc.brighter (0.1f);
  124. }
  125. g.setColour (bc);
  126. if (hex)
  127. {
  128. g.fillPath (hexpath);
  129. g.setColour (bc.contrasting().withAlpha ((isMouseOverButton) ? 0.6f : 0.4f));
  130. g.strokePath (hexpath, PathStrokeType ((isMouseOverButton) ? 2.0f : 1.4f));
  131. }
  132. else
  133. {
  134. Path p;
  135. p.addRoundedRectangle (indent, indent,
  136. width - indent * 2.0f,
  137. height - indent * 2.0f,
  138. (float) cornerSize);
  139. g.fillPath (p);
  140. g.setColour (bc.contrasting().withAlpha ((isMouseOverButton) ? 0.6f : 0.4f));
  141. g.strokePath (p, PathStrokeType ((isMouseOverButton) ? 2.0f : 1.4f));
  142. }
  143. }
  144. //==============================================================================
  145. void DrawablePad::paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown)
  146. {
  147. const int insetX = getWidth() / 4;
  148. const int insetY = getHeight() / 4;
  149. Rectangle<float> imageSpace;
  150. imageSpace.setBounds (insetX, insetY, getWidth() - insetX * 2, getHeight() - insetY * 2);
  151. drawButtonBackground (g,
  152. *this,
  153. getBackgroundColour(),
  154. isMouseOverButton,
  155. isButtonDown);
  156. g.setOpacity (1.0f);
  157. const Drawable* imageToDraw = 0;
  158. if (isEnabled())
  159. {
  160. imageToDraw = getCurrentImage();
  161. }
  162. else
  163. {
  164. imageToDraw = getToggleState() ? disabledImageOn
  165. : disabledImage;
  166. if (imageToDraw == 0)
  167. {
  168. g.setOpacity (0.4f);
  169. imageToDraw = getNormalImage();
  170. }
  171. }
  172. if (imageToDraw != 0)
  173. {
  174. g.setImageResamplingQuality (Graphics::highResamplingQuality);
  175. imageToDraw->drawWithin (g,
  176. imageSpace,
  177. RectanglePlacement::centred,
  178. 1.0f);
  179. }
  180. float fontsize = jmin ((float)(proportionOfWidth(0.2f)),(float)(proportionOfHeight(0.15f)));
  181. if (fontsize < 5.0) fontsize=5.0;
  182. g.setFont (Font (fontsize, Font::bold));
  183. g.setColour (getBackgroundColour().contrasting(0.8f));
  184. g.drawText (Label,
  185. proportionOfWidth (0.0447f),
  186. proportionOfHeight (0.0499f),
  187. proportionOfWidth (0.9137f),
  188. proportionOfHeight (0.1355f),
  189. Justification::centred, true);
  190. if (showdot && ! hex)
  191. {
  192. g.setFont (Font (fontsize*0.9f, Font::plain));
  193. String xy;
  194. if (showx && showy) xy = "x:" + String((int)(x*127.1)) + " y:" + String((int)(y*127.1));
  195. else if (showx) xy = "x:" + String((int)(x*127.1));
  196. else if (showy) xy = "y:" + String((int)(y*127.1));
  197. g.drawText (xy,
  198. proportionOfWidth (0.0447f),
  199. proportionOfHeight (0.8057f),
  200. proportionOfWidth (0.9137f),
  201. proportionOfHeight (0.1355f),
  202. Justification::centred, true);
  203. float diameter = jmin ((float)(proportionOfHeight(0.125f)), (float)(proportionOfWidth(0.5f)));
  204. g.setColour (Colour (0x88faa52a));
  205. g.fillEllipse ((float)(proportionOfWidth(x)) - diameter*0.5f,
  206. (float)(proportionOfHeight(1.0f-y)) - diameter*0.5f,
  207. diameter,
  208. diameter);
  209. g.setColour (Colour (0x99a52a88));
  210. g.drawEllipse ((float)(proportionOfWidth(x)) - diameter*0.5f,
  211. (float)(proportionOfHeight(1.0f-y)) - diameter*0.5f,
  212. diameter,
  213. diameter,
  214. diameter*0.1f);
  215. }
  216. }
  217. //==============================================================================
  218. void DrawablePad::resized()
  219. {
  220. hexpath.clear();
  221. hexpath.startNewSubPath ((float) (proportionOfWidth (0.0100f)), (float) (proportionOfHeight (0.5000f)));
  222. hexpath.lineTo ((float) (proportionOfWidth (0.2500f)), (float) (proportionOfHeight (0.0100f)));
  223. hexpath.lineTo ((float) (proportionOfWidth (0.7500f)), (float) (proportionOfHeight (0.0100f)));
  224. hexpath.lineTo ((float) (proportionOfWidth (0.9900f)), (float) (proportionOfHeight (0.5000f)));
  225. hexpath.lineTo ((float) (proportionOfWidth (0.7500f)), (float) (proportionOfHeight (0.9900f)));
  226. hexpath.lineTo ((float) (proportionOfWidth (0.2500f)), (float) (proportionOfHeight (0.9900f)));
  227. hexpath.closeSubPath();
  228. }
  229. //==============================================================================
  230. void DrawablePad::setHex (bool newhex)
  231. {
  232. hex = newhex;
  233. resized ();
  234. repaint ();
  235. }
  236. bool DrawablePad::isHex() {
  237. return hex;
  238. }
  239. //==============================================================================
  240. const Drawable* DrawablePad::getCurrentImage() const throw()
  241. {
  242. if (isDown())
  243. return getDownImage();
  244. if (isOver())
  245. return getOverImage();
  246. return getNormalImage();
  247. }
  248. const Drawable* DrawablePad::getNormalImage() const throw()
  249. {
  250. return (getToggleState() && normalImageOn != 0) ? normalImageOn
  251. : normalImage;
  252. }
  253. const Drawable* DrawablePad::getOverImage() const throw()
  254. {
  255. const Drawable* d = normalImage;
  256. if (getToggleState())
  257. {
  258. if (overImageOn != 0)
  259. d = overImageOn;
  260. else if (normalImageOn != 0)
  261. d = normalImageOn;
  262. else if (overImage != 0)
  263. d = overImage;
  264. }
  265. else
  266. {
  267. if (overImage != 0)
  268. d = overImage;
  269. }
  270. return d;
  271. }
  272. const Drawable* DrawablePad::getDownImage() const throw()
  273. {
  274. const Drawable* d = normalImage;
  275. if (getToggleState())
  276. {
  277. if (downImageOn != 0) d = downImageOn;
  278. else if (overImageOn != 0) d = overImageOn;
  279. else if (normalImageOn != 0) d = normalImageOn;
  280. else if (downImage != 0) d = downImage;
  281. else d = getOverImage();
  282. }
  283. else
  284. {
  285. if (downImage != 0) d = downImage;
  286. else d = getOverImage();
  287. }
  288. return d;
  289. }
  290. END_JUCE_NAMESPACE