Pass stage from actor to attachment to avoid unnecessary calls to Stage::GetCurrent()
[platform/core/uifw/dali-core.git] / dali / internal / event / actor-attachments / text-attachment-impl.h
1 #ifndef __DALI_INTERNAL_TEXT_ATTACHMENT_H__
2 #define __DALI_INTERNAL_TEXT_ATTACHMENT_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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
21 // INTERNAL INCLUDES
22 #include <dali/public-api/math/radian.h>
23 #include <dali/public-api/math/vector2.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/public-api/text/text-style.h>
26 #include <dali/internal/event/actor-attachments/renderable-attachment-impl.h>
27 #include <dali/internal/event/actor-attachments/actor-attachment-declarations.h>
28 #include <dali/internal/event/text/font-declarations.h>
29 #include <dali/internal/event/text/text-request-helper.h>
30 #include <dali/internal/event/text/resource/glyph-texture-observer.h>
31 #include <dali/internal/common/owner-pointer.h>
32 #include <dali/integration-api/text-array.h>
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39
40 class TextParameters;
41
42 namespace SceneGraph
43 {
44 class TextAttachment;
45 class Node;
46 }
47
48 /**
49  * An attachment for rendering text from a font atlas.
50  */
51 class TextAttachment : public RenderableAttachment, public GlyphTextureObserver
52 {
53 public:
54
55   /**
56    * Create a new TextAttachment.
57    * @param[in] stage The stage to use for messaging
58    * @param[in] parentNode The node to attach a scene-object to.
59    * @param [in] text The text which will be displayed.
60    * @param [in] font The font which will be used for the text.
61    * @return A smart-pointer to the newly allocated TextAttachment.
62    */
63   static TextAttachmentPtr New( Stage& stage, const SceneGraph::Node& parentNode, const Integration::TextArray& text, FontPointer font );
64
65   /**
66    * Set the text label displayed by the attachment
67    * @param [in] text The new text label
68    */
69   void SetText(const Integration::TextArray& text);
70
71   /**
72    * Get the text label displayed by the attachment
73    * @return The text label
74    */
75   const Integration::TextArray& GetText() const
76   {
77     // This is not animatable; the cached value is up-to-date.
78     return mText;
79   }
80
81   /**
82    * Get the natural text size
83    * @return the natural size of the text.
84    */
85   const Vector2& GetNaturalTextSize() const
86   {
87     // This is not animatable; the cached value is up-to-date.
88     return mTextSize;
89   }
90
91   /**
92    * Set the font used to display the text label displayed by the attachment
93    * @param [in] font The new font
94    */
95   void SetFont(Font& font);
96
97   /**
98    * Get the font used to display the text label displayed by the attachment
99    * @return The font currently in use
100    */
101   Font& GetFont()
102   {
103     return *mFont;
104   }
105
106   /**
107    * Set the text color
108    * @param[in] color The text color
109    */
110   void SetTextColor(const Vector4& color);
111
112   /**
113    * Get the text color
114    * @return The text color
115    */
116   Vector4 GetTextColor() const;
117
118   /**
119    * Resets to default the text color.
120    */
121   void ResetTextColor();
122
123   /**
124    * @copydoc Dali::TextActor::SetWeight()
125    */
126   void SetWeight( TextStyle::Weight weight );
127
128   /**
129    * @copydoc Dali::TextActor::GetWeight()
130    */
131   TextStyle::Weight GetWeight() const;
132
133   /**
134    * Resets to default the text weight.
135    */
136   void ResetWeight();
137
138   /**
139    * @copydoc Dali::TextActor::SetSmoothEdge(const float)
140    */
141   void SetSmoothEdge( float smoothEdge );
142
143   /**
144    * Retrieve the smooth edge value.
145    * @return the smooth edge value.
146    */
147   float GetSmoothEdge() const;
148
149   /**
150    * Resets to default the smooth edge.
151    */
152   void ResetSmoothEdge();
153
154   /**
155    * Enable italics on the text actor, the text will be sheared by the given angle.
156    * @param[in] angle Italics angle in radians.
157    */
158   void SetItalics( Radian angle );
159
160   /**
161    * @copydoc Dali::TextActor::GetItalics()
162    */
163   bool GetItalics() const;
164
165   /**
166    * @copydoc Dali::TextActor::GetItalicsAngle()
167    */
168   Radian GetItalicsAngle() const;
169
170   /**
171    * Resets to default the italics.
172    */
173   void ResetItalics();
174
175   /**
176    * @copydoc Dali::TextActor::SetUnderline()
177    */
178   void SetUnderline( bool enable, float thickness, float position );
179
180   /**
181    * @copydoc Dali::TextActor::GetUnderline()
182    */
183   bool GetUnderline() const;
184
185   /**
186    * @copydoc Internal::TextActor::GetUnderlineThickness()
187    */
188   float GetUnderlineThickness() const;
189
190   /**
191    * @copydoc Internal::TextActor::GetUnderlinePosition()
192    */
193   float GetUnderlinePosition() const;
194
195   /**
196    * Resets to default the text underline.
197    */
198   void ResetUnderline();
199
200   /**
201    * @copydoc Dali::TextActor::SetOutline(const bool,const Vector4&,const Vector2&)
202    */
203   void SetOutline( bool enable, const Vector4& color, const Vector2& thickness );
204
205   /**
206    * Get outline state.
207    * @return \e true if outline is enabled, otherwise \e false.
208    */
209   bool GetOutline() const;
210
211   /**
212    * Retrieve outline parameters.
213    * @param[out] color The outline color.
214    * @param[out] thickness The outline parameters.
215    */
216   void GetOutlineParams( Vector4& color, Vector2& thickness ) const;
217
218   /**
219    * Resets to default the text outline.
220    */
221   void ResetOutline();
222
223   /**
224    * @copydoc Dali::TextActor::SetGlow(const bool,const Vector4&,const float)
225    */
226   void SetGlow( bool enable, const Vector4& color, float intensity );
227
228   /**
229    * Get glow state.
230    * @return \e true if glow is enabled, otherwise \e false.
231    */
232   bool GetGlow() const;
233
234   /**
235    * Retrieve glow parameters.
236    * @param[out] color The glow color.
237    * @param[out] intensity The glow intensity.
238    */
239   void GetGlowParams( Vector4& color, float& intensity ) const;
240
241   /**
242    * Resets to default the text glow.
243    */
244   void ResetGlow();
245
246   /**
247    * @copydoc Dali::TextActor::SetShadow(const bool,const Vector4&,const Vector2&,const float)
248    */
249   void SetShadow( bool enable, const Vector4& color, const Vector2& offset, float size );
250
251   /**
252    * Retrieve the shadow state.
253    * @return \e true if shadow is enabled, otherwise \e false.
254    */
255   bool GetShadow() const;
256
257   /**
258    * Retrieve shadow parameters.
259    * @param[out] color The shadow color.
260    * @param[out] offset The shadow offset.
261    * @param[out] size The shadow size.
262    */
263   void GetShadowParams( Vector4& color, Vector2& offset, float& size ) const;
264
265   /**
266    * Resets to default the text shadow.
267    */
268   void ResetShadow();
269
270   /**
271    * @see Dali::TextActor::SetGradientColor()
272    * @see Dali::TextActor::SetGradientStartPoint()
273    * @see Dali::TextActor::SetGradientEndPoint()
274    */
275   void SetGradient( const Vector4& color, const Vector2& startPoint, const Vector2& endPoint );
276
277   /**
278    * @copydoc Dali::TextActor::GetGradientColor()
279    */
280   const Vector4& GetGradientColor() const;
281
282   /**
283    * @copydoc Dali::TextActor::GetGradientStartPoint()
284    */
285   const Vector2& GetGradientStartPoint() const;
286
287   /**
288    * @copydoc Dali::TextActor::GetGradientEndPoint()
289    */
290   const Vector2& GetGradientEndPoint() const;
291
292   /**
293    * Resets to default the text gradient.
294    */
295   void ResetGradient();
296
297   /**
298    * Retrieves the text style.
299    *
300    * @param[out] style The text style.
301    */
302   void GetTextStyle( TextStyle& style ) const;
303
304   /**
305    * Measure the natural size of a text string, as displayed in this font.
306    * @return The natural size of the text.
307    */
308   Vector3 MeasureText() const;
309
310   /**
311    * Inform the text attachment the text / font it is using has changed.
312    * The TextChanged() and TextNotRequired() functions are used to avoid duplicate
313    * requests for text. For example if the text is changed, and the font is changed
314    * then we want to avoid requesting the new text with the old font, then the new
315    * text with the new font.
316    */
317   void TextChanged();
318
319   /**
320    * @return true if all text has been loaded and is ready to display
321    */
322   bool IsTextLoaded();
323
324   /**
325    * Calculates the weighted smoothing value.
326    */
327   void CalculateWeightedSmoothing( TextStyle::Weight weight, float smoothEdge );
328
329 public: // glyph texture observer
330
331   /**
332    *  @copydoc Dali::Internal::GlyphTextureObserver::TextureResized()
333    */
334   virtual void TextureResized( const TextureIdList& oldTextureIds, unsigned int newTextureId );
335
336   /**
337    * @copydoc Dali::Internal::GlyphTextureObserver::TextureSplit()
338    */
339   virtual void TextureSplit( FontId fontId, const TextureIdList& oldTextureIds, unsigned int newTextureId );
340
341 private:
342
343   /**
344    * Check if the text has been modified
345    * @return true if the text or it's formating has been modified
346    */
347    bool IsTextModified();
348
349   /**
350    * First stage construction of a TextAttachment.
351    * @param[in] stage Used to send messages to scene-graph.
352    */
353   TextAttachment( Stage& stage );
354
355   /**
356    * @copydoc Dali::Internal::RenderableAttachment::OnStageConnection2()
357    */
358   virtual void OnStageConnection2();
359
360   /**
361    * @copydoc Dali::Internal::RenderableAttachment::OnStageDisconnection2()
362    */
363   virtual void OnStageDisconnection2();
364
365   /**
366    * @copydoc Dali::Internal::RenderableAttachment::GetSceneObject()
367    */
368   virtual const SceneGraph::RenderableAttachment& GetSceneObject() const;
369
370   /**
371    * Update any text & font changes to the on stage scene graph text attachment
372    */
373   void SetTextChanges();
374
375 protected:
376
377   /**
378    * A reference counted object may only be deleted by calling Unreference()
379    */
380   virtual ~TextAttachment();
381
382 private:
383
384   const SceneGraph::TextAttachment* mSceneObject; ///< Not owned
385
386   TextRequestHelper mTextRequestHelper;           ///< Text request helper
387   FontPointer mFont;                              ///< The font resource
388   Vector4* mTextColor;                            ///< on demand storage for text color
389   TextStyle mStyle;                               ///< on demand storage for text effects parameters ( do not store font parameters and color )
390                                                   ///< Font parameters are stored in the FontPointer member. Inside the TextStyle, font parameters and color are allocated in the same struct,
391                                                   ///< so store the text color in the style will allocate space for the font parameters as well, wasting space.
392   // Cached values for public getters
393   Integration::TextArray mText;
394
395   bool    mTextChanged:1;       ///< whether the text has changed
396   bool    mFontChanged:1;       ///< whether the font has changed
397   bool    mUnderlineChanged:1;  ///< whether the underline has changed
398   bool    mItalicsChanged:1;    ///< whether the italics has changed
399   bool    mTextureIdSet:1;      ///< flag to signify if the texture id has been set on the scene-graph text-attachment
400   unsigned int mTextureId;      ///< the texture id of the glyph atlas being used
401   Vector2 mTextSize;            ///< text natural size
402
403   OwnerPointer< TextVertexBuffer > mVertexBuffer; ///< vertex data to display the text
404 };
405
406 } // namespace Internal
407
408 } // namespace Dali
409
410 #endif // __DALI_INTERNAL_TEXT_ATTACHMENT_H__