365f29692bd0decebb0180f4e0c35cf4c39e97b1
[platform/core/uifw/dali-core.git] / dali / internal / update / node-attachments / scene-graph-text-attachment.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_TEXT_ATTACHMENT_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_TEXT_ATTACHMENT_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.0 (the License);
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //     http://floralicense.org/license/
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an AS IS BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19
20 // INTERNAL INCLUDES
21 #include <dali/internal/common/event-to-update.h>
22 #include <dali/internal/common/owner-pointer.h>
23 #include <dali/internal/update/resources/resource-manager.h>
24 #include <dali/internal/update/node-attachments/scene-graph-renderable-attachment.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 class TextParameters;
33
34 namespace SceneGraph
35 {
36
37 class TextRenderer;
38 class Shader;
39 class RenderQueue;
40
41 /**
42  * An attachment for rendering text from a font atlas.
43  */
44 class TextAttachment : public RenderableAttachment
45 {
46 public:
47
48   /**
49    * Construct a new TextAttachment.
50    * @return a new TextAttachment
51    */
52   static TextAttachment* New();
53
54   /**
55    * Virtual destructor
56    */
57   virtual ~TextAttachment();
58
59   /**
60    * @copydoc RenderableAttachment::GetRenderer().
61    */
62   virtual Renderer& GetRenderer();
63
64   /**
65    * @copydoc RenderableAttachment::GetRenderer().
66    */
67   virtual const Renderer& GetRenderer() const;
68
69   /**
70    * Set the text vertex buffer; ownership is passed to the renderer.
71    * This is to avoid duplicate data being held in the text-attachment and the scene-graph-text-attachment.
72    * @param[in] updateBufferIndex The current update buffer index.
73    * @param[in] vertexBuffer vertex buffer
74    */
75   void SetTextVertexBuffer( BufferIndex updateBufferIndex, TextVertexBuffer* vertexBuffer );
76
77   /**
78    * Set the color associated with the gradient end point.
79    * @param[in] updateBufferIndex The current update buffer index.
80    * @param[in] color The gradient color (end-point color)
81    */
82   void SetGradientColor( BufferIndex updateBufferIndex, const Vector4& color );
83
84   /**
85    * Set the gradient start point.
86    * @param[in] updateBufferIndex The current update buffer index.
87    * @param[in] position The relative position of the gradient start point.
88    */
89   void SetGradientStartPoint( BufferIndex updateBufferIndex, const Vector2& position );
90
91   /**
92    * Set the gradient end point.
93    * @param[in] updateBufferIndex The current update buffer index.
94    * @param[in] position The relative position of the gradient end point.
95    */
96   void SetGradientEndPoint( BufferIndex updateBufferIndex, const Vector2& position );
97
98   /**
99    * Set the text color.
100    * @param[in] updateBufferIndex The current update buffer index.
101    * @param[in] color The text color.
102    */
103   void SetTextColor( BufferIndex updateBufferIndex, const Vector4& color );
104
105   /**
106    * Set text outlining.
107    * @param[in] updateBufferIndex The current update buffer index.
108    * @param[in] enable Set to true to enable text outlining.
109    * @param[in] color Outline color.
110    * @param[in] thickness Thickness of outline.
111    */
112   void SetOutline( BufferIndex updateBufferIndex, bool enable, const Vector4& color, const Vector2& thickness );
113
114   /**
115    * Set text glow.
116    * @param[in] updateBufferIndex The current update buffer index.
117    * @param[in] enable Set to true to enable text outer glow.
118    * @param[in] color Glow color.
119    * @param[in] intensity Determines the amount of glow around text.
120    */
121   void SetGlow( BufferIndex updateBufferIndex, bool enable, const Vector4& color, float intensity );
122
123   /**
124    * Set text shadow.
125    * @param[in] updateBufferIndex The current update buffer index.
126    * @param[in] enable  Set to true to enable text drop shadow.
127    * @param[in] color   Shadow color.
128    * @param[in] offset  Offset in pixels. To avoid cropping of the drop shadow limit the offset to PointSize / 3.5.
129    * @param[in] size    Size of shadow in pixels. 0 means the shadow is the same size as the text.
130    */
131   void SetDropShadow( BufferIndex updateBufferIndex, bool enable, const Vector4& color, const Vector2& offset, const float size );
132
133   /**
134    * Set soft edge smoothing.
135    * @param[in] updateBufferIndex The current update buffer index.
136    * @param[in] smoothEdge Specify the distance field value for the center of the text edge.
137    */
138   void SetSmoothEdge( BufferIndex updateBufferIndex, float smoothEdge );
139
140   /**
141    * @copydoc RenderableAttachment::ShaderChanged()
142    */
143   virtual void ShaderChanged( BufferIndex updateBufferIndex );
144
145   /**
146    * @copydoc RenderableAttachment::SizeChanged()
147    */
148   virtual void SizeChanged( BufferIndex updateBufferIndex );
149
150   /**
151    * @copydoc RenderableAttachment::DoPrepareRender()
152    */
153   virtual void DoPrepareRender( BufferIndex updateBufferIndex );
154
155   /**
156    * @copydoc RenderableAttachment::IsFullyOpaque()
157    */
158   virtual bool IsFullyOpaque( BufferIndex updateBufferIndex )
159   {
160     return false; // false since drawing text requires alpha
161   }
162
163   /**
164    * @copydoc RenderableAttachment::GetScaleForSize()
165    */
166   virtual void DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
167
168 protected:
169
170   /**
171    * Protected constructor. See New()
172    */
173   TextAttachment();
174
175 private:
176
177   /**
178    * @copydoc RenderableAttachment::ConnectToSceneGraph2().
179    */
180   virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex );
181
182   /**
183    * @copydoc RenderableAttachment::OnDestroy2().
184    */
185   virtual void OnDestroy2();
186
187   /**
188    * @copydoc RenderableAttachment::DoPrepareResources()
189    */
190   virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
191
192   // Undefined
193   TextAttachment(const TextAttachment&);
194
195   // Undefined
196   TextAttachment& operator=(const TextAttachment& rhs);
197
198 private:
199
200   TextRenderer* mTextRenderer;    ///< Raw-pointers to renderer that is owned by RenderManager
201   Vector2 mGeometrySize;          ///< used for geometry scaling
202   Vector2 mGeometryScaling;       ///< used for geometry scaling
203
204   unsigned int mAtlasId;          ///< atlas Id from last vertex buffer
205
206 };
207
208 inline void SetTextVertexBufferMessage( EventToUpdate& eventToUpdate, const TextAttachment& attachment, TextVertexBuffer& buffer)
209 {
210   typedef MessageDoubleBuffered1< TextAttachment, OwnerPointer< TextVertexBuffer> > LocalType;
211
212   // Reserve some memory inside the message queue
213   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
214
215   // Construct message in the message queue memory; note that delete should not be called on the return value
216   new (slot) LocalType( &attachment, &TextAttachment::SetTextVertexBuffer, &buffer );
217 }
218
219 inline void SetGradientColorMessage( EventToUpdate& eventToUpdate, const TextAttachment& attachment, const Vector4& color )
220 {
221   typedef MessageDoubleBuffered1< TextAttachment, Vector4 > LocalType;
222
223   // Reserve some memory inside the message queue
224   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
225
226   // Construct message in the message queue memory; note that delete should not be called on the return value
227   new (slot) LocalType( &attachment, &TextAttachment::SetGradientColor, color );
228 }
229
230 inline void SetGradientStartPointMessage( EventToUpdate& eventToUpdate, const TextAttachment& attachment, const Vector2& position )
231 {
232   typedef MessageDoubleBuffered1< TextAttachment, Vector2 > LocalType;
233
234   // Reserve some memory inside the message queue
235   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
236
237   // Construct message in the message queue memory; note that delete should not be called on the return value
238   new (slot) LocalType( &attachment, &TextAttachment::SetGradientStartPoint, position );
239 }
240
241 inline void SetGradientEndPointMessage( EventToUpdate& eventToUpdate, const TextAttachment& attachment, const Vector2& position )
242 {
243   typedef MessageDoubleBuffered1< TextAttachment, Vector2 > LocalType;
244
245   // Reserve some memory inside the message queue
246   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
247
248   // Construct message in the message queue memory; note that delete should not be called on the return value
249   new (slot) LocalType( &attachment, &TextAttachment::SetGradientEndPoint, position );
250 }
251
252 inline void SetTextColorMessage( EventToUpdate& eventToUpdate, const TextAttachment& attachment, const Vector4& color )
253 {
254   typedef MessageDoubleBuffered1< TextAttachment, Vector4 > LocalType;
255
256   // Reserve some memory inside the message queue
257   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
258
259   // Construct message in the message queue memory; note that delete should not be called on the return value
260   new (slot) LocalType( &attachment, &TextAttachment::SetTextColor, color );
261 }
262
263 inline void SetOutlineMessage( EventToUpdate& eventToUpdate, const TextAttachment& attachment, bool enable, const Vector4& color, const Vector2& params )
264 {
265   typedef MessageDoubleBuffered3< TextAttachment, bool, Vector4, Vector2 > LocalType;
266
267   // Reserve some memory inside the message queue
268   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
269
270   // Construct message in the message queue memory; note that delete should not be called on the return value
271   new (slot) LocalType( &attachment, &TextAttachment::SetOutline, enable, color, params );
272 }
273
274 inline void SetGlowMessage( EventToUpdate& eventToUpdate, const TextAttachment& attachment, bool enable, const Vector4& color, float params )
275 {
276   typedef MessageDoubleBuffered3< TextAttachment, bool, Vector4, float > LocalType;
277
278   // Reserve some memory inside the message queue
279   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
280
281   // Construct message in the message queue memory; note that delete should not be called on the return value
282   new (slot) LocalType( &attachment, &TextAttachment::SetGlow, enable, color, params );
283 }
284
285 inline void SetDropShadowMessage( EventToUpdate& eventToUpdate, const TextAttachment& attachment, bool enable, const Vector4& color, const Vector2& offset, const float size )
286 {
287   typedef MessageDoubleBuffered4< TextAttachment, bool, Vector4, Vector2, float > LocalType;
288
289   // Reserve some memory inside the message queue
290   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
291
292   // Construct message in the message queue memory; note that delete should not be called on the return value
293   new (slot) LocalType( &attachment, &TextAttachment::SetDropShadow, enable, color, offset, size );
294 }
295
296 inline void SetSmoothEdgeMessage( EventToUpdate& eventToUpdate, const TextAttachment& attachment, float params )
297 {
298   typedef MessageDoubleBuffered1< TextAttachment, float > LocalType;
299
300   // Reserve some memory inside the message queue
301   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
302
303   // Construct message in the message queue memory; note that delete should not be called on the return value
304   new (slot) LocalType( &attachment, &TextAttachment::SetSmoothEdge, params );
305 }
306
307 } // namespace SceneGraph
308
309 } // namespace Internal
310
311 } // namespace Dali
312
313 #endif // __DALI_INTERNAL_SCENE_GRAPH_TEXT_ATTACHMENT_H__