Extending Layout tester demo
[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 #include <string>
19 #include "shared/view.h"
20 #include "linear-example.h"
21 #include "padding-example.h"
22 #include <dali/dali.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/devel-api/controls/control-devel.h>
25
26 using namespace Dali;
27 using namespace Dali::Toolkit;
28
29 namespace
30 {
31
32 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "lake_front.jpg" );
33 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
34
35 const char* APPLICATION_TITLE( "Layout tester" );
36
37 }  // namespace
38
39 class LayoutingExample: public ConnectionTracker
40 {
41  public:
42
43   LayoutingExample( Application& application )
44   : mApplication( application ),
45     mLinearExample(),
46     mPaddedExample(),
47     mLayoutIndex( 0 )
48   {
49     // Connect to the Application's Init signal
50     mApplication.InitSignal().Connect( this, &LayoutingExample::Create );
51   }
52
53   ~LayoutingExample()
54   {
55     // Nothing to do here
56   }
57
58   void Create( Application& application )
59   {
60     // The Init signal is received once (only) during the Application lifetime
61
62     auto stage = Stage::GetCurrent();
63
64     auto bg = ImageView::New( BACKGROUND_IMAGE );
65     bg.SetParentOrigin( ParentOrigin::CENTER );
66     stage.Add( bg );
67     auto toolbar = ImageView::New( TOOLBAR_IMAGE );
68     toolbar.SetParentOrigin( ParentOrigin::TOP_CENTER );
69     toolbar.SetAnchorPoint( AnchorPoint::TOP_CENTER );
70     toolbar.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
71     toolbar.SetProperty( Actor::Property::SIZE_HEIGHT, 50.0f);
72
73     stage.Add( toolbar );
74
75     auto title = TextLabel::New( APPLICATION_TITLE );
76     title.SetParentOrigin( ParentOrigin::CENTER );
77     title.SetAnchorPoint( AnchorPoint::CENTER );
78     title.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLUE );
79     title.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, HorizontalAlignment::LEFT );
80     toolbar.Add( title );
81
82     mNextLayout = PushButton::New();
83     mNextLayout.SetProperty( Toolkit::Button::Property::LABEL, "change layout");
84     mNextLayout.ClickedSignal().Connect( this, &LayoutingExample::ChangeLayout );
85     mNextLayout.SetParentOrigin( ParentOrigin::TOP_RIGHT );
86     mNextLayout.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
87     mNextLayout.SetSize(175, 50);
88     toolbar.Add( mNextLayout );
89
90     mLinearExample.Demo::LinearExample::Create();
91   }
92
93   bool ChangeLayout( Button button )
94   {
95     mLayoutIndex++;
96
97     switch( mLayoutIndex )
98     {
99       case 1 :
100       {
101         mLinearExample.Remove();
102         mPaddedExample.Create();
103         break;
104       }
105       case 2 :
106       {
107         mPaddedExample.Remove();
108         mNextLayout.SetProperty( Toolkit::Button::Property::LABEL, "end of test");
109         mNextLayout.SetProperty( Toolkit::Button::Property::DISABLED, true );
110         break;
111       }
112       default :
113       {
114         break;
115       }
116     }
117
118     return true;
119   }
120
121 private:
122   /**
123    * Main key event handler
124    */
125   void OnKeyEvent(const KeyEvent& event)
126   {
127     if(event.state == KeyEvent::Down)
128     {
129       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
130       {
131         mApplication.Quit();
132       }
133     }
134   }
135
136
137 private:
138   Application& mApplication;
139   Demo::LinearExample mLinearExample;
140   Demo::PaddingExample mPaddedExample;
141   PushButton mNextLayout;
142   unsigned int mLayoutIndex;
143 };
144
145 int DALI_EXPORT_API main( int argc, char **argv )
146 {
147   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
148   LayoutingExample test( application );
149   application.MainLoop();
150   return 0;
151 };