2 * Copyright (c) 2020 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.
21 #include "shared/view.h"
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/devel-api/controls/effects-view/effects-view.h>
25 #include <dali/dali.h>
29 using namespace Dali::Toolkit;
33 const char* const TITLE("EffectsView: effect size = ");
34 const char* TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png");
35 const char* VIEW_SWAP_IMAGE(DEMO_IMAGE_DIR "icon-change.png");
36 const char* VIEW_SWAP_SELECTED_IMAGE(DEMO_IMAGE_DIR "icon-change-selected.png");
37 const char* TEST_IMAGE(DEMO_IMAGE_DIR "Kid1.svg");
40 // This example illustrates the capabilities of the EffectsView container
42 class EffectsViewApp : public ConnectionTracker
48 EffectsViewApp(Application& application);
56 * Initialisation. This method gets called once the main loop of application is up and running
58 void OnAppInitialize(Application& application);
61 * Create a effect view of drop shadow
63 * @param[in] type The type of effect to be performed by the EffectView.
64 * @param[in] viewSize Size of the effect view
65 * @param[in] effectSize The effect size used in image filters.
67 EffectsView CreateEffectsView(EffectsView::EffectType type, const Vector2& viewSize, int effectSize);
70 * Animate the effect offset and color properties.
71 * @param[in] effectsView The view whose properties to be animated.
73 void AnimateEffectProperties(EffectsView& effectsView);
76 * Set title onto the toolbar
77 * @param[in] effectSize The effect size value to be indicated on the title
79 void SetTitle(int effectSize);
82 * Callback function to change the effect size.
83 * @param[in] button The button which triggered the callback.
85 bool ChangeEffectSize(Button button);
88 * Main key event handler
90 void OnKeyEvent(const KeyEvent& event);
93 Application& mApplication;
95 Toolkit::Control mView;
96 Toolkit::ToolBar mToolBar;
97 EffectsView mDropShadowView;
98 EffectsView mEmbossView;
99 Toolkit::TextLabel mTitleActor; ///< The title on the toolbar
104 EffectsViewApp::EffectsViewApp(Application& application)
105 : mApplication(application),
108 // Connect to the Application's Init signal
109 mApplication.InitSignal().Connect(this, &EffectsViewApp::OnAppInitialize);
112 EffectsViewApp::~EffectsViewApp()
114 // Nothing to do here;
117 void EffectsViewApp::OnAppInitialize(Application& application)
119 // The Init signal is received once (only) during the Application lifetime
121 auto window = application.GetWindow();
122 window.KeyEventSignal().Connect(this, &EffectsViewApp::OnKeyEvent);
123 window.SetBackgroundColor(Color::WHITE);
125 mWindowSize = window.GetSize();
127 // Creates a default view with a default tool bar.
128 // The view is added to the window.
129 mContents = DemoHelper::CreateView(application, mView, mToolBar, "", TOOLBAR_IMAGE, "");
131 // Creates view change button.
132 Toolkit::PushButton viewButton = Toolkit::PushButton::New();
133 viewButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, VIEW_SWAP_IMAGE);
134 viewButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, VIEW_SWAP_SELECTED_IMAGE);
135 // Connects the view change button clicked signal to the OnView method.
136 viewButton.ClickedSignal().Connect(this, &EffectsViewApp::ChangeEffectSize);
137 mToolBar.AddControl(viewButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_RIGHT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING);
139 Vector2 effectsViewSize(mWindowSize.width, mWindowSize.height * 0.25f);
140 mDropShadowView = CreateEffectsView(EffectsView::DROP_SHADOW, effectsViewSize, mEffectSize);
141 mDropShadowView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
142 mDropShadowView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
143 mDropShadowView.SetProperty(Actor::Property::POSITION_Z, -mWindowSize.height * 0.1f);
144 mContents.Add(mDropShadowView);
146 mEmbossView = CreateEffectsView(EffectsView::EMBOSS, effectsViewSize, mEffectSize);
147 mEmbossView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
148 mEmbossView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
149 mEmbossView.SetProperty(Actor::Property::POSITION_Z, mWindowSize.height * 0.1f);
150 mContents.Add(mEmbossView);
152 SetTitle(mEffectSize);
155 EffectsView EffectsViewApp::CreateEffectsView(EffectsView::EffectType type, const Vector2& viewSize, int effectSize)
157 Toolkit::EffectsView effectsView = Toolkit::EffectsView::New(type);
159 effectsView.SetProperty(Actor::Property::SIZE, Vector2(viewSize.width, viewSize.height));
160 // set effect size property
161 effectsView.SetProperty(EffectsView::Property::EFFECT_SIZE, effectSize);
163 // Create some content
165 std::string text = (type == EffectsView::DROP_SHADOW) ? "Drop Shadow" : "Emboss";
166 TextLabel textActor(TextLabel::New(text));
167 textActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER_LEFT);
168 textActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_LEFT);
169 textActor.SetProperty(Actor::Property::SIZE, viewSize);
170 textActor.SetProperty(Actor::Property::POSITION, Vector2(viewSize.width * 0.4f, viewSize.height * 0.3f));
171 textActor.SetProperty(TextLabel::Property::POINT_SIZE, 14.f);
172 effectsView.Add(textActor);
175 ImageView icon = ImageView::New(TEST_IMAGE);
176 icon.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER_LEFT);
177 icon.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_LEFT);
178 icon.SetProperty(Actor::Property::POSITION_X, viewSize.width * 0.1f);
179 icon.SetProperty(Actor::Property::SIZE, Vector2(viewSize.height * 0.8f, viewSize.height * 0.8f));
180 effectsView.Add(icon);
182 AnimateEffectProperties(effectsView);
187 void EffectsViewApp::AnimateEffectProperties(EffectsView& effectsView)
189 const float animationTime(5.0f);
190 Animation animation(Animation::New(animationTime));
192 animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_OFFSET), Vector3(2.f, -2.f, 0.0f), TimePeriod(animationTime * 0.0f, animationTime * 0.2f));
193 animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_OFFSET), Vector3(-2.f, -2.f, 0.0f), TimePeriod(animationTime * 0.2f, animationTime * 0.2f));
194 animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_OFFSET), Vector3(-2.f, 2.f, 0.0f), TimePeriod(animationTime * 0.4f, animationTime * 0.2f));
195 animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_OFFSET), Vector3(4.f, 4.f, 0.0f), TimePeriod(animationTime * 0.6f, animationTime * 0.2f));
196 animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_OFFSET), Vector3::ZERO, TimePeriod(animationTime * 0.8f, animationTime * 0.2f));
198 effectsView.SetProperty(EffectsView::Property::EFFECT_COLOR, Color::BLACK);
199 animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_COLOR), Color::BLUE, TimePeriod(animationTime * 0.0f, animationTime * 0.33f));
200 animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_COLOR), Color::RED, TimePeriod(animationTime * 0.33f, animationTime * 0.33f));
201 animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_COLOR), Color::BLACK, TimePeriod(animationTime * 0.66f, animationTime * 0.34f));
203 animation.SetLooping(true);
207 void EffectsViewApp::SetTitle(int effectSize)
209 std::ostringstream title;
210 title << TITLE << effectSize;
214 mTitleActor = DemoHelper::CreateToolBarLabel(title.str());
215 // Add title to the tool bar.
216 mToolBar.AddControl(mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HORIZONTAL_CENTER);
218 mTitleActor.SetProperty(Toolkit::TextLabel::Property::TEXT, title.str());
221 bool EffectsViewApp::ChangeEffectSize(Button button)
223 mEffectSize = (mEffectSize + 1) % 5;
224 mDropShadowView.SetProperty(EffectsView::Property::EFFECT_SIZE, mEffectSize);
225 mEmbossView.SetProperty(EffectsView::Property::EFFECT_SIZE, mEffectSize);
226 SetTitle(mEffectSize);
231 void EffectsViewApp::OnKeyEvent(const KeyEvent& event)
233 if(event.GetState() == KeyEvent::DOWN)
235 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
242 /*****************************************************************************/
244 int DALI_EXPORT_API main(int argc, char** argv)
246 Application application = Application::New(&argc, &argv, DEMO_THEME_PATH);
247 EffectsViewApp test(application);
248 application.MainLoop();