NAP
renderabletextcomponent.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "rendercomponent.h"
9 #include "materialinstance.h"
10 #include "renderableglyph.h"
11 #include "color.h"
12 
13 // External Includes
14 #include <font.h>
15 #include <planemesh.h>
16 #include <renderablemesh.h>
17 #include <transformcomponent.h>
18 
19 namespace nap
20 {
21  class RenderableTextComponentInstance;
22 
33  {
34  RTTI_ENABLE(RenderableComponent)
36  public:
38  std::string mText;
39  RGBColorFloat mColor = { 1.0f, 1.0f, 1.0f };
40  };
41 
42 
54  {
55  RTTI_ENABLE(RenderableComponentInstance)
56  public:
57 
59 
65  virtual bool init(utility::ErrorState& errorState) override;
66 
70  const FontInstance& getFont() const;
71 
80  bool setText(const std::string& text, utility::ErrorState& error);
81 
86  void setColor(const glm::vec3& color);
87 
96  bool setText(int lineIndex, const std::string& text, utility::ErrorState& error);
97 
105  bool addLine(const std::string& text, utility::ErrorState& error);
106 
113  void setLineIndex(int index);
114 
119  const std::string& getText();
120 
126  const std::string& getText(int index);
127 
133  void resize(int lines);
134 
139  int getCount() const;
140 
144  void clear();
145 
150  const math::Rect& getBoundingBox() const;
151 
156  const math::Rect& getBoundingBox(int index);
157 
166  virtual RenderableGlyph* getRenderableGlyph(uint index, float scale, utility::ErrorState& error) const = 0;
167 
171  const MaterialInstance& getMaterialInstance() const { return mMaterialInstance; }
172 
176  MaterialInstance& getMaterialInstance() { return mMaterialInstance; }
177 
182  float getDPIScale() const { return mDPIScale; }
183 
184  protected:
194  void draw(IRenderTarget& renderTarget, VkCommandBuffer commandBuffer, const glm::mat4& viewMatrix, const glm::mat4& projectionMatrix, const glm::mat4& modelMatrix);
195 
199  bool hasTransform() const { return mTransform != nullptr; }
200 
204  const nap::TransformComponentInstance* getTransform() const { return mTransform; }
205 
212  virtual bool setup(float scale, utility::ErrorState& errorState);
213 
214  FontInstance* mFont = nullptr;
215  RenderService* mRenderService = nullptr;
216 
217  private:
218  int mIndex = 0;
219  float mDPIScale = 1.0f;
220  MaterialInstance mMaterialInstance;
221  PlaneMesh mPlane;
222  Sampler2DInstance* mGlyphUniform = nullptr;
223  UniformVec3Instance* mColorUniform = nullptr;
224  UniformMat4Instance* mModelUniform = nullptr;
225  UniformMat4Instance* mViewUniform = nullptr;
226  UniformMat4Instance* mProjectionUniform = nullptr;
227  TransformComponentInstance* mTransform = nullptr;
228  RenderableMesh mRenderableMesh;
229  VertexAttribute<glm::vec3>* mPositionAttr = nullptr;
230  std::vector<math::Rect> mTextBounds;
231  std::vector<std::vector<RenderableGlyph*>> mGlyphCache;
232  std::vector<std::string> mLinesCache;
233  MaterialInstanceResource mMaterialInstanceResource;
234  };
235 }
nap::RenderableTextComponentInstance
Definition: renderabletextcomponent.h:53
nap::uint
unsigned int uint
Definition: numeric.h:23
nap::RenderableComponentInstance
Definition: rendercomponent.h:46
nap::IRenderTarget
Definition: irendertarget.h:21
nap::RenderableTextComponent::mText
std::string mText
Property: 'Text' to draw.
Definition: renderabletextcomponent.h:38
nap::RenderableTextComponent::mFont
ResourcePtr< Font > mFont
Property: 'Font' that represents the style of the text.
Definition: renderabletextcomponent.h:37
nap::RenderableTextComponentInstance::hasTransform
bool hasTransform() const
Definition: renderabletextcomponent.h:199
nap::RGBColor< float >
nap::VertexAttribute< glm::vec3 >
nap::rtti::ObjectPtr
Definition: objectptr.h:154
nap::math::Rect
Definition: rect.h:19
nap::utility::ErrorState
Definition: errorstate.h:19
nap::RenderableTextComponentInstance::getTransform
const nap::TransformComponentInstance * getTransform() const
Definition: renderabletextcomponent.h:204
nap::RenderableTextComponent
Definition: renderabletextcomponent.h:32
nap::RenderService
Definition: renderservice.h:275
nap::EntityInstance
Definition: entity.h:34
nap::RenderableComponent
Definition: rendercomponent.h:29
nap::Component
Definition: component.h:151
nap::RenderableMesh
Definition: renderablemesh.h:22
nap::MaterialInstance
Definition: materialinstance.h:297
nap::RenderableTextComponentInstance::getDPIScale
float getDPIScale() const
Definition: renderabletextcomponent.h:182
nap::RenderableGlyph
Definition: renderableglyph.h:24
nap
Definition: templateapp.h:17
nap::RenderableTextComponentInstance::getMaterialInstance
MaterialInstance & getMaterialInstance()
Definition: renderabletextcomponent.h:176
nap::TypedUniformValueInstance
Definition: uniforminstance.h:243
nap::Sampler2DInstance
Definition: samplerinstance.h:93
nap::TransformComponentInstance
Definition: transformcomponent.h:73
nap::RenderableTextComponentInstance::getMaterialInstance
const MaterialInstance & getMaterialInstance() const
Definition: renderabletextcomponent.h:171
nap::PlaneMesh
Definition: planemesh.h:26
nap::MaterialInstanceResource
Definition: materialinstance.h:68
nap::FontInstance
Definition: font.h:121