Changed some examples to use initializer list for Maps & Arrays
[platform/core/uifw/dali-demo.git] / examples / layouting / layouting-examples.cpp
1 /*
2  * Copyright (c) 2019 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 <memory>
20 #include <string>
21 #include <dali/dali.h>
22 #include <dali-toolkit/dali-toolkit.h>
23 #include <dali-toolkit/devel-api/controls/control-devel.h>
24
25 // INTERNAL INCLUDES
26 #include "shared/view.h"
27 #include "animation-example.h"
28 #include "linear-example.h"
29 #include "padding-example.h"
30 #include "flex-example.h"
31 #include "grid-example.h"
32 #include "example.h"
33 #include "absolute-example.h"
34
35 using namespace Dali;
36 using namespace Dali::Toolkit;
37
38 namespace
39 {
40 const Property::Value BACKGROUND
41 {
42   { Toolkit::Visual::Property::TYPE, Visual::GRADIENT },
43   { GradientVisual::Property::STOP_COLOR, Property::Array{ Vector4( 0.0f, 0.352941176f, 0.654901961f, 1.0f ),
44                                                            Vector4( 1.0f, 0.992156863f, 0.894117647f, 1.0f ) } },
45   { GradientVisual::Property::START_POSITION, Vector2( 0.0f, -0.5f ) },
46   { GradientVisual::Property::END_POSITION,   Vector2( 0.0f,  0.5f ) }
47 };
48
49 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
50
51 typedef std::unique_ptr< Demo::Example > ExamplePointer;
52
53 typedef std::vector< ExamplePointer > ExampleContainer;
54
55 /// All layouting examples to be shown should be added to this method
56 void CreateExamples( ExampleContainer& container )
57 {
58   container.push_back( ExamplePointer(new Demo::AnimationExample) );
59   container.push_back( ExamplePointer(new Demo::LinearExample) );
60   container.push_back( ExamplePointer(new Demo::PaddingExample) );
61   container.push_back( ExamplePointer(new Demo::AbsoluteExample) );
62   container.push_back( ExamplePointer(new Demo::FlexExample) ) ;
63   container.push_back( ExamplePointer(new Demo::GridExample) ) ;
64 }
65
66 } // anonymous namespace
67
68 class LayoutingExample: public ConnectionTracker
69 {
70  public:
71
72   LayoutingExample( Application& application )
73   : mApplication( application ),
74     mLayoutingExamples(),
75     mLayoutIndex( 0 )
76   {
77     // Connect to the Application's Init signal
78     mApplication.InitSignal().Connect( this, &LayoutingExample::Create );
79   }
80
81 private:
82
83   void Create( Application& application )
84   {
85     auto stage = Stage::GetCurrent();
86     stage.KeyEventSignal().Connect( this, &LayoutingExample::OnKeyEvent );
87
88     auto bg = Control::New();
89     bg.SetParentOrigin( ParentOrigin::CENTER );
90     bg.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
91     bg.SetProperty( Control::Property::BACKGROUND, BACKGROUND );
92     stage.Add( bg );
93     auto toolbar = ImageView::New( TOOLBAR_IMAGE );
94     toolbar.SetParentOrigin( ParentOrigin::TOP_CENTER );
95     toolbar.SetAnchorPoint( AnchorPoint::TOP_CENTER );
96     toolbar.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
97     toolbar.SetProperty( Actor::Property::SIZE_HEIGHT, 50.0f);
98
99     stage.Add( toolbar );
100
101     mToolbarTitle = TextLabel::New( "");
102     mToolbarTitle.SetParentOrigin( ParentOrigin::CENTER );
103     mToolbarTitle.SetAnchorPoint( AnchorPoint::CENTER );
104     mToolbarTitle.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLUE );
105     mToolbarTitle.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, HorizontalAlignment::LEFT );
106     toolbar.Add( mToolbarTitle );
107
108     mNextLayout = PushButton::New();
109     mNextLayout.SetStyleName( "ChangeLayoutButton" );
110     mNextLayout.SetProperty( Toolkit::Button::Property::LABEL, "Change Layout" );
111     mNextLayout.ClickedSignal().Connect( this, &LayoutingExample::ChangeLayout );
112     mNextLayout.SetParentOrigin( ParentOrigin::TOP_RIGHT );
113     mNextLayout.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
114     mNextLayout.SetSize(175, 50);
115     toolbar.Add( mNextLayout );
116
117     CreateExamples( mLayoutingExamples );
118     if( ! mLayoutingExamples.empty() )
119     {
120       mLayoutingExamples[ mLayoutIndex ]->Create();
121       mToolbarTitle.SetProperty(Toolkit::TextLabel::Property::TEXT, mLayoutingExamples[ mLayoutIndex ]->GetExampleTitle() );
122     }
123   }
124
125   bool ChangeLayout( Button button )
126   {
127     if( ! mLayoutingExamples.empty() )
128     {
129       mLayoutingExamples[ mLayoutIndex ]->Remove();
130       mLayoutIndex = ( mLayoutIndex + 1 ) % mLayoutingExamples.size();
131       mLayoutingExamples[ mLayoutIndex ]->Create();
132       mToolbarTitle.SetProperty(Toolkit::TextLabel::Property::TEXT, mLayoutingExamples[ mLayoutIndex ]->Example::GetExampleTitle() );
133     }
134     return true;
135   }
136
137   /**
138    * Main key event handler
139    */
140   void OnKeyEvent(const KeyEvent& event)
141   {
142     if(event.state == KeyEvent::Down)
143     {
144       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
145       {
146         mApplication.Quit();
147       }
148     }
149   }
150
151
152 private:
153   Application& mApplication;
154   ExampleContainer mLayoutingExamples;
155   PushButton mNextLayout;
156   unsigned int mLayoutIndex;
157   TextLabel mToolbarTitle;
158 };
159
160 int DALI_EXPORT_API main( int argc, char **argv )
161 {
162   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
163   LayoutingExample test( application );
164   application.MainLoop();
165   return 0;
166 };