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.
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <dali/devel-api/common/stage-devel.h>
23 #include "frame-callback.h"
26 using namespace Dali::Toolkit;
30 const char* IMAGE_NAME = DEMO_IMAGE_DIR "application-icon-1.png";
32 const char* TEXT_ENABLED("FrameCallback: ON");
33 const char* TEXT_DISABLED("FrameCallback: OFF");
34 Vector4 TEXT_COLOR_ENABLED(Color::BLACK);
35 Vector4 TEXT_COLOR_DISABLED(Color::RED);
37 float ANIMATION_TIME(4.0f);
38 float ANIMATION_PROGRESS_MULTIPLIER(0.02f);
39 } // unnamed namespace
42 * @brief An example of how to set/unset the FrameCallbackInterface in DALi.
44 * Creates a scene with several image-views which are animated from side-to-side.
45 * With the frame-callback enabled, the image-views' sizes expand as they hits the sides and the opacity
46 * changes to transparent as they go to the middle.
48 class FrameCallbackController : public ConnectionTracker
53 * @param[in] application The application.
55 FrameCallbackController(Application& application)
56 : mApplication(application),
60 mFrameCallbackEnabled(false)
62 // Connect to the Application's Init signal
63 mApplication.InitSignal().Connect(this, &FrameCallbackController::Create);
68 * @brief Creates the scene.
70 * Creates several image-views and places them appropriately.
71 * Animate all image-views.
72 * Set the FrameCallbackInterface on the window.
73 * Tapping on the window enables/disables the FrameCallback.
75 void Create(Application& application)
77 // Set the window background color and connect to the window's key signal to allow Back and Escape to exit.
78 Window window = application.GetWindow();
79 window.SetBackgroundColor(Color::WHITE);
80 window.KeyEventSignal().Connect(this, &FrameCallbackController::OnKeyEvent);
82 // Notify mFrameCallback about the window width.
83 // Can call methods in mFrameCallback directly as we have not set it on the window yet.
84 Vector2 windowSize = window.GetSize();
85 mFrameCallback.SetWindowWidth(windowSize.width);
87 // Detect taps on the root layer.
88 mTapDetector = TapGestureDetector::New();
89 mTapDetector.Attach(window.GetRootLayer());
90 mTapDetector.DetectedSignal().Connect(this, &FrameCallbackController::OnTap);
92 // Create some key-frames to be used by all animations.
93 KeyFrames keyFrames = KeyFrames::New();
94 keyFrames.Add(0.0f, 0.0f);
95 keyFrames.Add(0.25f, windowSize.width * 0.5f);
96 keyFrames.Add(0.75f, -windowSize.width * 0.5f);
97 keyFrames.Add(1.0f, 0.0f);
100 for(int i = 0; yPos < windowSize.height; ++i)
102 ImageView imageView = ImageView::New(IMAGE_NAME);
103 imageView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
104 imageView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
105 imageView.SetProperty(Actor::Property::POSITION_Y, yPos);
106 yPos += imageView.GetNaturalSize().height;
108 // Add the ID of the created ImageView to mFrameCallback.
109 // Again, can call methods in mFrameCallback directly as we have not set it on the window yet.
110 mFrameCallback.AddId(imageView.GetProperty<int>(Actor::Property::ID));
112 window.Add(imageView);
114 // Create an animation and set the progress so that each image starts at a different point.
115 Animation animation = Animation::New(ANIMATION_TIME);
116 animation.SetLooping(true);
117 animation.AnimateBetween(Property(imageView, Actor::Property::POSITION_X), keyFrames);
118 animation.SetCurrentProgress(std::min(1.0f, ANIMATION_PROGRESS_MULTIPLIER * i));
122 // Create a text-label to display whether the FrameCallback is enabled/disabled.
123 mTextLabel = TextLabel::New(TEXT_ENABLED);
124 mTextLabel.SetProperty(TextLabel::Property::TEXT_COLOR, TEXT_COLOR_ENABLED);
125 mTextLabel.SetProperty(TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");
126 mTextLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
127 mTextLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
128 window.Add(mTextLabel);
130 // Set the FrameCallbackInterface on the root layer.
131 DevelStage::AddFrameCallback(Stage::GetCurrent(), mFrameCallback, window.GetRootLayer());
132 mFrameCallbackEnabled = true;
136 * @brief Called when a tap on the window occurs.
138 * Toggle enabling/disabling of the FrameCallbackInterface
140 void OnTap(Actor actor, const TapGesture& /* tap */)
142 if(mFrameCallbackEnabled)
144 DevelStage::RemoveFrameCallback(Stage::GetCurrent(), mFrameCallback);
145 mTextLabel.SetProperty(TextLabel::Property::TEXT, TEXT_DISABLED);
146 mTextLabel.SetProperty(TextLabel::Property::TEXT_COLOR, TEXT_COLOR_DISABLED);
150 DevelStage::AddFrameCallback(Stage::GetCurrent(), mFrameCallback, actor);
151 mTextLabel.SetProperty(TextLabel::Property::TEXT, TEXT_ENABLED);
152 mTextLabel.SetProperty(TextLabel::Property::TEXT_COLOR, TEXT_COLOR_ENABLED);
155 mFrameCallbackEnabled = !mFrameCallbackEnabled;
159 * @brief Called when any key event is received
161 * Will use this to quit the application if Back or the Escape key is received
162 * @param[in] event The key event information
164 void OnKeyEvent(const KeyEvent& event)
166 if(event.GetState() == KeyEvent::DOWN)
168 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
176 Application& mApplication; ///< A reference to the application instance.
177 FrameCallback mFrameCallback; ///< An instance of our implementation of the FrameCallbackInterface.
178 TextLabel mTextLabel; ///< Text label which shows whether the frame-callback is enabled/disabled.
179 TapGestureDetector mTapDetector; ///< Tap detector to enable/disable the FrameCallbackInterface.
180 bool mFrameCallbackEnabled; ///< Stores whether the FrameCallbackInterface is enabled/disabled.
183 int DALI_EXPORT_API main(int argc, char** argv)
185 Application application = Application::New(&argc, &argv);
186 FrameCallbackController controller(application);
187 application.MainLoop();