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.

289 lines
9.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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. #include "../../../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_DrawableText.h"
  21. #include "juce_DrawableComposite.h"
  22. //==============================================================================
  23. DrawableText::DrawableText()
  24. : colour (Colours::black),
  25. justification (Justification::centredLeft)
  26. {
  27. setBoundingBox (RelativeParallelogram (RelativePoint (0.0f, 0.0f),
  28. RelativePoint (50.0f, 0.0f),
  29. RelativePoint (0.0f, 20.0f)));
  30. setFont (Font (15.0f), true);
  31. }
  32. DrawableText::DrawableText (const DrawableText& other)
  33. : text (other.text),
  34. font (other.font),
  35. colour (other.colour),
  36. justification (other.justification),
  37. bounds (other.bounds),
  38. fontSizeControlPoint (other.fontSizeControlPoint)
  39. {
  40. }
  41. DrawableText::~DrawableText()
  42. {
  43. }
  44. //==============================================================================
  45. void DrawableText::setText (const String& newText)
  46. {
  47. text = newText;
  48. }
  49. void DrawableText::setColour (const Colour& newColour)
  50. {
  51. colour = newColour;
  52. }
  53. void DrawableText::setFont (const Font& newFont, bool applySizeAndScale)
  54. {
  55. font = newFont;
  56. if (applySizeAndScale)
  57. {
  58. Point<float> corners[3];
  59. bounds.resolveThreePoints (corners, parent);
  60. setFontSizeControlPoint (RelativePoint (RelativeParallelogram::getPointForInternalCoord (corners,
  61. Point<float> (font.getHorizontalScale() * font.getHeight(), font.getHeight()))));
  62. }
  63. }
  64. void DrawableText::setJustification (const Justification& newJustification)
  65. {
  66. justification = newJustification;
  67. }
  68. void DrawableText::setBoundingBox (const RelativeParallelogram& newBounds)
  69. {
  70. bounds = newBounds;
  71. }
  72. void DrawableText::setFontSizeControlPoint (const RelativePoint& newPoint)
  73. {
  74. fontSizeControlPoint = newPoint;
  75. }
  76. //==============================================================================
  77. void DrawableText::render (const Drawable::RenderingContext& context) const
  78. {
  79. Point<float> points[3];
  80. bounds.resolveThreePoints (points, parent);
  81. const float w = Line<float> (points[0], points[1]).getLength();
  82. const float h = Line<float> (points[0], points[2]).getLength();
  83. const Point<float> fontCoords (bounds.getInternalCoordForPoint (points, fontSizeControlPoint.resolve (parent)));
  84. const float fontHeight = jlimit (1.0f, h, fontCoords.getY());
  85. const float fontWidth = jlimit (0.01f, w, fontCoords.getX());
  86. Font f (font);
  87. f.setHeight (fontHeight);
  88. f.setHorizontalScale (fontWidth / fontHeight);
  89. context.g.setColour (colour.withMultipliedAlpha (context.opacity));
  90. GlyphArrangement ga;
  91. ga.addFittedText (f, text, 0, 0, w, h, justification, 0x100000);
  92. ga.draw (context.g,
  93. AffineTransform::fromTargetPoints (0, 0, points[0].getX(), points[0].getY(),
  94. w, 0, points[1].getX(), points[1].getY(),
  95. 0, h, points[2].getX(), points[2].getY())
  96. .followedBy (context.transform));
  97. }
  98. const Rectangle<float> DrawableText::getBounds() const
  99. {
  100. return bounds.getBounds (parent);
  101. }
  102. bool DrawableText::hitTest (float x, float y) const
  103. {
  104. Path p;
  105. bounds.getPath (p, parent);
  106. return p.contains (x, y);
  107. }
  108. Drawable* DrawableText::createCopy() const
  109. {
  110. return new DrawableText (*this);
  111. }
  112. void DrawableText::invalidatePoints()
  113. {
  114. }
  115. //==============================================================================
  116. const Identifier DrawableText::valueTreeType ("Text");
  117. const Identifier DrawableText::ValueTreeWrapper::text ("text");
  118. const Identifier DrawableText::ValueTreeWrapper::colour ("colour");
  119. const Identifier DrawableText::ValueTreeWrapper::font ("font");
  120. const Identifier DrawableText::ValueTreeWrapper::justification ("justification");
  121. const Identifier DrawableText::ValueTreeWrapper::topLeft ("topLeft");
  122. const Identifier DrawableText::ValueTreeWrapper::topRight ("topRight");
  123. const Identifier DrawableText::ValueTreeWrapper::bottomLeft ("bottomLeft");
  124. const Identifier DrawableText::ValueTreeWrapper::fontSizeAnchor ("fontSizeAnchor");
  125. //==============================================================================
  126. DrawableText::ValueTreeWrapper::ValueTreeWrapper (const ValueTree& state_)
  127. : ValueTreeWrapperBase (state_)
  128. {
  129. jassert (state.hasType (valueTreeType));
  130. }
  131. const String DrawableText::ValueTreeWrapper::getText() const
  132. {
  133. return state [text].toString();
  134. }
  135. void DrawableText::ValueTreeWrapper::setText (const String& newText, UndoManager* undoManager)
  136. {
  137. state.setProperty (text, newText, undoManager);
  138. }
  139. Value DrawableText::ValueTreeWrapper::getTextValue (UndoManager* undoManager)
  140. {
  141. return state.getPropertyAsValue (text, undoManager);
  142. }
  143. const Colour DrawableText::ValueTreeWrapper::getColour() const
  144. {
  145. return Colour::fromString (state [colour].toString());
  146. }
  147. void DrawableText::ValueTreeWrapper::setColour (const Colour& newColour, UndoManager* undoManager)
  148. {
  149. state.setProperty (colour, newColour.toString(), undoManager);
  150. }
  151. const Justification DrawableText::ValueTreeWrapper::getJustification() const
  152. {
  153. return Justification ((int) state [justification]);
  154. }
  155. void DrawableText::ValueTreeWrapper::setJustification (const Justification& newJustification, UndoManager* undoManager)
  156. {
  157. state.setProperty (justification, newJustification.getFlags(), undoManager);
  158. }
  159. const Font DrawableText::ValueTreeWrapper::getFont() const
  160. {
  161. return Font::fromString (state [font]);
  162. }
  163. void DrawableText::ValueTreeWrapper::setFont (const Font& newFont, UndoManager* undoManager)
  164. {
  165. state.setProperty (font, newFont.toString(), undoManager);
  166. }
  167. Value DrawableText::ValueTreeWrapper::getFontValue (UndoManager* undoManager)
  168. {
  169. return state.getPropertyAsValue (font, undoManager);
  170. }
  171. const RelativeParallelogram DrawableText::ValueTreeWrapper::getBoundingBox() const
  172. {
  173. return RelativeParallelogram (state [topLeft].toString(), state [topRight].toString(), state [bottomLeft].toString());
  174. }
  175. void DrawableText::ValueTreeWrapper::setBoundingBox (const RelativeParallelogram& newBounds, UndoManager* undoManager)
  176. {
  177. state.setProperty (topLeft, newBounds.topLeft.toString(), undoManager);
  178. state.setProperty (topRight, newBounds.topRight.toString(), undoManager);
  179. state.setProperty (bottomLeft, newBounds.bottomLeft.toString(), undoManager);
  180. }
  181. const RelativePoint DrawableText::ValueTreeWrapper::getFontSizeControlPoint() const
  182. {
  183. return state [fontSizeAnchor].toString();
  184. }
  185. void DrawableText::ValueTreeWrapper::setFontSizeControlPoint (const RelativePoint& p, UndoManager* undoManager)
  186. {
  187. state.setProperty (fontSizeAnchor, p.toString(), undoManager);
  188. }
  189. //==============================================================================
  190. const Rectangle<float> DrawableText::refreshFromValueTree (const ValueTree& tree, ImageProvider*)
  191. {
  192. ValueTreeWrapper v (tree);
  193. setName (v.getID());
  194. const RelativeParallelogram newBounds (v.getBoundingBox());
  195. const RelativePoint newFontPoint (v.getFontSizeControlPoint());
  196. const Colour newColour (v.getColour());
  197. const Justification newJustification (v.getJustification());
  198. const String newText (v.getText());
  199. const Font newFont (v.getFont());
  200. if (text != newText || font != newFont || justification != newJustification
  201. || colour != newColour || bounds != newBounds || newFontPoint != fontSizeControlPoint)
  202. {
  203. const Rectangle<float> damage (getBounds());
  204. setBoundingBox (newBounds);
  205. setFontSizeControlPoint (newFontPoint);
  206. setColour (newColour);
  207. setFont (newFont, false);
  208. setJustification (newJustification);
  209. setText (newText);
  210. return damage.getUnion (getBounds());
  211. }
  212. return Rectangle<float>();
  213. }
  214. const ValueTree DrawableText::createValueTree (ImageProvider*) const
  215. {
  216. ValueTree tree (valueTreeType);
  217. ValueTreeWrapper v (tree);
  218. v.setID (getName(), 0);
  219. v.setText (text, 0);
  220. v.setFont (font, 0);
  221. v.setJustification (justification, 0);
  222. v.setColour (colour, 0);
  223. v.setBoundingBox (bounds, 0);
  224. v.setFontSizeControlPoint (fontSizeControlPoint, 0);
  225. return tree;
  226. }
  227. END_JUCE_NAMESPACE