Merge "Add new layouting support for TextLabel and ImageView." into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-factory-impl.h
1 #ifndef DALI_TOOLKIT_VISUAL_FACTORY_IMPL_H
2 #define DALI_TOOLKIT_VISUAL_FACTORY_IMPL_H
3
4 /*
5  * Copyright (c) 2017 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 // EXTERNAL INCLUDES
21 #include <dali/public-api/object/base-object.h>
22
23 // INTERNAL INCLUDES
24 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
25 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
26 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
27 #include <dali-toolkit/public-api/styling/style-manager.h>
28 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 class VisualFactoryCache;
40
41 /**
42  * @copydoc Toolkit::VisualFactory
43  */
44 class VisualFactory : public BaseObject
45 {
46 public:
47
48   /**
49    * @brief Constructor
50    *
51    * @param[in] debugEnabled If true, use debug renderer to replace all the concrete renderer.
52    */
53   VisualFactory( bool debugEnabled );
54
55   /**
56    * @brief StyleChanged callback
57    *
58    * @param[in] styleManager Handle for style manager.
59    * @param[in] type Style change type.
60    */
61   void OnStyleChangedSignal( Toolkit::StyleManager styleManager, StyleChange::Type type );
62
63   /**
64    * @copydoc Toolkit::VisualFactory::CreateVisual( const Property::Map& )
65    */
66   Toolkit::Visual::Base CreateVisual( const Property::Map& propertyMap );
67
68   /**
69    * @copydoc Toolkit::VisualFactory::CreateVisual( const Image& )
70    */
71   Toolkit::Visual::Base CreateVisual( const Image& image );
72
73   /**
74    * @copydoc Toolkit::VisualFactory::CreateVisual( const std::string&, ImageDimensions )
75    */
76   Toolkit::Visual::Base CreateVisual( const std::string& image, ImageDimensions size );
77
78   /**
79    * @copydoc Toolkit::VisualFactory::SetPreMultiplyOnLoad()
80    */
81   void SetPreMultiplyOnLoad( bool preMultiply );
82
83   /**
84    * @copydoc Toolkit::VisualFactory::GetPreMultiplyOnLoad()
85    */
86   bool GetPreMultiplyOnLoad() const;
87
88   /**
89    * @return the reference to texture manager
90    */
91   Internal::TextureManager& GetTextureManager();
92
93 protected:
94
95   /**
96    * A reference counted object may only be deleted by calling Unreference()
97    */
98   virtual ~VisualFactory();
99
100 private:
101   /**
102    * Get the factory cache, creating it if necessary.
103    */
104   Internal::VisualFactoryCache& GetFactoryCache();
105
106   VisualFactory(const VisualFactory&) = delete;
107
108   VisualFactory& operator=(const VisualFactory& rhs) = delete;
109
110 private:
111   std::unique_ptr<VisualFactoryCache> mFactoryCache;
112   SlotDelegate< VisualFactory >       mSlotDelegate;
113   bool                                mDebugEnabled:1;
114   bool                                mPreMultiplyOnLoad:1; ///< Local store for this flag
115 };
116
117 /**
118  * @brief Template to allow discard old visual, get new one and set it on stage if possible
119  *
120  * @tparam ParameterType0 The type of first argument passed to the CreateVisual()
121  * @tparam ParameterType1 The type of second argument passed to the CreateVisual()
122  * @SINCE_1_0.39
123  * @param[in] actor Actor for which the visual will be replaced
124  * @param[in,out] visual The visual to be replaced
125  * @param[in] param0 First template based argument passed to the visual factory
126  * @param[in] param1 Second template based argument passed to the visual factory
127  */
128 template< class ParameterType0, class ParameterType1 >
129 void InitializeVisual( Actor& actor, Toolkit::Visual::Base& visual, ParameterType0& param0, ParameterType1& param1 )
130 {
131   if( actor )
132   {
133     Toolkit::GetImplementation(visual).SetOffStage( actor );
134   }
135   visual = Toolkit::VisualFactory::Get().CreateVisual( param0, param1 );
136   if( visual && actor && actor.OnStage() )
137   {
138     Toolkit::GetImplementation(visual).SetOnStage(actor);
139   }
140 }
141
142 /**
143  * @brief Template to allow discard old visual, get new one and set it on stage if possible
144  *
145  * @tparam ParameterType The type of argument passed to the CreateVisual()
146  * @SINCE_1_0.39
147  * @param[in] actor Actor for which the visual will be replaced
148  * @param[in,out] visual The visual to be replaced
149  * @param[in] param Template based argument passed to the visual factory
150  */
151 template< class ParameterType >
152 void InitializeVisual( Actor& actor, Toolkit::Visual::Base& visual, ParameterType& param )
153 {
154   if( actor && visual )
155   {
156     Toolkit::GetImplementation(visual).SetOffStage( actor );
157   }
158   visual =  Toolkit::VisualFactory::Get().CreateVisual( param );
159   if( visual && actor && actor.OnStage() )
160   {
161     Toolkit::GetImplementation(visual).SetOnStage(actor);
162   }
163 }
164
165 } // namespace Internal
166
167 inline const Internal::VisualFactory& GetImplementation(const Toolkit::VisualFactory& factory)
168 {
169   DALI_ASSERT_ALWAYS( factory && "VisualFactory handle is empty" );
170
171   const BaseObject& handle = factory.GetBaseObject();
172
173   return static_cast<const Internal::VisualFactory&>(handle);
174 }
175
176 inline Internal::VisualFactory& GetImplementation(Toolkit::VisualFactory& factory)
177 {
178   DALI_ASSERT_ALWAYS( factory && "VisualFactory handle is empty" );
179
180   BaseObject& handle = factory.GetBaseObject();
181
182   return static_cast<Internal::VisualFactory&>(handle);
183 }
184
185 } // namespace Toolkit
186
187 } // namespace Dali
188
189 #endif /* DALI_TOOLKIT_VISUAL_FACTORY_IMPL_H */