2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
22 #include "generated/image-channel-control-frag.h"
26 using namespace Dali; // Needed for macros
34 Dali::BaseHandle Create()
36 return Demo::ImageChannelControl::New();
39 DALI_TYPE_REGISTRATION_BEGIN(ImageChannelControl, Dali::Toolkit::Control, Create);
41 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "url", STRING, RESOURCE_URL);
42 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "redChannel", FLOAT, RED_CHANNEL);
43 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "greenChannel", FLOAT, GREEN_CHANNEL);
44 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "blueChannel", FLOAT, BLUE_CHANNEL);
46 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "visibility", BOOLEAN, VISIBILITY);
47 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "enableVisibilityTransition", ARRAY, ENABLE_VISIBILITY_TRANSITION);
48 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "disableVisibilityTransition", ARRAY, DISABLE_VISIBILITY_TRANSITION);
50 DALI_PROPERTY_REGISTRATION(Demo, ImageChannelControl, "imageVisual", MAP, IMAGE_VISUAL);
51 DALI_TYPE_REGISTRATION_END();
53 } // anonymous namespace
55 Internal::ImageChannelControl::ImageChannelControl()
56 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
57 mChannels(1.0f, 1.0f, 1.0f),
58 mChannelIndex(Property::INVALID_INDEX),
60 mTargetVisibility(true)
64 Internal::ImageChannelControl::~ImageChannelControl()
68 Demo::ImageChannelControl Internal::ImageChannelControl::New()
70 IntrusivePtr<Internal::ImageChannelControl> impl = new Internal::ImageChannelControl();
71 Demo::ImageChannelControl handle = Demo::ImageChannelControl(*impl);
76 void ImageChannelControl::SetImage(const std::string& url)
82 Property::Map properties;
84 shader[Dali::Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = Dali::Shader::GetFragmentShaderPrefix() + SHADER_IMAGE_CHANNEL_CONTROL_FRAG.data();
85 properties[Dali::Toolkit::Visual::Property::TYPE] = Dali::Toolkit::Visual::IMAGE;
86 properties[Dali::Toolkit::Visual::Property::SHADER] = shader;
87 properties[Dali::Toolkit::ImageVisual::Property::URL] = url;
89 mVisual = Toolkit::VisualFactory::Get().CreateVisual(properties);
90 Toolkit::DevelControl::RegisterVisual(*this, Demo::ImageChannelControl::Property::IMAGE_VISUAL, mVisual);
91 mVisual.SetName("imageVisual");
96 void ImageChannelControl::SetVisibility(bool visibility)
98 printf("ImageChannelControl %s: SetVisibility( %s )\n", Self().GetProperty<std::string>(Dali::Actor::Property::NAME).c_str(), visibility ? "T" : "F");
103 mAnimation.FinishedSignal().Disconnect(this, &ImageChannelControl::OnStateChangeAnimationFinished);
104 OnStateChangeAnimationFinished(mAnimation);
107 if(mVisibility != visibility)
111 if(mDisableVisibilityTransition.Count() > 0)
113 mAnimation = Toolkit::DevelControl::CreateTransition(*this, mDisableVisibilityTransition);
118 if(mEnableVisibilityTransition.Count() > 0)
120 mAnimation = Toolkit::DevelControl::CreateTransition(*this, mEnableVisibilityTransition);
127 mAnimation.FinishedSignal().Connect(this, &ImageChannelControl::OnStateChangeAnimationFinished);
129 mTargetVisibility = visibility;
133 mVisibility = visibility;
137 void ImageChannelControl::OnStateChangeAnimationFinished(Animation& src)
139 mVisibility = mTargetVisibility;
142 void ImageChannelControl::OnInitialize()
145 mChannelIndex = self.RegisterProperty("uChannels", Vector3(1.0f, 1.0f, 1.0f));
148 void ImageChannelControl::OnSceneConnection(int depth)
150 Control::OnSceneConnection(depth);
153 void ImageChannelControl::OnSceneDisconnection()
155 Control::OnSceneDisconnection();
158 void ImageChannelControl::OnSizeSet(const Vector3& targetSize)
160 Control::OnSizeSet(targetSize);
164 Vector2 size(targetSize);
165 Property::Map transformMap;
167 .Add(Toolkit::Visual::Transform::Property::OFFSET, Vector2(0.0f, 0.0f))
168 .Add(Toolkit::Visual::Transform::Property::SIZE, Vector2(1.0f, 1.0f))
169 .Add(Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::CENTER)
170 .Add(Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER)
171 .Add(Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2(Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE))
172 .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2(Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE));
174 mVisual.SetTransformAndSize(transformMap, size);
178 Vector3 ImageChannelControl::GetNaturalSize()
183 mVisual.GetNaturalSize(naturalSize);
184 return Vector3(naturalSize);
186 return Vector3::ZERO;
189 void ImageChannelControl::OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change)
192 Control::OnStyleChange(styleManager, change);
195 ///////////////////////////////////////////////////////////
200 void ImageChannelControl::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
202 Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast(Dali::BaseHandle(object));
204 if(imageChannelControl)
206 ImageChannelControl& impl = GetImpl(imageChannelControl);
207 Actor self = impl.Self();
210 case Demo::ImageChannelControl::Property::RESOURCE_URL:
212 impl.SetImage(value.Get<std::string>());
215 case Demo::ImageChannelControl::Property::IMAGE_VISUAL:
217 const Property::Map* map = value.GetMap();
220 impl.mVisual = Toolkit::VisualFactory::Get().CreateVisual(*map);
221 Toolkit::DevelControl::RegisterVisual(impl, Demo::ImageChannelControl::Property::IMAGE_VISUAL, impl.mVisual);
225 case Demo::ImageChannelControl::Property::VISIBILITY:
227 impl.SetVisibility(value.Get<bool>());
230 case Demo::ImageChannelControl::Property::ENABLE_VISIBILITY_TRANSITION:
232 if(value.GetType() == Property::ARRAY)
234 impl.mEnableVisibilityTransition = Toolkit::TransitionData::New(*value.GetArray());
236 else if(value.GetType() == Property::MAP)
238 impl.mEnableVisibilityTransition = Toolkit::TransitionData::New(*value.GetMap());
242 case Demo::ImageChannelControl::Property::DISABLE_VISIBILITY_TRANSITION:
244 if(value.GetType() == Property::ARRAY)
246 impl.mDisableVisibilityTransition = Toolkit::TransitionData::New(*value.GetArray());
248 else if(value.GetType() == Property::MAP)
250 impl.mDisableVisibilityTransition = Toolkit::TransitionData::New(*value.GetMap());
254 case Demo::ImageChannelControl::Property::RED_CHANNEL:
256 impl.mChannels[0] = value.Get<float>();
257 self.SetProperty(impl.mChannelIndex, impl.mChannels);
260 case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
262 impl.mChannels[1] = value.Get<float>();
263 self.SetProperty(impl.mChannelIndex, impl.mChannels);
266 case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
268 impl.mChannels[2] = value.Get<float>();
269 self.SetProperty(impl.mChannelIndex, impl.mChannels);
276 Property::Value ImageChannelControl::GetProperty(BaseObject* object, Property::Index propertyIndex)
278 Property::Value value;
280 Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast(Dali::BaseHandle(object));
282 if(imageChannelControl)
284 ImageChannelControl& impl = GetImpl(imageChannelControl);
285 switch(propertyIndex)
287 case Demo::ImageChannelControl::Property::RED_CHANNEL:
289 value = impl.mChannels[0];
292 case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
294 value = impl.mChannels[1];
297 case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
299 value = impl.mChannels[2];
302 case Demo::ImageChannelControl::Property::VISIBILITY:
304 value = impl.mVisibility;
315 } // namespace Internal