Text.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Font.h"
  25. #include "Log.h"
  26. #include "Profiler.h"
  27. #include "ResourceCache.h"
  28. #include "StringUtils.h"
  29. #include "Text.h"
  30. #include "Texture2D.h"
  31. #include "DebugNew.h"
  32. Text::Text(const std::string& name, const std::string& text) :
  33. UIElement(name),
  34. mFontSize(DEFAULT_FONT_SIZE),
  35. mMaxWidth(0),
  36. mText(text),
  37. mTextAlignment(HA_LEFT),
  38. mRowSpacing(1.0f),
  39. mSelectionStart(0),
  40. mSelectionLength(0),
  41. mSelectionColor(Color(0.0f, 0.0f, 0.0f, 0.0f)),
  42. mHoverColor(Color(0.0f, 0.0f, 0.0f, 0.0f)),
  43. mRowHeight(0)
  44. {
  45. }
  46. Text::~Text()
  47. {
  48. }
  49. void Text::setStyle(const XMLElement& element, ResourceCache* cache)
  50. {
  51. UIElement::setStyle(element, cache);
  52. if (element.hasChildElement("font"))
  53. {
  54. XMLElement fontElem = element.getChildElement("font");
  55. setFont(cache->getResource<Font>(fontElem.getString("name")), fontElem.getInt("size"));
  56. }
  57. if (element.hasChildElement("maxwidth"))
  58. setMaxWidth(element.getChildElement("maxwidth").getInt("value"));
  59. if (element.hasChildElement("text"))
  60. {
  61. std::string text = element.getChildElement("text").getString("value");
  62. replaceInPlace(text, "\\n", "\n");
  63. setText(text);
  64. }
  65. if (element.hasChildElement("textalignment"))
  66. {
  67. std::string horiz = element.getChildElement("textalignment").getStringLower("value");
  68. if (horiz == "left")
  69. setTextAlignment(HA_LEFT);
  70. if (horiz == "center")
  71. setTextAlignment(HA_CENTER);
  72. if (horiz == "right")
  73. setTextAlignment(HA_RIGHT);
  74. }
  75. if (element.hasChildElement("rowspacing"))
  76. setRowSpacing(element.getChildElement("rowspacing").getFloat("value"));
  77. if (element.hasChildElement("selection"))
  78. {
  79. XMLElement selectionElem = element.getChildElement("selection");
  80. setSelection(selectionElem.getInt("start"), selectionElem.getInt("length"));
  81. }
  82. if (element.hasChildElement("selectioncolor"))
  83. setSelectionColor(element.getChildElement("selectioncolor").getColor("value"));
  84. if (element.hasChildElement("hovercolor"))
  85. setHoverColor(element.getChildElement("hovercolor").getColor("value"));
  86. }
  87. bool Text::setFont(Font* font, int size)
  88. {
  89. if (!font)
  90. {
  91. LOGERROR("Null font for Text");
  92. return false;
  93. }
  94. if ((font != mFont) || (size != mFontSize))
  95. {
  96. mFont = font;
  97. mFontSize = max(size, 1);
  98. updateText();
  99. }
  100. return true;
  101. }
  102. void Text::setMaxWidth(int maxWidth)
  103. {
  104. if (maxWidth != mMaxWidth)
  105. {
  106. mMaxWidth = max(maxWidth, 0);
  107. updateText();
  108. }
  109. }
  110. void Text::setText(const std::string& text)
  111. {
  112. mText = text;
  113. validateSelection();
  114. updateText();
  115. }
  116. void Text::setTextAlignment(HorizontalAlignment align)
  117. {
  118. if (align != mTextAlignment)
  119. {
  120. mTextAlignment = align;
  121. updateText();
  122. }
  123. }
  124. void Text::setRowSpacing(float spacing)
  125. {
  126. if (spacing != mRowSpacing)
  127. {
  128. mRowSpacing = max(spacing, 0.5f);
  129. updateText();
  130. }
  131. }
  132. void Text::setSelection(unsigned start, unsigned length)
  133. {
  134. mSelectionStart = start;
  135. mSelectionLength = length;
  136. validateSelection();
  137. }
  138. void Text::setSelectionColor(const Color& color)
  139. {
  140. mSelectionColor = color;
  141. }
  142. void Text::setHoverColor(const Color& color)
  143. {
  144. mHoverColor = color;
  145. }
  146. void Text::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  147. {
  148. // Hovering batch
  149. if (((mHovering) || (mSelected)) && (mHoverColor.mA > 0.0f))
  150. {
  151. UIBatch batch;
  152. batch.begin(&quads);
  153. batch.mBlendMode = BLEND_ALPHA;
  154. batch.mScissor = currentScissor;
  155. batch.mTexture = 0;
  156. batch.addQuad(*this, 0, 0, getWidth(), getHeight(), 0, 0, 0, 0, mHoverColor);
  157. UIBatch::addOrMerge(batch, batches);
  158. }
  159. // Selection batch
  160. if ((mSelectionLength) && (mCharSizes.size() >= mSelectionStart + mSelectionLength) && (mSelectionColor.mA > 0.0f))
  161. {
  162. UIBatch batch;
  163. batch.begin(&quads);
  164. batch.mBlendMode = BLEND_ALPHA;
  165. batch.mScissor = currentScissor;
  166. batch.mTexture = 0;
  167. IntVector2 currentStart = mCharPositions[mSelectionStart];
  168. IntVector2 currentEnd = currentStart;
  169. for (unsigned i = mSelectionStart; i < mSelectionStart + mSelectionLength; ++i)
  170. {
  171. // Check if row changes, and start a new quad in that case
  172. if ((mCharSizes[i].mX) && (mCharSizes[i].mY))
  173. {
  174. if (mCharPositions[i].mY != currentStart.mY)
  175. {
  176. batch.addQuad(*this, currentStart.mX, currentStart.mY, currentEnd.mX - currentStart.mX, currentEnd.mY - currentStart.mY,
  177. 0, 0, 0, 0, mSelectionColor);
  178. currentStart = mCharPositions[i];
  179. currentEnd = currentStart + mCharSizes[i];
  180. }
  181. else
  182. {
  183. currentEnd.mX += mCharSizes[i].mX;
  184. currentEnd.mY = max(currentStart.mY + mCharSizes[i].mY, currentEnd.mY);
  185. }
  186. }
  187. }
  188. if (currentEnd != currentStart)
  189. {
  190. batch.addQuad(*this, currentStart.mX, currentStart.mY, currentEnd.mX - currentStart.mX, currentEnd.mY - currentStart.mY,
  191. 0, 0, 0, 0, mSelectionColor);
  192. }
  193. UIBatch::addOrMerge(batch, batches);
  194. }
  195. // Text batch
  196. if (mFont)
  197. {
  198. const FontFace* face = mFont->getFace(mFontSize);
  199. UIBatch batch;
  200. batch.begin(&quads);
  201. batch.mBlendMode = BLEND_ALPHA;
  202. batch.mScissor = currentScissor;
  203. batch.mTexture = face->mTexture;
  204. unsigned rowIndex = 0;
  205. int x = getRowStartPosition(rowIndex);
  206. int y = 0;
  207. for (unsigned i = 0; i < mPrintText.length(); ++i)
  208. {
  209. unsigned char c = (unsigned char)mPrintText[i];
  210. if (c != '\n')
  211. {
  212. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  213. if (c != ' ')
  214. batch.addQuad(*this, x + glyph.mOffsetX, y + glyph.mOffsetY, glyph.mWidth, glyph.mHeight, glyph.mX, glyph.mY);
  215. x += glyph.mAdvanceX;
  216. }
  217. else
  218. {
  219. rowIndex++;
  220. x = getRowStartPosition(rowIndex);
  221. y += mRowHeight;
  222. }
  223. }
  224. UIBatch::addOrMerge(batch, batches);
  225. }
  226. // Reset hovering for next frame
  227. mHovering = false;
  228. }
  229. void Text::updateText()
  230. {
  231. int width = 0;
  232. int height = 0;
  233. mRowWidths.clear();
  234. mPrintText.clear();
  235. static std::vector<unsigned> printToText;
  236. printToText.clear();
  237. if (mFont)
  238. {
  239. const FontFace* face = mFont->getFace(mFontSize);
  240. mRowHeight = face->mRowHeight;
  241. int rowWidth = 0;
  242. int rowHeight = (int)(mRowSpacing * mRowHeight);
  243. // First see if the text must be split up
  244. if (!mMaxWidth)
  245. {
  246. mPrintText = mText;
  247. printToText.resize(mText.length());
  248. for (unsigned i = 0; i < mText.length(); ++i)
  249. printToText[i] = i;
  250. }
  251. else
  252. {
  253. unsigned nextBreak = 0;
  254. unsigned lineStart = 0;
  255. for (unsigned i = 0; i < mText.length(); ++i)
  256. {
  257. unsigned j;
  258. if (mText[i] != '\n')
  259. {
  260. bool ok = true;
  261. if (nextBreak <= i)
  262. {
  263. int futureRowWidth = rowWidth;
  264. for (j = i; j < mText.length(); ++j)
  265. {
  266. if ((mText[j] == ' ') || (mText[j] == '\n'))
  267. {
  268. nextBreak = j;
  269. break;
  270. }
  271. futureRowWidth += face->mGlyphs[face->mGlyphIndex[mText[j]]].mAdvanceX;
  272. if ((mText[j] == '-') && (futureRowWidth <= mMaxWidth))
  273. {
  274. nextBreak = j + 1;
  275. break;
  276. }
  277. if (futureRowWidth > mMaxWidth)
  278. {
  279. ok = false;
  280. break;
  281. }
  282. }
  283. }
  284. if (!ok)
  285. {
  286. // If did not find any breaks on the line, copy until j, or at least 1 char, to prevent infinite loop
  287. if (nextBreak == lineStart)
  288. {
  289. int copyLength = max(j - i, 1);
  290. mPrintText.append(mText.substr(i, copyLength));
  291. for (int k = 0; k < copyLength; ++k)
  292. {
  293. printToText.push_back(i);
  294. ++i;
  295. }
  296. }
  297. mPrintText += '\n';
  298. printToText.push_back(i);
  299. rowWidth = 0;
  300. nextBreak = lineStart = i;
  301. }
  302. if (i < mText.length())
  303. {
  304. // When copying a space, we may be over row width
  305. rowWidth += face->mGlyphs[face->mGlyphIndex[mText[i]]].mAdvanceX;
  306. if (rowWidth <= mMaxWidth)
  307. {
  308. mPrintText += mText[i];
  309. printToText.push_back(i);
  310. }
  311. }
  312. }
  313. else
  314. {
  315. mPrintText += '\n';
  316. printToText.push_back(i);
  317. rowWidth = 0;
  318. nextBreak = lineStart = i;
  319. }
  320. }
  321. }
  322. rowWidth = 0;
  323. for (unsigned i = 0; i < mPrintText.length(); ++i)
  324. {
  325. unsigned char c = (unsigned char)mPrintText[i];
  326. if (c != '\n')
  327. {
  328. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  329. rowWidth += glyph.mAdvanceX;
  330. }
  331. else
  332. {
  333. width = max(width, rowWidth);
  334. height += rowHeight;
  335. mRowWidths.push_back(rowWidth);
  336. rowWidth = 0;
  337. }
  338. }
  339. if (rowWidth)
  340. {
  341. width = max(width, rowWidth);
  342. height += rowHeight;
  343. mRowWidths.push_back(rowWidth);
  344. }
  345. // Set row height even if text is empty
  346. if (!height)
  347. height = rowHeight;
  348. // Store position & size of each character
  349. mCharPositions.resize(mText.length() + 1);
  350. mCharSizes.resize(mText.length());
  351. unsigned rowIndex = 0;
  352. int x = getRowStartPosition(rowIndex);
  353. int y = 0;
  354. for (unsigned i = 0; i < mPrintText.length(); ++i)
  355. {
  356. mCharPositions[printToText[i]] = IntVector2(x, y);
  357. unsigned char c = (unsigned char)mPrintText[i];
  358. if (c != '\n')
  359. {
  360. const FontGlyph& glyph = face->mGlyphs[face->mGlyphIndex[c]];
  361. mCharSizes[printToText[i]] = IntVector2(glyph.mAdvanceX, mRowHeight);
  362. x += glyph.mAdvanceX;
  363. }
  364. else
  365. {
  366. mCharSizes[printToText[i]] = IntVector2::sZero;
  367. rowIndex++;
  368. x = getRowStartPosition(rowIndex);
  369. y += rowHeight;
  370. }
  371. }
  372. // Store the ending position
  373. mCharPositions[mText.length()] = IntVector2(x, y);
  374. }
  375. // If maxwidth is nonzero, fix the element width
  376. if (mMaxWidth)
  377. width = mMaxWidth;
  378. setSize(width, height);
  379. }
  380. void Text::validateSelection()
  381. {
  382. unsigned textLength = mText.length();
  383. if (textLength)
  384. {
  385. if (mSelectionStart >= textLength)
  386. mSelectionStart = textLength - 1;
  387. if (mSelectionStart + mSelectionLength > textLength)
  388. mSelectionLength = textLength - mSelectionStart;
  389. }
  390. else
  391. {
  392. mSelectionStart = 0;
  393. mSelectionLength = 0;
  394. }
  395. }
  396. int Text::getRowStartPosition(unsigned rowIndex) const
  397. {
  398. int rowWidth = 0;
  399. if (rowIndex < mRowWidths.size())
  400. rowWidth = mRowWidths[rowIndex];
  401. switch (mTextAlignment)
  402. {
  403. default:
  404. return 0;
  405. case HA_CENTER:
  406. return (getSize().mX - rowWidth) / 2;
  407. case HA_RIGHT:
  408. return getSize().mX - rowWidth;
  409. }
  410. }