Added application to test visual transition framework
[platform/core/uifw/dali-demo.git] / examples / transitions / transition-application.cpp
1 /*
2  * Copyright (c) 2017 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  * @file transition-application.cpp
19  * @brief Application class for showing stylable transitions
20  */
21
22 // Class include
23 #include "transition-application.h"
24
25 // External includes
26 #include <dali-toolkit/dali-toolkit.h>
27 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
28 #include <dali-toolkit/devel-api/visuals/text-visual-properties.h>
29 #include "shadow-button.h"
30 #include <cstdio>
31 #include <sstream>
32
33 // Internal includes
34
35 using namespace Dali;
36 using namespace Dali::Toolkit;
37
38 namespace
39 {
40
41 void SetLabelText( Button button, const char* label )
42 {
43   button.SetProperty( Toolkit::Button::Property::LABEL, label );
44 }
45
46 }
47
48 namespace Demo
49 {
50
51 const char* TransitionApplication::DEMO_THEME_ONE_PATH( DEMO_STYLE_DIR "style-example-theme-one.json" );
52 const char* TransitionApplication::DEMO_THEME_TWO_PATH( DEMO_STYLE_DIR "style-example-theme-two.json" );
53
54
55 TransitionApplication::TransitionApplication( Application& application )
56 : mApplication( application ),
57   mTitle(),
58   mShadowButton(),
59   mActionButtons(),
60   mActionIndex( Property::INVALID_INDEX )
61 {
62   application.InitSignal().Connect( this, &TransitionApplication::Create );
63 }
64
65 TransitionApplication::~TransitionApplication()
66 {
67 }
68
69 void TransitionApplication::Create( Application& application )
70 {
71   Stage stage = Stage::GetCurrent();
72   stage.KeyEventSignal().Connect(this, &TransitionApplication::OnKeyEvent);
73   stage.SetBackgroundColor( Vector4( 0.1f, 0.1f, 0.1f, 1.0f ) );
74
75   // Hide the indicator bar
76   application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
77
78   // Content panes:
79   TableView contentLayout = TableView::New( 3, 1 );
80   contentLayout.SetName("ContentLayout");
81   contentLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
82   contentLayout.SetAnchorPoint( AnchorPoint::TOP_LEFT );
83   contentLayout.SetParentOrigin( ParentOrigin::TOP_LEFT );
84   contentLayout.SetCellPadding( Vector2( 0.0f, 5.0f ) );
85   contentLayout.SetBackgroundColor( Vector4(0.949, 0.949, 0.949, 1.0) );
86   // Assign all rows the size negotiation property of fitting to children
87
88   stage.Add( contentLayout );
89
90   mTitle = TextLabel::New( "Custom Control Transition Example" );
91   mTitle.SetName( "Title" );
92   mTitle.SetStyleName("Title");
93   mTitle.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
94   mTitle.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
95   mTitle.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
96   contentLayout.Add( mTitle );
97   contentLayout.SetFitHeight(0); // Fill width
98
99   // Provide some padding around the center cell
100   TableView buttonLayout = TableView::New( 3, 3 );
101   buttonLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
102   buttonLayout.SetFixedHeight(1, 100 );
103   buttonLayout.SetFixedWidth(1, 350 );
104   contentLayout.Add( buttonLayout );
105
106   mShadowButton = ShadowButton::New();
107   mShadowButton.SetName("ShadowButton");
108   mShadowButton.SetActiveState( false );
109   mShadowButton.SetCheckState( false );
110   mShadowButton.SetAnchorPoint( AnchorPoint::CENTER );
111   mShadowButton.SetParentOrigin( ParentOrigin::CENTER );
112   mShadowButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
113   buttonLayout.AddChild( mShadowButton, TableView::CellPosition(1, 1) );
114
115   TableView actionButtonLayout = TableView::New( 1, NUMBER_OF_ACTION_BUTTONS+1 );
116   actionButtonLayout.SetName("ThemeButtonsLayout");
117   actionButtonLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
118   actionButtonLayout.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
119   actionButtonLayout.SetFitHeight( 0 );
120
121   TextLabel label = TextLabel::New( "Action: ");
122   label.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
123   label.SetStyleName("ActionLabel");
124   actionButtonLayout.AddChild( label, TableView::CellPosition( 0, 0 ) );
125   actionButtonLayout.SetCellAlignment( TableView::CellPosition( 0, 0 ), HorizontalAlignment::LEFT, VerticalAlignment::CENTER );
126
127   for( int i=0; i<NUMBER_OF_ACTION_BUTTONS; ++i )
128   {
129     mActionButtons[i] = PushButton::New();
130     mActionButtons[i].SetName("ActionButton");
131     mActionButtons[i].SetStyleName("ActionButton");
132     mActionButtons[i].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
133     mActionButtons[i].SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
134     mActionIndex = mActionButtons[i].RegisterProperty( "actionId", i, Property::READ_WRITE );
135     mActionButtons[i].ClickedSignal().Connect( this, &TransitionApplication::OnActionButtonClicked );
136     actionButtonLayout.AddChild( mActionButtons[i], TableView::CellPosition( 0, 1+i ) );
137   }
138   SetLabelText( mActionButtons[0], "Activate" );
139   SetLabelText( mActionButtons[1], "Check" );
140
141   contentLayout.Add( actionButtonLayout );
142   contentLayout.SetFitHeight(2);
143 }
144
145 bool TransitionApplication::OnActionButtonClicked( Button button )
146 {
147   int action = button.GetProperty<int>( mActionIndex );
148   switch( action )
149   {
150     case 0:
151     {
152       bool activeState = mShadowButton.GetActiveState();
153       mShadowButton.SetActiveState( ! activeState );
154       if( activeState )
155       {
156         SetLabelText( button, "Activate" );
157       }
158       else
159       {
160         SetLabelText( button, "Deactivate" );
161       }
162       break;
163     }
164     case 1:
165     {
166       bool checkState = mShadowButton.GetCheckState();
167       mShadowButton.SetCheckState( ! checkState );
168       break;
169     }
170     case 2:
171     {
172       break;
173     }
174     case 3:
175     {
176       break;
177     }
178   }
179
180   return true;
181 }
182
183 void TransitionApplication::OnKeyEvent( const KeyEvent& keyEvent )
184 {
185   static int keyPressed = 0;
186
187   if( keyEvent.state == KeyEvent::Down)
188   {
189     if( keyPressed == 0 ) // Is this the first down event?
190     {
191       printf("Key pressed: %s %d\n", keyEvent.keyPressedName.c_str(), keyEvent.keyCode );
192
193       if( IsKey( keyEvent, DALI_KEY_ESCAPE) || IsKey( keyEvent, DALI_KEY_BACK ) )
194       {
195         mApplication.Quit();
196       }
197       else if( keyEvent.keyPressedName.compare("Return") == 0 )
198       {
199       }
200     }
201     keyPressed = 1;
202   }
203   else if( keyEvent.state == KeyEvent::Up )
204   {
205     keyPressed = 0;
206   }
207 }
208
209 } // namespace Demo