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