d9c4d579e2af1e5d77f36178b8ae3c51b0ecbfec
[platform/core/uifw/dali-demo.git] / examples / contact-cards / contact-cards-example.cpp
1 /*
2  * Copyright (c) 2016 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 // EXTERNAL INCLUDES
19 #include <vector>
20 #include <dali/public-api/adaptor-framework/application.h>
21 #include <dali/public-api/adaptor-framework/key.h>
22 #include <dali/public-api/common/stage.h>
23 #include <dali/public-api/events/key-event.h>
24
25 // INTERNAL INCLUDES
26 #include "contact-card-layouter.h"
27 #include "contact-data.h"
28
29 using namespace Dali;
30
31 namespace
32 {
33 const Vector4 STAGE_COLOR( 211.0f / 255.0f, 211.0f / 255.0f, 211.0f / 255.0f, 1.0f ); ///< The color of the stage
34 const char * const THEME_PATH( DEMO_STYLE_DIR "contact-cards-example-theme.json" ); ///< The theme used for this example
35 } // unnamed namespace
36
37 /**
38  * @brief Creates several contact cards that animate between a folded and unfolded state.
39  *
40  * This demonstrates how different animations can start and stop at different times within the same Animation function.
41  * Additionally, this also shows how to morph between two different geometries.
42  *
43  * ContactCardLayouter: This class is used to lay out the different contact cards on the screen.
44  *                      This takes stage size into account but does not support relayouting.
45  * ContactCard: This class represents each contact card on the screen.
46  *              Two animations are set up in this class which animate several properties with multiple start and stop times.
47  *              An overview of the two animations can be found in contact-card.cpp.
48  * ContactCardLayoutInfo: This is a structure to store common layout information and is created by the ContactCardLayouter and used by each ContactCard.
49  * ContactData: This namespace contains a table which has the contact information we use to populate the contact cards.
50  * ClippedImage: This namespace provides a helper function which creates an ImageView which is added to a control that has clipping.
51  *               This clipping comes in the form of a Circle or Quad.
52  *               The Vertex shader mixes in the Circle and Quad geometry depending on the value of a uniform float.
53  *               Animating this float between CIRCLE_GEOMETRY and QUAD_GEOMETRY is what enables the morphing between the two geometries.
54  */
55 class ContactCardController : public ConnectionTracker // Inherit from ConnectionTracker so that our signals can be automatically disconnected upon our destruction.
56 {
57 public:
58
59   /**
60    * @brief Constructor.
61    * @param[in]  application A reference to the Application class.
62    */
63   ContactCardController( Application& application )
64   : mApplication( application )
65   {
66     // Connect to the Application's Init signal
67     mApplication.InitSignal().Connect( this, &ContactCardController::Create );
68   }
69
70 private:
71
72   /**
73    * @brief Called to initialise the application content
74    * @param[in] application A reference to the Application class.
75    */
76   void Create( Application& application )
77   {
78     // Hide the indicator bar
79     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
80
81     // Set the stage background color and connect to the stage's key signal to allow Back and Escape to exit.
82     Stage stage = Stage::GetCurrent();
83     stage.SetBackgroundColor( STAGE_COLOR );
84     stage.KeyEventSignal().Connect( this, &ContactCardController::OnKeyEvent );
85
86     // Add all the contacts to the layouter
87     for( size_t i = 0; i < ContactData::TABLE_SIZE; ++i )
88     {
89       mContactCardLayouter.AddContact( ContactData::TABLE[ i ].name, ContactData::TABLE[ i ].address, ContactData::TABLE[ i ].imagePath );
90     }
91   }
92
93   /**
94    * @brief Called when any key event is received
95    *
96    * Will use this to quit the application if Back or the Escape key is received
97    * @param[in] event The key event information
98    */
99   void OnKeyEvent( const KeyEvent& event )
100   {
101     if( event.state == KeyEvent::Down )
102     {
103       if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
104       {
105         mApplication.Quit();
106       }
107     }
108   }
109
110   Application& mApplication; ///< Reference to the application class.
111   ContactCardLayouter mContactCardLayouter; ///< The contact card layouter.
112 };
113
114 int DALI_EXPORT_API main( int argc, char **argv )
115 {
116   Application application = Application::New( &argc, &argv, THEME_PATH );
117   ContactCardController contactCardController( application );
118   application.MainLoop();
119   return 0;
120 }