Text3D.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 "Geometry.h"
  26. #include "Material.h"
  27. #include "Node.h"
  28. #include "Technique.h"
  29. #include "Text.h"
  30. #include "Text3D.h"
  31. #include "VertexBuffer.h"
  32. namespace Urho3D
  33. {
  34. static const float TEXT_SCALING = 1.0f / 128.0f;
  35. OBJECTTYPESTATIC(Text3D);
  36. Text3D::Text3D(Context* context) :
  37. Drawable(context, DRAWABLE_GEOMETRY),
  38. text_(new Text(context)),
  39. vertexBuffer_(new VertexBuffer(context_)),
  40. faceCamera_(false),
  41. textDirty_(true),
  42. geometryDirty_(true)
  43. {
  44. }
  45. Text3D::~Text3D()
  46. {
  47. }
  48. void Text3D::RegisterObject(Context* context)
  49. {
  50. context->RegisterFactory<Text3D>();
  51. }
  52. void Text3D::UpdateBatches(const FrameInfo& frame)
  53. {
  54. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  55. distance_ = frame.camera_->GetDistance(GetWorldBoundingBox().Center());
  56. if (faceCamera_)
  57. {
  58. customWorldTransform_ = Matrix3x4(node_->GetWorldPosition(), frame.camera_->GetNode()->GetWorldRotation(), node_->GetWorldScale());
  59. worldBoundingBoxDirty_ = true;
  60. }
  61. for (unsigned i = 0; i < batches_.Size(); ++i)
  62. {
  63. batches_[i].distance_ = distance_;
  64. batches_[i].worldTransform_ = faceCamera_ ? &customWorldTransform_ : &worldTransform;
  65. }
  66. }
  67. void Text3D::UpdateGeometry(const FrameInfo& frame)
  68. {
  69. for (unsigned i = 0; i < batches_.Size(); ++i)
  70. {
  71. Geometry* geometry = geometries_[i];
  72. geometry->SetDrawRange(TRIANGLE_LIST, 0, 0, uiBatches_[i].vertexStart_, uiBatches_[i].vertexEnd_ - uiBatches_[i].vertexStart_);
  73. }
  74. if (uiVertexData_.Size())
  75. {
  76. unsigned vertexCount = uiVertexData_.Size() / UI_VERTEX_SIZE;
  77. if (vertexBuffer_->GetVertexCount() != vertexCount)
  78. vertexBuffer_->SetSize(vertexCount, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1);
  79. vertexBuffer_->SetData(&uiVertexData_[0]);
  80. }
  81. geometryDirty_ = false;
  82. }
  83. UpdateGeometryType Text3D::GetUpdateGeometryType()
  84. {
  85. if (geometryDirty_ || vertexBuffer_->IsDataLost())
  86. return UPDATE_MAIN_THREAD;
  87. else
  88. return UPDATE_NONE;
  89. }
  90. bool Text3D::SetFont(const String& fontName, int size)
  91. {
  92. bool success = text_->SetFont(fontName, size);
  93. // Changing font requires materials to be re-evaluated. Material evaluation can not be done in worker threads,
  94. // so UI batches must be brought up-to-date immediately
  95. UpdateTextBatches();
  96. UpdateTextMaterials();
  97. MarkNetworkUpdate();
  98. return success;
  99. }
  100. bool Text3D::SetFont(Font* font, int size)
  101. {
  102. bool success = text_->SetFont(font, size);
  103. UpdateTextBatches();
  104. UpdateTextMaterials();
  105. MarkNetworkUpdate();
  106. return success;
  107. }
  108. void Text3D::SetText(const String& text)
  109. {
  110. text_->SetText(text);
  111. // Changing text requires materials to be re-evaluated, in case the font is multi-page
  112. UpdateTextBatches();
  113. UpdateTextMaterials();
  114. MarkNetworkUpdate();
  115. }
  116. void Text3D::SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign)
  117. {
  118. text_->SetAlignment(hAlign, vAlign);
  119. MarkTextDirty();
  120. }
  121. void Text3D::SetHorizontalAlignment(HorizontalAlignment align)
  122. {
  123. text_->SetHorizontalAlignment(align);
  124. MarkTextDirty();
  125. }
  126. void Text3D::SetVerticalAlignment(VerticalAlignment align)
  127. {
  128. text_->SetVerticalAlignment(align);
  129. MarkTextDirty();
  130. }
  131. void Text3D::SetTextAlignment(HorizontalAlignment align)
  132. {
  133. text_->SetTextAlignment(align);
  134. MarkTextDirty();
  135. }
  136. void Text3D::SetRowSpacing(float spacing)
  137. {
  138. text_->SetRowSpacing(spacing);
  139. MarkTextDirty();
  140. }
  141. void Text3D::SetWordwrap(bool enable)
  142. {
  143. text_->SetWordwrap(enable);
  144. MarkTextDirty();
  145. }
  146. void Text3D::SetMaxWidth(int width)
  147. {
  148. text_->SetMaxWidth(width);
  149. MarkTextDirty();
  150. }
  151. void Text3D::SetColor(const Color& color)
  152. {
  153. text_->SetColor(color);
  154. MarkTextDirty();
  155. }
  156. void Text3D::SetColor(Corner corner, const Color& color)
  157. {
  158. text_->SetColor(corner, color);
  159. MarkTextDirty();
  160. }
  161. void Text3D::SetOpacity(float opacity)
  162. {
  163. text_->SetOpacity(opacity);
  164. MarkTextDirty();
  165. }
  166. void Text3D::SetFaceCamera(bool enable)
  167. {
  168. faceCamera_ = enable;
  169. }
  170. Font* Text3D::GetFont() const
  171. {
  172. return text_->GetFont();
  173. }
  174. int Text3D::GetFontSize() const
  175. {
  176. return text_->GetFontSize();
  177. }
  178. const String& Text3D::GetText() const
  179. {
  180. return text_->GetText();
  181. }
  182. HorizontalAlignment Text3D::GetHorizontalAlignment() const
  183. {
  184. return text_->GetHorizontalAlignment();
  185. }
  186. VerticalAlignment Text3D::GetVerticalAlignment() const
  187. {
  188. return text_->GetVerticalAlignment();
  189. }
  190. HorizontalAlignment Text3D::GetTextAlignment() const
  191. {
  192. return text_->GetTextAlignment();
  193. }
  194. float Text3D::GetRowSpacing() const
  195. {
  196. return text_->GetRowSpacing();
  197. }
  198. bool Text3D::GetWordwrap() const
  199. {
  200. return text_->GetWordwrap();
  201. }
  202. int Text3D::GetMaxWidth() const
  203. {
  204. return text_->GetMaxWidth();
  205. }
  206. int Text3D::GetRowHeight() const
  207. {
  208. return text_->GetRowHeight();
  209. }
  210. unsigned Text3D::GetNumRows() const
  211. {
  212. return text_->GetNumRows();
  213. }
  214. const PODVector<int>& Text3D::GetRowWidths() const
  215. {
  216. return text_->GetRowWidths();
  217. }
  218. const Color& Text3D::GetColor(Corner corner) const
  219. {
  220. return text_->GetColor(corner);
  221. }
  222. float Text3D::GetOpacity() const
  223. {
  224. return text_->GetOpacity();
  225. }
  226. void Text3D::OnNodeSet(Node* node)
  227. {
  228. Drawable::OnNodeSet(node);
  229. if (node)
  230. customWorldTransform_ = node->GetWorldTransform();
  231. }
  232. void Text3D::OnWorldBoundingBoxUpdate()
  233. {
  234. if (textDirty_)
  235. UpdateTextBatches();
  236. worldBoundingBox_ = faceCamera_ ? boundingBox_.Transformed(customWorldTransform_) :
  237. boundingBox_.Transformed(node_->GetWorldTransform());
  238. }
  239. void Text3D::MarkTextDirty()
  240. {
  241. textDirty_ = true;
  242. MarkNetworkUpdate();
  243. }
  244. void Text3D::UpdateTextBatches()
  245. {
  246. uiBatches_.Clear();
  247. uiVertexData_.Clear();
  248. text_->GetBatches(uiBatches_, uiVertexData_, IntRect::ZERO);
  249. Vector3 offset(Vector3::ZERO);
  250. switch (text_->GetHorizontalAlignment())
  251. {
  252. case HA_LEFT:
  253. break;
  254. case HA_CENTER:
  255. offset.x_ -= (float)text_->GetWidth() * 0.5f;
  256. break;
  257. case HA_RIGHT:
  258. offset.x_ -= (float)text_->GetWidth();
  259. break;
  260. }
  261. switch (text_->GetVerticalAlignment())
  262. {
  263. case VA_TOP:
  264. break;
  265. case VA_CENTER:
  266. offset.y_ -= (float)text_->GetHeight() * 0.5f;
  267. break;
  268. case VA_BOTTOM:
  269. offset.y_ -= (float)text_->GetHeight();
  270. break;
  271. }
  272. boundingBox_.defined_ = false;
  273. boundingBox_.min_ = boundingBox_.max_ = Vector3::ZERO;
  274. for (unsigned i = 0; i < uiVertexData_.Size(); i += UI_VERTEX_SIZE)
  275. {
  276. Vector3& position = *(reinterpret_cast<Vector3*>(&uiVertexData_[i]));
  277. position += offset;
  278. position *= TEXT_SCALING;
  279. position.y_ = -position.y_;
  280. boundingBox_.Merge(position);
  281. }
  282. textDirty_ = false;
  283. geometryDirty_ = true;
  284. }
  285. void Text3D::UpdateTextMaterials()
  286. {
  287. batches_.Resize(uiBatches_.Size());
  288. geometries_.Resize(uiBatches_.Size());
  289. for (unsigned i = 0; i < batches_.Size(); ++i)
  290. {
  291. if (!geometries_[i])
  292. {
  293. Geometry* geometry = new Geometry(context_);
  294. geometry->SetVertexBuffer(0, vertexBuffer_, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1);
  295. batches_[i].geometry_ = geometries_[i] = geometry;
  296. }
  297. if (!batches_[i].material_)
  298. {
  299. Material* material = new Material(context_);
  300. Technique* tech = new Technique(context_);
  301. Pass* pass = tech->CreatePass(PASS_ALPHA);
  302. pass->SetVertexShader("Basic_DiffVCol");
  303. pass->SetPixelShader("Basic_AlphaVCol");
  304. material->SetTechnique(0, tech);
  305. material->SetCullMode(CULL_NONE);
  306. batches_[i].material_ = material;
  307. }
  308. Material* material = batches_[i].material_;
  309. material->SetTexture(TU_DIFFUSE, uiBatches_[i].texture_);
  310. Pass* pass = material->GetTechnique(0)->GetPass(PASS_ALPHA);
  311. pass->SetBlendMode(uiBatches_[i].blendMode_);
  312. }
  313. }
  314. }