UIText.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "Font.h"
  25. #include "FontFace.h"
  26. #include "Log.h"
  27. #include "Material.h"
  28. #include "Node.h"
  29. #include "Profiler.h"
  30. #include "ResourceCache.h"
  31. #include "Technique.h"
  32. #include "Texture2D.h"
  33. #include "UIRect.h"
  34. #include "UIText.h"
  35. #include "UIXEvents.h"
  36. #include "DebugNew.h"
  37. namespace Urho3D
  38. {
  39. static const float MIN_ROW_SPACING = 0.5f;
  40. extern const char* textEffects[];
  41. extern const char* horizontalAlignments[];
  42. extern const char* UIX_CATEGORY;
  43. void UITextLineInfo::Clear()
  44. {
  45. lineWidth_ = 0.0f;
  46. fontGlyphs_.Clear();
  47. glyphAdvances_.Clear();
  48. }
  49. void UITextLineInfo::Add(const FontGlyph* glyph, short kerning)
  50. {
  51. fontGlyphs_.Push(glyph);
  52. float advance = (glyph->advanceX_ + kerning) * PIXEL_SIZE;
  53. lineWidth_ += advance;
  54. glyphAdvances_.Push(advance);
  55. }
  56. UIText::UIText(Context* context) :
  57. Drawable2D(context),
  58. usedInText3D_(false),
  59. fontSize_(DEFAULT_FONT_SIZE),
  60. textAlignment_(HA_LEFT),
  61. rowSpacing_(1.0f),
  62. color_(Color::WHITE),
  63. selectionStart_(0),
  64. selectionLength_(0),
  65. selectionColor_(Color::TRANSPARENT),
  66. textEffect_(TE_NONE),
  67. effectColor_(Color::BLACK),
  68. rowHeight_(0.0f),
  69. numChars_(0)
  70. {
  71. }
  72. UIText::~UIText()
  73. {
  74. }
  75. void UIText::RegisterObject(Context* context)
  76. {
  77. context->RegisterFactory<UIText>(UIX_CATEGORY);
  78. MIXED_ACCESSOR_ATTRIBUTE("Font", GetFontAttr, SetFontAttr, ResourceRef, ResourceRef(Font::GetTypeStatic()), AM_FILE);
  79. ACCESSOR_ATTRIBUTE("Font Size", GetFontSize, SetFontSize, int, DEFAULT_FONT_SIZE, AM_FILE);
  80. ACCESSOR_ATTRIBUTE("Text", GetText, SetText, String, String::EMPTY, AM_FILE);
  81. ENUM_ACCESSOR_ATTRIBUTE("Text Alignment", GetTextAlignment, SetTextAlignment, HorizontalAlignment, horizontalAlignments, HA_LEFT, AM_FILE);
  82. ACCESSOR_ATTRIBUTE("Row Spacing", GetRowSpacing, SetRowSpacing, float, 1.0f, AM_FILE);
  83. ACCESSOR_ATTRIBUTE("Color", GetColor, SetColor, Color, Color::WHITE, AM_FILE);
  84. ACCESSOR_ATTRIBUTE("Selection Color", GetSelectionColor, SetSelectionColor, Color, Color::TRANSPARENT, AM_FILE);
  85. ENUM_ACCESSOR_ATTRIBUTE("Text Effect", GetTextEffect, SetTextEffect, TextEffect, textEffects, TE_NONE, AM_FILE);
  86. ACCESSOR_ATTRIBUTE("Effect Color", GetEffectColor, SetEffectColor, Color, Color::BLACK, AM_FILE);
  87. COPY_BASE_ATTRIBUTES(Drawable2D);
  88. }
  89. bool UIText::SetFont(const String& fontName, int size)
  90. {
  91. ResourceCache* cache = GetSubsystem<ResourceCache>();
  92. return SetFont(cache->GetResource<Font>(fontName), size);
  93. }
  94. bool UIText::SetFont(Font* font, int size)
  95. {
  96. if (!font)
  97. {
  98. LOGERROR("Null font for Text");
  99. return false;
  100. }
  101. if (font != font_ || size != fontSize_)
  102. {
  103. font_ = font;
  104. fontSize_ = Max(size, 1);
  105. if (font_)
  106. {
  107. fontFace_ = font->GetFace(fontSize_);
  108. SetTexture(fontFace_->GetTextures()[0]);
  109. UpdateMaterial(true);
  110. }
  111. else
  112. {
  113. SetTexture(0);
  114. SetCustomMaterial(0);
  115. }
  116. UpdateText();
  117. verticesDirty_ = true;
  118. }
  119. return true;
  120. }
  121. void UIText::SetFontSize(int fontSize)
  122. {
  123. SetFont(font_, fontSize);
  124. }
  125. void UIText::SetText(const String& text)
  126. {
  127. if (text == text_)
  128. return;
  129. text_ = text;
  130. UpdateText();
  131. ValidateSelection();
  132. verticesDirty_ = true;
  133. }
  134. void UIText::SetTextAlignment(HorizontalAlignment align)
  135. {
  136. if (align == textAlignment_)
  137. return;
  138. textAlignment_ = align;
  139. verticesDirty_ = true;
  140. }
  141. void UIText::SetRowSpacing(float spacing)
  142. {
  143. if (spacing == rowSpacing_)
  144. return;
  145. rowSpacing_ = Max(spacing, MIN_ROW_SPACING);
  146. verticesDirty_ = true;
  147. }
  148. void UIText::SetColor(const Color& color)
  149. {
  150. if (color == color_)
  151. return;
  152. color_ = color;
  153. verticesDirty_ = true;
  154. }
  155. void UIText::SetSelection(unsigned start, unsigned length)
  156. {
  157. if (start == selectionStart_ && length == selectionLength_)
  158. return;
  159. selectionStart_ = start;
  160. selectionLength_ = length;
  161. ValidateSelection();
  162. verticesDirty_ = true;
  163. }
  164. void UIText::ClearSelection()
  165. {
  166. selectionStart_ = 0;
  167. if (selectionLength_ != 0)
  168. {
  169. selectionLength_ = 0;
  170. verticesDirty_ = true;
  171. }
  172. }
  173. void UIText::SetSelectionColor(const Color& color)
  174. {
  175. if (color == selectionColor_)
  176. return;
  177. selectionColor_ = color;
  178. if (selectionLength_ != 0)
  179. verticesDirty_ = true;
  180. }
  181. void UIText::SetTextEffect(TextEffect textEffect)
  182. {
  183. if (textEffect == textEffect_)
  184. return;
  185. textEffect_ = textEffect;
  186. UpdateMaterial();
  187. verticesDirty_ = true;
  188. }
  189. void UIText::SetEffectColor(const Color& effectColor)
  190. {
  191. if (effectColor == effectColor_)
  192. return;
  193. effectColor_ = effectColor;
  194. if (textEffect_ != TE_NONE)
  195. verticesDirty_ = true;
  196. }
  197. void UIText::SetFontAttr(const ResourceRef& value)
  198. {
  199. ResourceCache* cache = GetSubsystem<ResourceCache>();
  200. SetFont(cache->GetResource<Font>(value.name_), fontSize_);
  201. }
  202. ResourceRef UIText::GetFontAttr() const
  203. {
  204. return GetResourceRef(font_, Font::GetTypeStatic());
  205. }
  206. void UIText::OnNodeSet(Node* node)
  207. {
  208. Drawable2D::OnNodeSet(node);
  209. if (node)
  210. {
  211. uiRect_ = node->GetComponent<UIRect>();
  212. if (uiRect_)
  213. SubscribeToEvent(uiRect_, E_UIRECTDIRTIED, HANDLER(UIText, HandleRectDirtied));
  214. else
  215. LOGERROR("UIRect must by added first");
  216. }
  217. }
  218. void UIText::OnWorldBoundingBoxUpdate()
  219. {
  220. boundingBox_.Clear();
  221. if (uiRect_)
  222. {
  223. const Rect& rect = uiRect_->GetRect();
  224. boundingBox_.Merge(rect.min_);
  225. boundingBox_.Merge(rect.max_);
  226. }
  227. worldBoundingBox_ = boundingBox_;
  228. }
  229. void UIText::UpdateVertices()
  230. {
  231. if (!verticesDirty_)
  232. return;
  233. vertices_.Clear();
  234. if (textLineInfo_.Empty())
  235. return;
  236. TextEffect textEffect = font_->IsSDFFont() ? TE_NONE : textEffect_;
  237. const Vector<SharedPtr<Texture2D> >& textures = fontFace_->GetTextures();
  238. const Rect& rect = uiRect_->GetRect();
  239. float totalHeight = textLineInfo_.Size() * rowHeight_;
  240. unsigned color = color_.ToUInt();
  241. unsigned effectColor = effectColor_.ToUInt();
  242. for (unsigned i = 0; i < textLineInfo_.Size(); ++i)
  243. {
  244. const UITextLineInfo& lineInfo = textLineInfo_[i];
  245. float x;
  246. switch (textAlignment_)
  247. {
  248. case HA_LEFT:
  249. x = rect.min_.x_;
  250. break;
  251. case HA_CENTER:
  252. x = rect.Center().x_ - lineInfo.lineWidth_ * 0.5f;
  253. break;
  254. case HA_RIGHT:
  255. x = rect.max_.x_ - lineInfo.lineWidth_;
  256. break;
  257. }
  258. float y = rect.Center().y_ + totalHeight * 0.5f - rowHeight_ * i;
  259. switch (textEffect)
  260. {
  261. case TE_NONE:
  262. UpdateTextLineVertices(lineInfo, x, y, color);
  263. break;
  264. case TE_SHADOW:
  265. UpdateTextLineVertices(lineInfo, x + PIXEL_SIZE, y - PIXEL_SIZE, effectColor);
  266. UpdateTextLineVertices(lineInfo, x, y, color);
  267. break;
  268. case TE_STROKE:
  269. UpdateTextLineVertices(lineInfo, x - PIXEL_SIZE, y - PIXEL_SIZE, effectColor);
  270. UpdateTextLineVertices(lineInfo, x , y - PIXEL_SIZE, effectColor);
  271. UpdateTextLineVertices(lineInfo, x + PIXEL_SIZE, y - PIXEL_SIZE, effectColor);
  272. UpdateTextLineVertices(lineInfo, x - PIXEL_SIZE, y, effectColor);
  273. UpdateTextLineVertices(lineInfo, x + PIXEL_SIZE, y, effectColor);
  274. UpdateTextLineVertices(lineInfo, x - PIXEL_SIZE, y + PIXEL_SIZE, effectColor);
  275. UpdateTextLineVertices(lineInfo, x , y + PIXEL_SIZE, effectColor);
  276. UpdateTextLineVertices(lineInfo, x + PIXEL_SIZE, y + PIXEL_SIZE, effectColor);
  277. UpdateTextLineVertices(lineInfo, x, y, color);
  278. break;
  279. }
  280. }
  281. verticesDirty_ = false;
  282. }
  283. void UIText::HandleRectDirtied(StringHash eventType, VariantMap& eventData)
  284. {
  285. worldBoundingBoxDirty_ = true;
  286. verticesDirty_ = true;
  287. }
  288. void UIText::UpdateMaterial(bool newFont)
  289. {
  290. Material* material = GetCustomMaterial();
  291. if (!material)
  292. {
  293. material = new Material(context_);
  294. Technique* tech = new Technique(context_);
  295. Pass* pass = tech->CreatePass(PASS_ALPHA);
  296. pass->SetVertexShader("Text");
  297. pass->SetPixelShader("Text");
  298. pass->SetDepthWrite(false);
  299. // Text alway use alpha blend mode
  300. pass->SetBlendMode(BLEND_ALPHA);
  301. material->SetTechnique(0, tech);
  302. material->SetCullMode(CULL_NONE);
  303. SetCustomMaterial(material);
  304. }
  305. material->SetTexture(TU_DIFFUSE, texture_);
  306. if (newFont)
  307. {
  308. Technique* tech = material->GetTechnique(0);
  309. Pass* pass = tech->GetPass(PASS_ALPHA);
  310. if (font_ && font_->IsSDFFont())
  311. {
  312. switch (textEffect_)
  313. {
  314. case TE_NONE:
  315. pass->SetPixelShaderDefines("SIGNED_DISTANCE_FIELD");
  316. break;
  317. case TE_SHADOW:
  318. pass->SetPixelShaderDefines("SIGNED_DISTANCE_FIELD TEXT_EFFECT_SHADOW");
  319. break;
  320. case TE_STROKE:
  321. pass->SetPixelShaderDefines("SIGNED_DISTANCE_FIELD TEXT_EFFECT_STROKE");
  322. break;
  323. }
  324. }
  325. else
  326. pass->SetPixelShaderDefines("");
  327. }
  328. }
  329. void UIText::UpdateText()
  330. {
  331. textLineInfo_.Clear();
  332. // Convert to Unicode text
  333. PODVector<unsigned> unicodeText;
  334. for (unsigned i = 0; i < text_.Length();)
  335. unicodeText.Push(text_.NextUTF8Char(i));
  336. numChars_ = unicodeText.Size();
  337. if (!fontFace_)
  338. return;
  339. rowHeight_ = fontFace_->GetRowHeight() * PIXEL_SIZE;
  340. UITextLineInfo lineInfo;
  341. for (unsigned i = 0; i < unicodeText.Size(); ++i)
  342. {
  343. unsigned c = unicodeText[i];
  344. if (c != '\n')
  345. {
  346. const FontGlyph* glyph = fontFace_->GetGlyph(c);
  347. if (glyph)
  348. lineInfo.Add(glyph, i < unicodeText.Size() - 1 ? fontFace_->GetKerning(c, unicodeText[i + 1]) : 0);
  349. }
  350. else
  351. {
  352. textLineInfo_.Push(lineInfo);
  353. lineInfo.Clear();
  354. }
  355. }
  356. if (!lineInfo.fontGlyphs_.Empty())
  357. textLineInfo_.Push(lineInfo);
  358. }
  359. void UIText::ValidateSelection()
  360. {
  361. if (numChars_)
  362. {
  363. if (selectionStart_ >= numChars_)
  364. selectionStart_ = numChars_ - 1;
  365. if (selectionStart_ + selectionLength_ > numChars_)
  366. selectionLength_ = numChars_ - selectionStart_;
  367. }
  368. else
  369. {
  370. selectionStart_ = 0;
  371. selectionLength_ = 0;
  372. }
  373. }
  374. void UIText::UpdateTextLineVertices(const UITextLineInfo& lineInfo, float x, float y, unsigned color)
  375. {
  376. const PODVector<float>& advances = lineInfo.glyphAdvances_;
  377. const PODVector<const FontGlyph*>& fontGlyphs = lineInfo.fontGlyphs_;
  378. float invTexWidth = 1.0f / (float)texture_->GetWidth();
  379. float invTexHeight = 1.0f / (float)texture_->GetHeight();
  380. for (unsigned i = 0; i < fontGlyphs.Size(); ++i)
  381. {
  382. const FontGlyph& glyph = *fontGlyphs[i];
  383. /*
  384. V1---------V2
  385. | / |
  386. | / |
  387. | / |
  388. | / |
  389. | / |
  390. V0---------V3
  391. */
  392. Vertex2D vertex0;
  393. Vertex2D vertex1;
  394. Vertex2D vertex2;
  395. Vertex2D vertex3;
  396. float left = x + glyph.offsetX_ * PIXEL_SIZE;
  397. float right = left + glyph.width_ * PIXEL_SIZE;
  398. float top = y - glyph.offsetY_ * PIXEL_SIZE;
  399. float bottom = top - glyph.height_ * PIXEL_SIZE;
  400. float uLeft = glyph.x_ * invTexWidth;
  401. float uRight = (glyph.x_ + glyph.width_) * invTexWidth;
  402. float vTop = glyph.y_ * invTexHeight;
  403. float vBottom = (glyph.y_ + glyph.height_) * invTexHeight;
  404. vertex0.position_ = Vector3(left, bottom);
  405. vertex1.position_ = Vector3(left, top);
  406. vertex2.position_ = Vector3(right, top);
  407. vertex3.position_ = Vector3(right, bottom);
  408. vertex0.uv_ = Vector2(uLeft, vBottom);
  409. vertex1.uv_ = Vector2(uLeft, vTop);
  410. vertex2.uv_ = Vector2(uRight, vTop);
  411. vertex3.uv_ = Vector2(uRight, vBottom);
  412. vertex0.color_ = color;
  413. vertex1.color_ = color;
  414. vertex2.color_ = color;
  415. vertex3.color_ = color;
  416. vertices_.Push(vertex0);
  417. vertices_.Push(vertex1);
  418. vertices_.Push(vertex2);
  419. vertices_.Push(vertex3);
  420. x += advances[i];
  421. }
  422. }
  423. }