| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- //
- // Copyright (c) 2008-2013 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Camera.h"
- #include "Context.h"
- #include "Font.h"
- #include "Geometry.h"
- #include "Log.h"
- #include "Material.h"
- #include "Node.h"
- #include "ResourceCache.h"
- #include "Technique.h"
- #include "Text.h"
- #include "Text3D.h"
- #include "VertexBuffer.h"
- namespace Urho3D
- {
- extern const char* horizontalAlignments[];
- extern const char* verticalAlignments[];
- extern const char* textEffects[];
- extern const char* GEOMETRY_CATEGORY;
- static const float TEXT_SCALING = 1.0f / 128.0f;
- Text3D::Text3D(Context* context) :
- Drawable(context, DRAWABLE_GEOMETRY),
- text_(context),
- vertexBuffer_(new VertexBuffer(context_)),
- customWorldTransform_(Matrix3x4::IDENTITY),
- faceCamera_(false),
- textDirty_(true),
- geometryDirty_(true)
- {
- }
- Text3D::~Text3D()
- {
- }
- void Text3D::RegisterObject(Context* context)
- {
- context->RegisterFactory<Text3D>(GEOMETRY_CATEGORY);
-
- ACCESSOR_ATTRIBUTE(Text3D, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Text3D, VAR_RESOURCEREF, "Font", GetFontAttr, SetFontAttr, ResourceRef, ResourceRef(Font::GetTypeStatic()), AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Text3D, VAR_RESOURCEREF, "Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
- ATTRIBUTE(Text3D, VAR_INT, "Font Size", text_.fontSize_, DEFAULT_FONT_SIZE, AM_DEFAULT);
- ATTRIBUTE(Text3D, VAR_STRING, "Text", text_.text_, String::EMPTY, AM_DEFAULT);
- ENUM_ATTRIBUTE(Text3D, "Text Alignment", text_.textAlignment_, horizontalAlignments, HA_LEFT, AM_DEFAULT);
- ATTRIBUTE(Text3D, VAR_FLOAT, "Row Spacing", text_.rowSpacing_, 1.0f, AM_DEFAULT);
- ATTRIBUTE(Text3D, VAR_BOOL, "Word Wrap", text_.wordWrap_, false, AM_DEFAULT);
- ENUM_ATTRIBUTE(Text3D, "Text Effect", text_.textEffect_, textEffects, TE_NONE, AM_FILE);
- REF_ACCESSOR_ATTRIBUTE(Text3D, VAR_COLOR, "Effect Color", GetEffectColor, SetEffectColor, Color, Color::TRANSPARENT, AM_FILE);
- ACCESSOR_ATTRIBUTE(Text3D, VAR_INT, "Width", GetWidth, SetWidth, int, 0, AM_DEFAULT);
- ENUM_ACCESSOR_ATTRIBUTE(Text3D, "Horiz Alignment", GetHorizontalAlignment, SetHorizontalAlignment, HorizontalAlignment, horizontalAlignments, HA_LEFT, AM_DEFAULT);
- ENUM_ACCESSOR_ATTRIBUTE(Text3D, "Vert Alignment", GetVerticalAlignment, SetVerticalAlignment, VerticalAlignment, verticalAlignments, VA_TOP, AM_DEFAULT);
- REF_ACCESSOR_ATTRIBUTE(Text3D, VAR_COLOR, "Color", GetColorAttr, SetColor, Color, Color::WHITE, AM_DEFAULT);
- ATTRIBUTE(Text3D, VAR_COLOR, "Top Left Color", text_.color_[0], Color::WHITE, AM_DEFAULT);
- ATTRIBUTE(Text3D, VAR_COLOR, "Top Right Color", text_.color_[1], Color::WHITE, AM_DEFAULT);
- ATTRIBUTE(Text3D, VAR_COLOR, "Bottom Left Color", text_.color_[2], Color::WHITE, AM_DEFAULT);
- ATTRIBUTE(Text3D, VAR_COLOR, "Bottom Right Color", text_.color_[3], Color::WHITE, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Text3D, VAR_BOOL, "Can Be Occluded", IsOccludee, SetOccludee, bool, true, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Text3D, VAR_BOOL, "Face Camera", GetFaceCamera, SetFaceCamera, bool, false, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Text3D, VAR_FLOAT, "Draw Distance", GetDrawDistance, SetDrawDistance, float, 0.0f, AM_DEFAULT);
- COPY_BASE_ATTRIBUTES(Text3D, Drawable);
- }
- void Text3D::ApplyAttributes()
- {
- text_.ApplyAttributes();
- MarkTextDirty();
- UpdateTextBatches();
- UpdateTextMaterials();
- }
- void Text3D::UpdateBatches(const FrameInfo& frame)
- {
- const Matrix3x4& worldTransform = node_->GetWorldTransform();
- distance_ = frame.camera_->GetDistance(GetWorldBoundingBox().Center());
-
- if (faceCamera_)
- {
- customWorldTransform_ = Matrix3x4(node_->GetWorldPosition(), frame.camera_->GetNode()->GetWorldRotation(), node_->GetWorldScale());
- worldBoundingBoxDirty_ = true;
- }
-
- for (unsigned i = 0; i < batches_.Size(); ++i)
- {
- batches_[i].distance_ = distance_;
- batches_[i].worldTransform_ = faceCamera_ ? &customWorldTransform_ : &worldTransform;
- }
- }
- void Text3D::UpdateGeometry(const FrameInfo& frame)
- {
- if (geometryDirty_)
- {
- for (unsigned i = 0; i < batches_.Size(); ++i)
- {
- Geometry* geometry = geometries_[i];
- geometry->SetDrawRange(TRIANGLE_LIST, 0, 0, uiBatches_[i].vertexStart_, (uiBatches_[i].vertexEnd_ -
- uiBatches_[i].vertexStart_) / UI_VERTEX_SIZE);
- }
- }
-
- if ((geometryDirty_ || vertexBuffer_->IsDataLost()) && uiVertexData_.Size())
- {
- unsigned vertexCount = uiVertexData_.Size() / UI_VERTEX_SIZE;
- if (vertexBuffer_->GetVertexCount() != vertexCount)
- vertexBuffer_->SetSize(vertexCount, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1);
- vertexBuffer_->SetData(&uiVertexData_[0]);
- }
-
- geometryDirty_ = false;
- }
- UpdateGeometryType Text3D::GetUpdateGeometryType()
- {
- if (geometryDirty_ || vertexBuffer_->IsDataLost())
- return UPDATE_MAIN_THREAD;
- else
- return UPDATE_NONE;
- }
- void Text3D::SetMaterial(Material* material)
- {
- material_ = material;
-
- UpdateTextMaterials(true);
- }
- bool Text3D::SetFont(const String& fontName, int size)
- {
- bool success = text_.SetFont(fontName, size);
-
- // Changing font requires materials to be re-evaluated. Material evaluation can not be done in worker threads,
- // so UI batches must be brought up-to-date immediately
- MarkTextDirty();
- UpdateTextBatches();
- UpdateTextMaterials();
-
- return success;
- }
- bool Text3D::SetFont(Font* font, int size)
- {
- bool success = text_.SetFont(font, size);
-
- MarkTextDirty();
- UpdateTextBatches();
- UpdateTextMaterials();
-
- return success;
- }
- void Text3D::SetText(const String& text)
- {
- text_.SetText(text);
-
- // Changing text requires materials to be re-evaluated, in case the font is multi-page
- MarkTextDirty();
- UpdateTextBatches();
- UpdateTextMaterials();
- }
- void Text3D::SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign)
- {
- text_.SetAlignment(hAlign, vAlign);
-
- MarkTextDirty();
- }
- void Text3D::SetHorizontalAlignment(HorizontalAlignment align)
- {
- text_.SetHorizontalAlignment(align);
-
- MarkTextDirty();
- }
- void Text3D::SetVerticalAlignment(VerticalAlignment align)
- {
- text_.SetVerticalAlignment(align);
-
- MarkTextDirty();
- }
- void Text3D::SetTextAlignment(HorizontalAlignment align)
- {
- text_.SetTextAlignment(align);
-
- MarkTextDirty();
- }
- void Text3D::SetRowSpacing(float spacing)
- {
- text_.SetRowSpacing(spacing);
-
- MarkTextDirty();
- }
- void Text3D::SetWordwrap(bool enable)
- {
- text_.SetWordwrap(enable);
-
- MarkTextDirty();
- }
- void Text3D::SetTextEffect(TextEffect textEffect)
- {
- text_.SetTextEffect(textEffect);
- }
- void Text3D::SetEffectColor(const Color& effectColor)
- {
- text_.SetEffectColor(effectColor);
- }
- void Text3D::SetWidth(int width)
- {
- text_.SetMinWidth(width);
- text_.SetWidth(width);
-
- MarkTextDirty();
- }
- void Text3D::SetColor(const Color& color)
- {
- text_.SetColor(color);
-
- MarkTextDirty();
- }
- void Text3D::SetColor(Corner corner, const Color& color)
- {
- text_.SetColor(corner, color);
-
- MarkTextDirty();
- }
- void Text3D::SetOpacity(float opacity)
- {
- text_.SetOpacity(opacity);
-
- MarkTextDirty();
- }
- void Text3D::SetFaceCamera(bool enable)
- {
- if (enable != faceCamera_)
- {
- faceCamera_ = enable;
-
- // Bounding box must be recalculated
- OnMarkedDirty(node_);
- }
- }
- Material* Text3D::GetMaterial() const
- {
- return material_;
- }
- Font* Text3D::GetFont() const
- {
- return text_.GetFont();
- }
- int Text3D::GetFontSize() const
- {
- return text_.GetFontSize();
- }
- const String& Text3D::GetText() const
- {
- return text_.GetText();
- }
- HorizontalAlignment Text3D::GetHorizontalAlignment() const
- {
- return text_.GetHorizontalAlignment();
- }
- VerticalAlignment Text3D::GetVerticalAlignment() const
- {
- return text_.GetVerticalAlignment();
- }
- HorizontalAlignment Text3D::GetTextAlignment() const
- {
- return text_.GetTextAlignment();
- }
- float Text3D::GetRowSpacing() const
- {
- return text_.GetRowSpacing();
- }
- bool Text3D::GetWordwrap() const
- {
- return text_.GetWordwrap();
- }
- TextEffect Text3D::GetTextEffect() const
- {
- return text_.GetTextEffect();
- }
- const Color& Text3D::GetEffectColor() const
- {
- return text_.GetEffectColor();
- }
- int Text3D::GetWidth() const
- {
- return text_.GetWidth();
- }
- int Text3D::GetRowHeight() const
- {
- return text_.GetRowHeight();
- }
- unsigned Text3D::GetNumRows() const
- {
- return text_.GetNumRows();
- }
- const PODVector<int>& Text3D::GetRowWidths() const
- {
- return text_.GetRowWidths();
- }
- const Color& Text3D::GetColor(Corner corner) const
- {
- return text_.GetColor(corner);
- }
- float Text3D::GetOpacity() const
- {
- return text_.GetOpacity();
- }
- void Text3D::OnNodeSet(Node* node)
- {
- Drawable::OnNodeSet(node);
-
- if (node)
- customWorldTransform_ = node->GetWorldTransform();
- }
- void Text3D::OnWorldBoundingBoxUpdate()
- {
- if (textDirty_)
- UpdateTextBatches();
-
- // In face camera mode, use the last camera rotation to build the world bounding box
- worldBoundingBox_ = boundingBox_.Transformed(faceCamera_ ? Matrix3x4(node_->GetWorldPosition(),
- customWorldTransform_.Rotation(), node_->GetWorldScale()) : node_->GetWorldTransform());
- }
- void Text3D::MarkTextDirty()
- {
- textDirty_ = true;
-
- OnMarkedDirty(node_);
- MarkNetworkUpdate();
- }
- void Text3D::SetMaterialAttr(ResourceRef value)
- {
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- SetMaterial(cache->GetResource<Material>(value.id_));
- }
- void Text3D::SetFontAttr(ResourceRef value)
- {
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- text_.font_ = cache->GetResource<Font>(value.id_);
- }
- ResourceRef Text3D::GetMaterialAttr() const
- {
- return GetResourceRef(material_, Material::GetTypeStatic());
- }
- ResourceRef Text3D::GetFontAttr() const
- {
- return GetResourceRef(text_.font_, Font::GetTypeStatic());
- }
- void Text3D::UpdateTextBatches()
- {
- uiBatches_.Clear();
- uiVertexData_.Clear();
-
- text_.GetBatches(uiBatches_, uiVertexData_, IntRect::ZERO);
-
- Vector3 offset(Vector3::ZERO);
-
- switch (text_.GetHorizontalAlignment())
- {
- case HA_LEFT:
- break;
-
- case HA_CENTER:
- offset.x_ -= (float)text_.GetWidth() * 0.5f;
- break;
-
- case HA_RIGHT:
- offset.x_ -= (float)text_.GetWidth();
- break;
- }
-
- switch (text_.GetVerticalAlignment())
- {
- case VA_TOP:
- break;
-
- case VA_CENTER:
- offset.y_ -= (float)text_.GetHeight() * 0.5f;
- break;
-
- case VA_BOTTOM:
- offset.y_ -= (float)text_.GetHeight();
- break;
- }
-
- boundingBox_.defined_ = false;
- boundingBox_.min_ = boundingBox_.max_ = Vector3::ZERO;
-
- for (unsigned i = 0; i < uiVertexData_.Size(); i += UI_VERTEX_SIZE)
- {
- Vector3& position = *(reinterpret_cast<Vector3*>(&uiVertexData_[i]));
- position += offset;
- position *= TEXT_SCALING;
- position.y_ = -position.y_;
- boundingBox_.Merge(position);
- }
-
- textDirty_ = false;
- geometryDirty_ = true;
- }
- void Text3D::UpdateTextMaterials(bool forceUpdate)
- {
- batches_.Resize(uiBatches_.Size());
- geometries_.Resize(uiBatches_.Size());
-
- for (unsigned i = 0; i < batches_.Size(); ++i)
- {
- if (!geometries_[i])
- {
- Geometry* geometry = new Geometry(context_);
- geometry->SetVertexBuffer(0, vertexBuffer_, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1);
- batches_[i].geometry_ = geometries_[i] = geometry;
- }
-
- if (!batches_[i].material_ || forceUpdate)
- {
- // If material not defined, create a reasonable default from scratch
- if (!material_)
- {
- Material* material = new Material(context_);
- Technique* tech = new Technique(context_);
- Pass* pass = tech->CreatePass(PASS_ALPHA);
- pass->SetVertexShader("Basic_DiffVCol");
- pass->SetPixelShader("Basic_AlphaVCol");
- pass->SetBlendMode(BLEND_ALPHA);
- material->SetTechnique(0, tech);
- material->SetCullMode(CULL_NONE);
- batches_[i].material_ = material;
- }
- else
- batches_[i].material_ = material_->Clone();
- }
-
- Material* material = batches_[i].material_;
- material->SetTexture(TU_DIFFUSE, uiBatches_[i].texture_);
- }
- }
- }
|