(Layouting) Made it easier to add new layouting examples & toggle between all
[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 "linear-example.h"
28 #include "padding-example.h"
29 #include "example.h"
30
31 using namespace Dali;
32 using namespace Dali::Toolkit;
33
34 namespace
35 {
36
37 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "lake_front.jpg" );
38 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
39
40 const char* APPLICATION_TITLE( "Layout Tester" );
41
42 typedef std::unique_ptr< Demo::Example > ExamplePointer;
43 typedef std::vector< ExamplePointer > ExampleContainer;
44
45 /// All layouting examples to be shown should be added to this method
46 void CreateExamples( ExampleContainer& container )
47 {
48   container.push_back( ExamplePointer( new Demo::LinearExample ) );
49   container.push_back( ExamplePointer( new Demo::PaddingExample ) );
50 }
51
52 } // anonymous namespace
53
54 class LayoutingExample: public ConnectionTracker
55 {
56  public:
57
58   LayoutingExample( Application& application )
59   : mApplication( application ),
60     mLayoutingExamples(),
61     mLayoutIndex( 0 )
62   {
63     // Connect to the Application's Init signal
64     mApplication.InitSignal().Connect( this, &LayoutingExample::Create );
65   }
66
67 private:
68
69   void Create( Application& application )
70   {
71     auto stage = Stage::GetCurrent();
72     stage.KeyEventSignal().Connect( this, &LayoutingExample::OnKeyEvent );
73
74     auto bg = ImageView::New( BACKGROUND_IMAGE );
75     bg.SetParentOrigin( ParentOrigin::CENTER );
76     stage.Add( bg );
77     auto toolbar = ImageView::New( TOOLBAR_IMAGE );
78     toolbar.SetParentOrigin( ParentOrigin::TOP_CENTER );
79     toolbar.SetAnchorPoint( AnchorPoint::TOP_CENTER );
80     toolbar.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
81     toolbar.SetProperty( Actor::Property::SIZE_HEIGHT, 50.0f);
82
83     stage.Add( toolbar );
84
85     auto title = TextLabel::New( APPLICATION_TITLE );
86     title.SetParentOrigin( ParentOrigin::CENTER );
87     title.SetAnchorPoint( AnchorPoint::CENTER );
88     title.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLUE );
89     title.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, HorizontalAlignment::LEFT );
90     toolbar.Add( title );
91
92     mNextLayout = PushButton::New();
93     mNextLayout.SetProperty( Toolkit::Button::Property::LABEL, "change layout");
94     mNextLayout.ClickedSignal().Connect( this, &LayoutingExample::ChangeLayout );
95     mNextLayout.SetParentOrigin( ParentOrigin::TOP_RIGHT );
96     mNextLayout.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
97     mNextLayout.SetSize(175, 50);
98     toolbar.Add( mNextLayout );
99
100     CreateExamples( mLayoutingExamples );
101     if( ! mLayoutingExamples.empty() )
102     {
103       mLayoutingExamples[ mLayoutIndex ]->Create();
104     }
105   }
106
107   bool ChangeLayout( Button button )
108   {
109     if( ! mLayoutingExamples.empty() )
110     {
111       mLayoutingExamples[ mLayoutIndex ]->Remove();
112       mLayoutIndex = ( mLayoutIndex + 1 ) % mLayoutingExamples.size();
113       mLayoutingExamples[ mLayoutIndex ]->Create();
114     }
115     return true;
116   }
117
118   /**
119    * Main key event handler
120    */
121   void OnKeyEvent(const KeyEvent& event)
122   {
123     if(event.state == KeyEvent::Down)
124     {
125       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
126       {
127         mApplication.Quit();
128       }
129     }
130   }
131
132
133 private:
134   Application& mApplication;
135   ExampleContainer mLayoutingExamples;
136   PushButton mNextLayout;
137   unsigned int mLayoutIndex;
138 };
139
140 int DALI_EXPORT_API main( int argc, char **argv )
141 {
142   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
143   LayoutingExample test( application );
144   application.MainLoop();
145   return 0;
146 };