Renamed KeyEvent enum values to comply with coding standards.
[platform/core/uifw/dali-demo.git] / examples / property-notification / property-notification-example.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
18 #include <dali/devel-api/actors/actor-devel.h>
19 #include <dali-toolkit/dali-toolkit.h>
20
21 using namespace Dali;
22 using namespace Dali::Toolkit;
23
24 namespace
25 {
26 const float COLOR_ANIMATION_DURATION( 5.0f );
27 const float OPACITY_ANIMATION_DURATION( 1.0f );
28 } // unnamed namespace
29
30 /**
31  * @brief An example that shows how to use property notifications.
32  *
33  * - Creates a text label and sets its text color to black.
34  * - Animates the text color of a text label to red.
35  * - Sets up a property notification so that we are informed when the color is 50% red.
36  * - When we are notified, we start a new animation which animates the text label to transparent.
37  */
38 class PropertyNotificationController : public ConnectionTracker
39 {
40 public:
41
42   /**
43    * @brief Constructor.
44    * @param[in] application A reference to the Application class.
45    */
46   PropertyNotificationController( Application& application )
47   : mApplication( application )
48   {
49     // Connect to the Application's Init signal
50     mApplication.InitSignal().Connect( this, &PropertyNotificationController::Create );
51   }
52
53   /**
54    * @brief Called to initialise the application content
55    * @param[in] application A reference to the Application class.
56    */
57   void Create( Application& application )
58   {
59     // Set the window background color and connect to the window's key signal to allow Back and Escape to exit.
60     Window window = application.GetWindow();
61     window.SetBackgroundColor( Color::WHITE );
62     window.KeyEventSignal().Connect( this, &PropertyNotificationController::OnKeyEvent );
63
64     // Create a text label and set the text color to black
65     mTextLabel = TextLabel::New( "Black to Red Animation\nNew opacity animation at 50% Red" );
66     mTextLabel.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
67     mTextLabel.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
68     mTextLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
69     mTextLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
70     mTextLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLACK );
71     window.Add( mTextLabel );
72
73     // Create an animation and animate the text color to red
74     Animation animation = Animation::New( COLOR_ANIMATION_DURATION );
75     animation.AnimateTo( Property( mTextLabel, TextLabel::Property::TEXT_COLOR ), Color::RED );
76     animation.Play();
77
78     // Set up a property notification so we are notified when the red component of the text-color reaches 50%
79     PropertyNotification notification = mTextLabel.AddPropertyNotification( TextLabel::Property::TEXT_COLOR_RED, GreaterThanCondition( 0.5f ) );
80     notification.NotifySignal().Connect( this, &PropertyNotificationController::RedComponentNotification );
81   }
82
83   /**
84    * @brief Called when any key event is received
85    *
86    * Will use this to quit the application if Back or the Escape key is received
87    * @param[in] event The key event information
88    */
89   void OnKeyEvent( const KeyEvent& event )
90   {
91     if( event.GetState() == KeyEvent::DOWN )
92     {
93       if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
94       {
95         mApplication.Quit();
96       }
97     }
98   }
99
100   /**
101    * @brief Called when the property notification condition is met.
102    *
103    * In our case, it's when the red component is greater than 50%.
104    * Will use this notification to animate the opacity of the text-label to transparent.
105    */
106   void RedComponentNotification( PropertyNotification& /* source */ )
107   {
108     Animation animation = Animation::New( OPACITY_ANIMATION_DURATION );
109     animation.AnimateTo( Property( mTextLabel, Actor::Property::OPACITY ), 0.0f );
110     animation.Play();
111   }
112
113 private:
114   Application& mApplication;
115   TextLabel mTextLabel;
116 };
117
118 int DALI_EXPORT_API main( int argc, char **argv )
119 {
120   Application application = Application::New( &argc, &argv );
121   PropertyNotificationController test( application );
122   application.MainLoop();
123   return 0;
124 }