Text3D.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. //
  2. // Copyright (c) 2008-2013 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 "Camera.h"
  24. #include "Context.h"
  25. #include "Font.h"
  26. #include "Geometry.h"
  27. #include "Log.h"
  28. #include "Material.h"
  29. #include "Node.h"
  30. #include "ResourceCache.h"
  31. #include "Technique.h"
  32. #include "Text.h"
  33. #include "Text3D.h"
  34. #include "VertexBuffer.h"
  35. namespace Urho3D
  36. {
  37. extern const char* horizontalAlignments[];
  38. extern const char* verticalAlignments[];
  39. extern const char* textEffects[];
  40. extern const char* GEOMETRY_CATEGORY;
  41. static const float TEXT_SCALING = 1.0f / 128.0f;
  42. Text3D::Text3D(Context* context) :
  43. Drawable(context, DRAWABLE_GEOMETRY),
  44. text_(context),
  45. vertexBuffer_(new VertexBuffer(context_)),
  46. customWorldTransform_(Matrix3x4::IDENTITY),
  47. faceCamera_(false),
  48. textDirty_(true),
  49. geometryDirty_(true)
  50. {
  51. }
  52. Text3D::~Text3D()
  53. {
  54. }
  55. void Text3D::RegisterObject(Context* context)
  56. {
  57. context->RegisterFactory<Text3D>(GEOMETRY_CATEGORY);
  58. ACCESSOR_ATTRIBUTE(Text3D, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  59. ACCESSOR_ATTRIBUTE(Text3D, VAR_RESOURCEREF, "Font", GetFontAttr, SetFontAttr, ResourceRef, ResourceRef(Font::GetTypeStatic()), AM_DEFAULT);
  60. ACCESSOR_ATTRIBUTE(Text3D, VAR_RESOURCEREF, "Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
  61. ATTRIBUTE(Text3D, VAR_INT, "Font Size", text_.fontSize_, DEFAULT_FONT_SIZE, AM_DEFAULT);
  62. ATTRIBUTE(Text3D, VAR_STRING, "Text", text_.text_, String::EMPTY, AM_DEFAULT);
  63. ENUM_ATTRIBUTE(Text3D, "Text Alignment", text_.textAlignment_, horizontalAlignments, HA_LEFT, AM_DEFAULT);
  64. ATTRIBUTE(Text3D, VAR_FLOAT, "Row Spacing", text_.rowSpacing_, 1.0f, AM_DEFAULT);
  65. ATTRIBUTE(Text3D, VAR_BOOL, "Word Wrap", text_.wordWrap_, false, AM_DEFAULT);
  66. ENUM_ATTRIBUTE(Text3D, "Text Effect", text_.textEffect_, textEffects, TE_NONE, AM_FILE);
  67. REF_ACCESSOR_ATTRIBUTE(Text3D, VAR_COLOR, "Effect Color", GetEffectColor, SetEffectColor, Color, Color::TRANSPARENT, AM_FILE);
  68. ACCESSOR_ATTRIBUTE(Text3D, VAR_INT, "Width", GetWidth, SetWidth, int, 0, AM_DEFAULT);
  69. ENUM_ACCESSOR_ATTRIBUTE(Text3D, "Horiz Alignment", GetHorizontalAlignment, SetHorizontalAlignment, HorizontalAlignment, horizontalAlignments, HA_LEFT, AM_DEFAULT);
  70. ENUM_ACCESSOR_ATTRIBUTE(Text3D, "Vert Alignment", GetVerticalAlignment, SetVerticalAlignment, VerticalAlignment, verticalAlignments, VA_TOP, AM_DEFAULT);
  71. REF_ACCESSOR_ATTRIBUTE(Text3D, VAR_COLOR, "Color", GetColorAttr, SetColor, Color, Color::WHITE, AM_DEFAULT);
  72. ATTRIBUTE(Text3D, VAR_COLOR, "Top Left Color", text_.color_[0], Color::WHITE, AM_DEFAULT);
  73. ATTRIBUTE(Text3D, VAR_COLOR, "Top Right Color", text_.color_[1], Color::WHITE, AM_DEFAULT);
  74. ATTRIBUTE(Text3D, VAR_COLOR, "Bottom Left Color", text_.color_[2], Color::WHITE, AM_DEFAULT);
  75. ATTRIBUTE(Text3D, VAR_COLOR, "Bottom Right Color", text_.color_[3], Color::WHITE, AM_DEFAULT);
  76. ACCESSOR_ATTRIBUTE(Text3D, VAR_BOOL, "Can Be Occluded", IsOccludee, SetOccludee, bool, true, AM_DEFAULT);
  77. ACCESSOR_ATTRIBUTE(Text3D, VAR_BOOL, "Face Camera", GetFaceCamera, SetFaceCamera, bool, false, AM_DEFAULT);
  78. ACCESSOR_ATTRIBUTE(Text3D, VAR_FLOAT, "Draw Distance", GetDrawDistance, SetDrawDistance, float, 0.0f, AM_DEFAULT);
  79. COPY_BASE_ATTRIBUTES(Text3D, Drawable);
  80. }
  81. void Text3D::ApplyAttributes()
  82. {
  83. text_.ApplyAttributes();
  84. MarkTextDirty();
  85. UpdateTextBatches();
  86. UpdateTextMaterials();
  87. }
  88. void Text3D::UpdateBatches(const FrameInfo& frame)
  89. {
  90. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  91. distance_ = frame.camera_->GetDistance(GetWorldBoundingBox().Center());
  92. if (faceCamera_)
  93. {
  94. customWorldTransform_ = Matrix3x4(node_->GetWorldPosition(), frame.camera_->GetNode()->GetWorldRotation(), node_->GetWorldScale());
  95. worldBoundingBoxDirty_ = true;
  96. }
  97. for (unsigned i = 0; i < batches_.Size(); ++i)
  98. {
  99. batches_[i].distance_ = distance_;
  100. batches_[i].worldTransform_ = faceCamera_ ? &customWorldTransform_ : &worldTransform;
  101. }
  102. }
  103. void Text3D::UpdateGeometry(const FrameInfo& frame)
  104. {
  105. if (geometryDirty_)
  106. {
  107. for (unsigned i = 0; i < batches_.Size(); ++i)
  108. {
  109. Geometry* geometry = geometries_[i];
  110. geometry->SetDrawRange(TRIANGLE_LIST, 0, 0, uiBatches_[i].vertexStart_, (uiBatches_[i].vertexEnd_ -
  111. uiBatches_[i].vertexStart_) / UI_VERTEX_SIZE);
  112. }
  113. }
  114. if ((geometryDirty_ || vertexBuffer_->IsDataLost()) && uiVertexData_.Size())
  115. {
  116. unsigned vertexCount = uiVertexData_.Size() / UI_VERTEX_SIZE;
  117. if (vertexBuffer_->GetVertexCount() != vertexCount)
  118. vertexBuffer_->SetSize(vertexCount, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1);
  119. vertexBuffer_->SetData(&uiVertexData_[0]);
  120. }
  121. geometryDirty_ = false;
  122. }
  123. UpdateGeometryType Text3D::GetUpdateGeometryType()
  124. {
  125. if (geometryDirty_ || vertexBuffer_->IsDataLost())
  126. return UPDATE_MAIN_THREAD;
  127. else
  128. return UPDATE_NONE;
  129. }
  130. void Text3D::SetMaterial(Material* material)
  131. {
  132. material_ = material;
  133. UpdateTextMaterials(true);
  134. }
  135. bool Text3D::SetFont(const String& fontName, int size)
  136. {
  137. bool success = text_.SetFont(fontName, size);
  138. // Changing font requires materials to be re-evaluated. Material evaluation can not be done in worker threads,
  139. // so UI batches must be brought up-to-date immediately
  140. MarkTextDirty();
  141. UpdateTextBatches();
  142. UpdateTextMaterials();
  143. return success;
  144. }
  145. bool Text3D::SetFont(Font* font, int size)
  146. {
  147. bool success = text_.SetFont(font, size);
  148. MarkTextDirty();
  149. UpdateTextBatches();
  150. UpdateTextMaterials();
  151. return success;
  152. }
  153. void Text3D::SetText(const String& text)
  154. {
  155. text_.SetText(text);
  156. // Changing text requires materials to be re-evaluated, in case the font is multi-page
  157. MarkTextDirty();
  158. UpdateTextBatches();
  159. UpdateTextMaterials();
  160. }
  161. void Text3D::SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign)
  162. {
  163. text_.SetAlignment(hAlign, vAlign);
  164. MarkTextDirty();
  165. }
  166. void Text3D::SetHorizontalAlignment(HorizontalAlignment align)
  167. {
  168. text_.SetHorizontalAlignment(align);
  169. MarkTextDirty();
  170. }
  171. void Text3D::SetVerticalAlignment(VerticalAlignment align)
  172. {
  173. text_.SetVerticalAlignment(align);
  174. MarkTextDirty();
  175. }
  176. void Text3D::SetTextAlignment(HorizontalAlignment align)
  177. {
  178. text_.SetTextAlignment(align);
  179. MarkTextDirty();
  180. }
  181. void Text3D::SetRowSpacing(float spacing)
  182. {
  183. text_.SetRowSpacing(spacing);
  184. MarkTextDirty();
  185. }
  186. void Text3D::SetWordwrap(bool enable)
  187. {
  188. text_.SetWordwrap(enable);
  189. MarkTextDirty();
  190. }
  191. void Text3D::SetTextEffect(TextEffect textEffect)
  192. {
  193. text_.SetTextEffect(textEffect);
  194. }
  195. void Text3D::SetEffectColor(const Color& effectColor)
  196. {
  197. text_.SetEffectColor(effectColor);
  198. }
  199. void Text3D::SetWidth(int width)
  200. {
  201. text_.SetMinWidth(width);
  202. text_.SetWidth(width);
  203. MarkTextDirty();
  204. }
  205. void Text3D::SetColor(const Color& color)
  206. {
  207. text_.SetColor(color);
  208. MarkTextDirty();
  209. }
  210. void Text3D::SetColor(Corner corner, const Color& color)
  211. {
  212. text_.SetColor(corner, color);
  213. MarkTextDirty();
  214. }
  215. void Text3D::SetOpacity(float opacity)
  216. {
  217. text_.SetOpacity(opacity);
  218. MarkTextDirty();
  219. }
  220. void Text3D::SetFaceCamera(bool enable)
  221. {
  222. if (enable != faceCamera_)
  223. {
  224. faceCamera_ = enable;
  225. // Bounding box must be recalculated
  226. OnMarkedDirty(node_);
  227. }
  228. }
  229. Material* Text3D::GetMaterial() const
  230. {
  231. return material_;
  232. }
  233. Font* Text3D::GetFont() const
  234. {
  235. return text_.GetFont();
  236. }
  237. int Text3D::GetFontSize() const
  238. {
  239. return text_.GetFontSize();
  240. }
  241. const String& Text3D::GetText() const
  242. {
  243. return text_.GetText();
  244. }
  245. HorizontalAlignment Text3D::GetHorizontalAlignment() const
  246. {
  247. return text_.GetHorizontalAlignment();
  248. }
  249. VerticalAlignment Text3D::GetVerticalAlignment() const
  250. {
  251. return text_.GetVerticalAlignment();
  252. }
  253. HorizontalAlignment Text3D::GetTextAlignment() const
  254. {
  255. return text_.GetTextAlignment();
  256. }
  257. float Text3D::GetRowSpacing() const
  258. {
  259. return text_.GetRowSpacing();
  260. }
  261. bool Text3D::GetWordwrap() const
  262. {
  263. return text_.GetWordwrap();
  264. }
  265. TextEffect Text3D::GetTextEffect() const
  266. {
  267. return text_.GetTextEffect();
  268. }
  269. const Color& Text3D::GetEffectColor() const
  270. {
  271. return text_.GetEffectColor();
  272. }
  273. int Text3D::GetWidth() const
  274. {
  275. return text_.GetWidth();
  276. }
  277. int Text3D::GetRowHeight() const
  278. {
  279. return text_.GetRowHeight();
  280. }
  281. unsigned Text3D::GetNumRows() const
  282. {
  283. return text_.GetNumRows();
  284. }
  285. const PODVector<int>& Text3D::GetRowWidths() const
  286. {
  287. return text_.GetRowWidths();
  288. }
  289. const Color& Text3D::GetColor(Corner corner) const
  290. {
  291. return text_.GetColor(corner);
  292. }
  293. float Text3D::GetOpacity() const
  294. {
  295. return text_.GetOpacity();
  296. }
  297. void Text3D::OnNodeSet(Node* node)
  298. {
  299. Drawable::OnNodeSet(node);
  300. if (node)
  301. customWorldTransform_ = node->GetWorldTransform();
  302. }
  303. void Text3D::OnWorldBoundingBoxUpdate()
  304. {
  305. if (textDirty_)
  306. UpdateTextBatches();
  307. // In face camera mode, use the last camera rotation to build the world bounding box
  308. worldBoundingBox_ = boundingBox_.Transformed(faceCamera_ ? Matrix3x4(node_->GetWorldPosition(),
  309. customWorldTransform_.Rotation(), node_->GetWorldScale()) : node_->GetWorldTransform());
  310. }
  311. void Text3D::MarkTextDirty()
  312. {
  313. textDirty_ = true;
  314. OnMarkedDirty(node_);
  315. MarkNetworkUpdate();
  316. }
  317. void Text3D::SetMaterialAttr(ResourceRef value)
  318. {
  319. ResourceCache* cache = GetSubsystem<ResourceCache>();
  320. SetMaterial(cache->GetResource<Material>(value.id_));
  321. }
  322. void Text3D::SetFontAttr(ResourceRef value)
  323. {
  324. ResourceCache* cache = GetSubsystem<ResourceCache>();
  325. text_.font_ = cache->GetResource<Font>(value.id_);
  326. }
  327. ResourceRef Text3D::GetMaterialAttr() const
  328. {
  329. return GetResourceRef(material_, Material::GetTypeStatic());
  330. }
  331. ResourceRef Text3D::GetFontAttr() const
  332. {
  333. return GetResourceRef(text_.font_, Font::GetTypeStatic());
  334. }
  335. void Text3D::UpdateTextBatches()
  336. {
  337. uiBatches_.Clear();
  338. uiVertexData_.Clear();
  339. text_.GetBatches(uiBatches_, uiVertexData_, IntRect::ZERO);
  340. Vector3 offset(Vector3::ZERO);
  341. switch (text_.GetHorizontalAlignment())
  342. {
  343. case HA_LEFT:
  344. break;
  345. case HA_CENTER:
  346. offset.x_ -= (float)text_.GetWidth() * 0.5f;
  347. break;
  348. case HA_RIGHT:
  349. offset.x_ -= (float)text_.GetWidth();
  350. break;
  351. }
  352. switch (text_.GetVerticalAlignment())
  353. {
  354. case VA_TOP:
  355. break;
  356. case VA_CENTER:
  357. offset.y_ -= (float)text_.GetHeight() * 0.5f;
  358. break;
  359. case VA_BOTTOM:
  360. offset.y_ -= (float)text_.GetHeight();
  361. break;
  362. }
  363. boundingBox_.defined_ = false;
  364. boundingBox_.min_ = boundingBox_.max_ = Vector3::ZERO;
  365. for (unsigned i = 0; i < uiVertexData_.Size(); i += UI_VERTEX_SIZE)
  366. {
  367. Vector3& position = *(reinterpret_cast<Vector3*>(&uiVertexData_[i]));
  368. position += offset;
  369. position *= TEXT_SCALING;
  370. position.y_ = -position.y_;
  371. boundingBox_.Merge(position);
  372. }
  373. textDirty_ = false;
  374. geometryDirty_ = true;
  375. }
  376. void Text3D::UpdateTextMaterials(bool forceUpdate)
  377. {
  378. batches_.Resize(uiBatches_.Size());
  379. geometries_.Resize(uiBatches_.Size());
  380. for (unsigned i = 0; i < batches_.Size(); ++i)
  381. {
  382. if (!geometries_[i])
  383. {
  384. Geometry* geometry = new Geometry(context_);
  385. geometry->SetVertexBuffer(0, vertexBuffer_, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1);
  386. batches_[i].geometry_ = geometries_[i] = geometry;
  387. }
  388. if (!batches_[i].material_ || forceUpdate)
  389. {
  390. // If material not defined, create a reasonable default from scratch
  391. if (!material_)
  392. {
  393. Material* material = new Material(context_);
  394. Technique* tech = new Technique(context_);
  395. Pass* pass = tech->CreatePass(PASS_ALPHA);
  396. pass->SetVertexShader("Basic_DiffVCol");
  397. pass->SetPixelShader("Basic_AlphaVCol");
  398. pass->SetBlendMode(BLEND_ALPHA);
  399. material->SetTechnique(0, tech);
  400. material->SetCullMode(CULL_NONE);
  401. batches_[i].material_ = material;
  402. }
  403. else
  404. batches_[i].material_ = material_->Clone();
  405. }
  406. Material* material = batches_[i].material_;
  407. material->SetTexture(TU_DIFFUSE, uiBatches_[i].texture_);
  408. }
  409. }
  410. }