Create Renderer when the Visual is created
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / gradient / gradient-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_GRADIENT_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_GRADIENT_VISUAL_H
3
4 /*
5  * Copyright (c) 2021 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 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/intrusive-ptr.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
26 #include <dali-toolkit/internal/visuals/gradient/gradient.h>
27
28 namespace Dali
29 {
30 class Vector2;
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 class Gradient;
39 class GradientVisual;
40 typedef IntrusivePtr< GradientVisual > GradientVisualPtr;
41
42 /**
43  * The visual which renders smooth transition of colors to the control's quad.
44  * It supports two types of gradients: linear and radial.
45  *
46  * The following properties are essential for create a LINEAR GradientRender
47  *
48  * | %Property Name          | Type             |
49  * |-------------------------|------------------|
50  * | startPosition           | VECTOR2          |
51  * | endPosition             | VECTOR2          |
52  * | stopColor               | ARRAY of VECTOR4 |
53  *
54  * The following properties are essential for create a RADIAL GradientRender
55  *
56  * | %Property Name          | Type             |
57  * |-------------------------|------------------|
58  * | center                  | VECTOR2          |
59  * | radius                  | FLOAT            |
60  * | stopColor               | ARRAY of VECTOR4 |
61  *
62  * The following properties are optional for both LINEAR and RADIAL GradientRender.
63  *
64  * | %Property Name          | Type             |
65  * |-------------------------|------------------|
66  * | stopOffset              | ARRAY of FLOAT   |
67  * | units                   | STRING           |
68  * | spreadMethod            | STRING           |
69  *
70  * Valid values for units are 'userSpace' and 'objectBoundingBox'.
71  * Valid values for spreadMethod are 'pad', 'repeat' and 'reflect.'
72  * If not provided, 'objectBoundingBox' is used as default gradient units, and 'pad' is used as default spread method.
73  */
74 class GradientVisual: public Visual::Base
75 {
76 public:
77
78   /**
79    * Types of the gradient
80    */
81   enum Type
82   {
83     LINEAR,
84     RADIAL
85   };
86
87   /**
88    * @brief Create a new gradient visual.
89    *
90    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
91    * @param[in] properties A Property::Map containing settings for this visual
92    * @return A smart-pointer to the newly allocated visual.
93    */
94   static GradientVisualPtr New( VisualFactoryCache& factoryCache, const Property::Map& properties );
95
96 public:  // from Visual
97
98   /**
99    * @copydoc Visual::Base::CreatePropertyMap
100    */
101   void DoCreatePropertyMap( Property::Map& map ) const override;
102
103   /**
104    * @copydoc Visual::Base::CreateInstancePropertyMap
105    */
106   void DoCreateInstancePropertyMap( Property::Map& map ) const override;
107
108 protected:
109
110   /**
111    * @brief Constructor.
112    *
113    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
114    */
115   GradientVisual( VisualFactoryCache& factoryCache );
116
117   /**
118    * @brief A reference counted object may only be deleted by calling Unreference().
119    */
120   virtual ~GradientVisual();
121
122   /**
123    * @copydoc Visual::Base::OnInitialize
124    */
125   void OnInitialize() override;
126
127   /**
128    * @copydoc Visual::Base::DoSetProperties
129    */
130   void DoSetProperties( const Property::Map& propertyMap ) override;
131
132   /**
133    * @copydoc Visual::Base::OnSetTransform
134    */
135   void OnSetTransform() override;
136
137   /**
138    * @copydoc Visual::Base::DoSetOnScene
139    */
140   void DoSetOnScene( Actor& actor ) override;
141
142   /**
143    * @copydoc Visual::Base::UpdateShader
144    */
145   void UpdateShader() override;
146
147 private:
148
149   /**
150    * New a gradient object with the given property map.
151    *
152    * @return True if the property map provides valid properties to create a gradient. Otherwise, returns false.
153    */
154   bool NewGradient(Type gradientType, const Property::Map& propertyMap);
155
156   /**
157    * @brief Get a shader for the current properties.
158    * @return The shader for the current properties.
159    */
160   Shader GetShader();
161
162   /**
163    * Get the stop-offsets from the property.
164    * The valid property type are ARRAY, VECTOR2, VECTOR3, VECTOR4.
165    *
166    * @param[in] value The property value of stop-offsets
167    * @param[out] stopOffsets The vector contains the stop offset values.
168    */
169   static void GetStopOffsets(const Property::Value* value, Vector<float>& stopOffsets);
170
171   // Undefined
172   GradientVisual( const GradientVisual& gradientVisual );
173
174   // Undefined
175   GradientVisual& operator=( const GradientVisual& gradientVisual );
176
177 private:
178
179   Matrix3 mGradientTransform;
180   IntrusivePtr<Gradient> mGradient;
181   Type mGradientType;
182   bool mIsOpaque; ///< Set to false if any of the stop colors are not opaque
183 };
184
185 } // namespace Internal
186
187 } // namespace Toolkit
188
189 } // namespace Dali
190
191 #endif /* DALI_TOOLKIT_INTERNAL_GRADIENT_VISUAL_H */