[dali_1.3.47] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / layouting / layouting-examples.cpp
1 /*
2  * Copyright (c) 2018 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
41 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "lake_front.jpg" );
42 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
43
44 typedef std::unique_ptr< Demo::Example > ExamplePointer;
45
46 typedef std::vector< ExamplePointer > ExampleContainer;
47
48 /// All layouting examples to be shown should be added to this method
49 void CreateExamples( ExampleContainer& container )
50 {
51   container.push_back( ExamplePointer(new Demo::AnimationExample) );
52   container.push_back( ExamplePointer(new Demo::LinearExample) );
53   container.push_back( ExamplePointer(new Demo::PaddingExample) );
54   container.push_back( ExamplePointer(new Demo::AbsoluteExample) );
55   container.push_back( ExamplePointer(new Demo::FlexExample) ) ;
56   container.push_back( ExamplePointer(new Demo::GridExample) ) ;
57 }
58
59 } // anonymous namespace
60
61 class LayoutingExample: public ConnectionTracker
62 {
63  public:
64
65   LayoutingExample( Application& application )
66   : mApplication( application ),
67     mLayoutingExamples(),
68     mLayoutIndex( 0 )
69   {
70     // Connect to the Application's Init signal
71     mApplication.InitSignal().Connect( this, &LayoutingExample::Create );
72   }
73
74 private:
75
76   void Create( Application& application )
77   {
78     auto stage = Stage::GetCurrent();
79     stage.KeyEventSignal().Connect( this, &LayoutingExample::OnKeyEvent );
80
81     auto bg = ImageView::New( BACKGROUND_IMAGE );
82     bg.SetParentOrigin( ParentOrigin::CENTER );
83     stage.Add( bg );
84     auto toolbar = ImageView::New( TOOLBAR_IMAGE );
85     toolbar.SetParentOrigin( ParentOrigin::TOP_CENTER );
86     toolbar.SetAnchorPoint( AnchorPoint::TOP_CENTER );
87     toolbar.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
88     toolbar.SetProperty( Actor::Property::SIZE_HEIGHT, 50.0f);
89
90     stage.Add( toolbar );
91
92     mToolbarTitle = TextLabel::New( "");
93     mToolbarTitle.SetParentOrigin( ParentOrigin::CENTER );
94     mToolbarTitle.SetAnchorPoint( AnchorPoint::CENTER );
95     mToolbarTitle.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLUE );
96     mToolbarTitle.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, HorizontalAlignment::LEFT );
97     toolbar.Add( mToolbarTitle );
98
99     mNextLayout = PushButton::New();
100     mNextLayout.SetProperty( Toolkit::Button::Property::LABEL, "change layout");
101     mNextLayout.ClickedSignal().Connect( this, &LayoutingExample::ChangeLayout );
102     mNextLayout.SetParentOrigin( ParentOrigin::TOP_RIGHT );
103     mNextLayout.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
104     mNextLayout.SetSize(175, 50);
105     toolbar.Add( mNextLayout );
106
107     CreateExamples( mLayoutingExamples );
108     if( ! mLayoutingExamples.empty() )
109     {
110       mLayoutingExamples[ mLayoutIndex ]->Create();
111       mToolbarTitle.SetProperty(Toolkit::TextLabel::Property::TEXT, mLayoutingExamples[ mLayoutIndex ]->GetExampleTitle() );
112     }
113   }
114
115   bool ChangeLayout( Button button )
116   {
117     if( ! mLayoutingExamples.empty() )
118     {
119       mLayoutingExamples[ mLayoutIndex ]->Remove();
120       mLayoutIndex = ( mLayoutIndex + 1 ) % mLayoutingExamples.size();
121       mLayoutingExamples[ mLayoutIndex ]->Create();
122       mToolbarTitle.SetProperty(Toolkit::TextLabel::Property::TEXT, mLayoutingExamples[ mLayoutIndex ]->Example::GetExampleTitle() );
123     }
124     return true;
125   }
126
127   /**
128    * Main key event handler
129    */
130   void OnKeyEvent(const KeyEvent& event)
131   {
132     if(event.state == KeyEvent::Down)
133     {
134       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
135       {
136         mApplication.Quit();
137       }
138     }
139   }
140
141
142 private:
143   Application& mApplication;
144   ExampleContainer mLayoutingExamples;
145   PushButton mNextLayout;
146   unsigned int mLayoutIndex;
147   TextLabel mToolbarTitle;
148 };
149
150 int DALI_EXPORT_API main( int argc, char **argv )
151 {
152   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
153   LayoutingExample test( application );
154   application.MainLoop();
155   return 0;
156 };