Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / styling / image-channel-control-impl.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "image-channel-control-impl.h"
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/control-devel.h>
20 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
21
22 #include <cstdio>
23
24 using namespace Dali; // Needed for macros
25
26 namespace Demo
27 {
28 namespace Internal
29 {
30 namespace
31 {
32 // clang-format off
33
34 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
35   varying mediump vec2 vTexCoord;\n
36   uniform sampler2D sTexture;\n
37   uniform mediump vec4 uColor;\n
38   uniform mediump vec3 mixColor;\n
39   uniform mediump vec3 uChannels;\n
40   \n
41   void main()\n
42   {\n
43       gl_FragColor = texture2D( sTexture, vTexCoord ) * vec4(mixColor,1.0) * uColor * vec4(uChannels, 1.0) ;\n
44   }\n
45 );
46 // clang-format on
47
48 Dali::BaseHandle Create()
49 {
50   return Demo::ImageChannelControl::New();
51 }
52
53 DALI_TYPE_REGISTRATION_BEGIN(ImageChannelControl, Dali::Toolkit::Control, Create);
54
55 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "url", STRING, RESOURCE_URL);
56 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "redChannel", FLOAT, RED_CHANNEL);
57 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "greenChannel", FLOAT, GREEN_CHANNEL);
58 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "blueChannel", FLOAT, BLUE_CHANNEL);
59
60 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "visibility", BOOLEAN, VISIBILITY);
61 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "enableVisibilityTransition", ARRAY, ENABLE_VISIBILITY_TRANSITION);
62 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "disableVisibilityTransition", ARRAY, DISABLE_VISIBILITY_TRANSITION);
63
64 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "imageVisual", MAP, IMAGE_VISUAL);
65 DALI_TYPE_REGISTRATION_END();
66
67 } // anonymous namespace
68
69 Internal::ImageChannelControl::ImageChannelControl()
70 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
71   mChannels(1.0f, 1.0f, 1.0f),
72   mChannelIndex(Property::INVALID_INDEX),
73   mVisibility(true),
74   mTargetVisibility(true)
75 {
76 }
77
78 Internal::ImageChannelControl::~ImageChannelControl()
79 {
80 }
81
82 Demo::ImageChannelControl Internal::ImageChannelControl::New()
83 {
84   IntrusivePtr<Internal::ImageChannelControl> impl   = new Internal::ImageChannelControl();
85   Demo::ImageChannelControl                   handle = Demo::ImageChannelControl(*impl);
86   impl->Initialize();
87   return handle;
88 }
89
90 void ImageChannelControl::SetImage(const std::string& url)
91 {
92   mUrl = url;
93
94   Actor self = Self();
95
96   Property::Map properties;
97   Property::Map shader;
98   shader[Dali::Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = FRAGMENT_SHADER;
99   properties[Dali::Toolkit::Visual::Property::TYPE]                = Dali::Toolkit::Visual::IMAGE;
100   properties[Dali::Toolkit::Visual::Property::SHADER]              = shader;
101   properties[Dali::Toolkit::ImageVisual::Property::URL]            = url;
102
103   mVisual = Toolkit::VisualFactory::Get().CreateVisual(properties);
104   Toolkit::DevelControl::RegisterVisual(*this, Demo::ImageChannelControl::Property::IMAGE_VISUAL, mVisual);
105   mVisual.SetName("imageVisual");
106
107   RelayoutRequest();
108 }
109
110 void ImageChannelControl::SetVisibility(bool visibility)
111 {
112   printf("ImageChannelControl %s: SetVisibility( %s )\n", Self().GetProperty<std::string>(Dali::Actor::Property::NAME).c_str(), visibility ? "T" : "F");
113
114   if(mAnimation)
115   {
116     mAnimation.Stop();
117     mAnimation.FinishedSignal().Disconnect(this, &ImageChannelControl::OnStateChangeAnimationFinished);
118     OnStateChangeAnimationFinished(mAnimation);
119   }
120
121   if(mVisibility != visibility)
122   {
123     if(mVisibility)
124     {
125       if(mDisableVisibilityTransition.Count() > 0)
126       {
127         mAnimation = Toolkit::DevelControl::CreateTransition(*this, mDisableVisibilityTransition);
128       }
129     }
130     else
131     {
132       if(mEnableVisibilityTransition.Count() > 0)
133       {
134         mAnimation = Toolkit::DevelControl::CreateTransition(*this, mEnableVisibilityTransition);
135       }
136     }
137   }
138
139   if(mAnimation)
140   {
141     mAnimation.FinishedSignal().Connect(this, &ImageChannelControl::OnStateChangeAnimationFinished);
142     mAnimation.Play();
143     mTargetVisibility = visibility;
144   }
145   else
146   {
147     mVisibility = visibility;
148   }
149 }
150
151 void ImageChannelControl::OnStateChangeAnimationFinished(Animation& src)
152 {
153   mVisibility = mTargetVisibility;
154 }
155
156 void ImageChannelControl::OnInitialize()
157 {
158   Actor self    = Self();
159   mChannelIndex = self.RegisterProperty("uChannels", Vector3(1.0f, 1.0f, 1.0f));
160 }
161
162 void ImageChannelControl::OnSceneConnection(int depth)
163 {
164   Control::OnSceneConnection(depth);
165 }
166
167 void ImageChannelControl::OnSceneDisconnection()
168 {
169   Control::OnSceneDisconnection();
170 }
171
172 void ImageChannelControl::OnSizeSet(const Vector3& targetSize)
173 {
174   Control::OnSizeSet(targetSize);
175
176   if(mVisual)
177   {
178     Vector2       size(targetSize);
179     Property::Map transformMap;
180     transformMap
181       .Add(Toolkit::Visual::Transform::Property::OFFSET, Vector2(0.0f, 0.0f))
182       .Add(Toolkit::Visual::Transform::Property::SIZE, Vector2(1.0f, 1.0f))
183       .Add(Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::CENTER)
184       .Add(Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER)
185       .Add(Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2(Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE))
186       .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2(Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE));
187
188     mVisual.SetTransformAndSize(transformMap, size);
189   }
190 }
191
192 Vector3 ImageChannelControl::GetNaturalSize()
193 {
194   if(mVisual)
195   {
196     Vector2 naturalSize;
197     mVisual.GetNaturalSize(naturalSize);
198     return Vector3(naturalSize);
199   }
200   return Vector3::ZERO;
201 }
202
203 void ImageChannelControl::OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change)
204 {
205   // Chain up.
206   Control::OnStyleChange(styleManager, change);
207 }
208
209 ///////////////////////////////////////////////////////////
210 //
211 // Properties
212 //
213
214 void ImageChannelControl::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
215 {
216   Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast(Dali::BaseHandle(object));
217
218   if(imageChannelControl)
219   {
220     ImageChannelControl& impl = GetImpl(imageChannelControl);
221     Actor                self = impl.Self();
222     switch(index)
223     {
224       case Demo::ImageChannelControl::Property::RESOURCE_URL:
225       {
226         impl.SetImage(value.Get<std::string>());
227         break;
228       }
229       case Demo::ImageChannelControl::Property::IMAGE_VISUAL:
230       {
231         Property::Map* map = value.GetMap();
232         if(map)
233         {
234           impl.mVisual = Toolkit::VisualFactory::Get().CreateVisual(*map);
235           Toolkit::DevelControl::RegisterVisual(impl, Demo::ImageChannelControl::Property::IMAGE_VISUAL, impl.mVisual);
236         }
237         break;
238       }
239       case Demo::ImageChannelControl::Property::VISIBILITY:
240       {
241         impl.SetVisibility(value.Get<bool>());
242         break;
243       }
244       case Demo::ImageChannelControl::Property::ENABLE_VISIBILITY_TRANSITION:
245       {
246         if(value.GetType() == Property::ARRAY)
247         {
248           impl.mEnableVisibilityTransition = Toolkit::TransitionData::New(*value.GetArray());
249         }
250         else if(value.GetType() == Property::MAP)
251         {
252           impl.mEnableVisibilityTransition = Toolkit::TransitionData::New(*value.GetMap());
253         }
254         break;
255       }
256       case Demo::ImageChannelControl::Property::DISABLE_VISIBILITY_TRANSITION:
257       {
258         if(value.GetType() == Property::ARRAY)
259         {
260           impl.mDisableVisibilityTransition = Toolkit::TransitionData::New(*value.GetArray());
261         }
262         else if(value.GetType() == Property::MAP)
263         {
264           impl.mDisableVisibilityTransition = Toolkit::TransitionData::New(*value.GetMap());
265         }
266         break;
267       }
268       case Demo::ImageChannelControl::Property::RED_CHANNEL:
269       {
270         impl.mChannels[0] = value.Get<float>();
271         self.SetProperty(impl.mChannelIndex, impl.mChannels);
272         break;
273       }
274       case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
275       {
276         impl.mChannels[1] = value.Get<float>();
277         self.SetProperty(impl.mChannelIndex, impl.mChannels);
278         break;
279       }
280       case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
281       {
282         impl.mChannels[2] = value.Get<float>();
283         self.SetProperty(impl.mChannelIndex, impl.mChannels);
284         break;
285       }
286     }
287   }
288 }
289
290 Property::Value ImageChannelControl::GetProperty(BaseObject* object, Property::Index propertyIndex)
291 {
292   Property::Value value;
293
294   Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast(Dali::BaseHandle(object));
295
296   if(imageChannelControl)
297   {
298     ImageChannelControl& impl = GetImpl(imageChannelControl);
299     switch(propertyIndex)
300     {
301       case Demo::ImageChannelControl::Property::RED_CHANNEL:
302       {
303         value = impl.mChannels[0];
304         break;
305       }
306       case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
307       {
308         value = impl.mChannels[1];
309         break;
310       }
311       case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
312       {
313         value = impl.mChannels[2];
314         break;
315       }
316       case Demo::ImageChannelControl::Property::VISIBILITY:
317       {
318         value = impl.mVisibility;
319         break;
320       }
321       default:
322         break;
323     }
324   }
325
326   return value;
327 }
328
329 } // namespace Internal
330 } // namespace Demo