Custom control transform animation
[platform/core/uifw/dali-demo.git] / examples / transitions / transition-application.cpp
1 /*
2  * Copyright (c) 2016 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 "beat-control.h"
28 #include <cstdio>
29 #include <sstream>
30
31 // Internal includes
32
33 using namespace Dali;
34 using namespace Dali::Toolkit;
35
36 namespace Demo
37 {
38
39 const char* TransitionApplication::DEMO_THEME_ONE_PATH( DEMO_STYLE_DIR "style-example-theme-one.json" );
40
41
42 TransitionApplication::TransitionApplication( Application& application )
43 : mApplication( application )
44 {
45   application.InitSignal().Connect( this, &TransitionApplication::Create );
46 }
47
48 TransitionApplication::~TransitionApplication()
49 {
50 }
51
52 void TransitionApplication::Create( Application& application )
53 {
54   Stage stage = Stage::GetCurrent();
55   stage.KeyEventSignal().Connect(this, &TransitionApplication::OnKeyEvent);
56   stage.SetBackgroundColor( Vector4( 0.1f, 0.1f, 0.1f, 1.0f ) );
57
58   // Hide the indicator bar
59   application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
60
61   // Content panes:
62   TableView contentLayout = TableView::New( 3, 1 );
63   contentLayout.SetName("ContentLayout");
64   contentLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
65   contentLayout.SetAnchorPoint( AnchorPoint::TOP_LEFT );
66   contentLayout.SetParentOrigin( ParentOrigin::TOP_LEFT );
67   contentLayout.SetCellPadding( Size( 10, 10 ) );
68
69   // Assign all rows the size negotiation property of fitting to children
70
71   stage.Add( contentLayout );
72
73   mTitle = TextLabel::New( "Custom Control Transition Example" );
74   mTitle.SetName( "Title" );
75   mTitle.SetStyleName("Title");
76   mTitle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
77   mTitle.SetParentOrigin( ParentOrigin::TOP_CENTER );
78   mTitle.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
79   mTitle.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
80   mTitle.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
81   contentLayout.Add( mTitle );
82   contentLayout.SetFitHeight(0);
83
84   mBeatControl = BeatControl::New();
85   mBeatControl.SetName("BeatControl");
86   mBeatControl.SetAnchorPoint( AnchorPoint::CENTER );
87   mBeatControl.SetParentOrigin( ParentOrigin::CENTER );
88   mBeatControl.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
89   contentLayout.Add( mBeatControl );
90   // beat control should fill the tableview cell, so no change to default parameters
91
92   TableView actionButtonLayout = TableView::New( 1, 4 );
93   actionButtonLayout.SetName("ThemeButtonsLayout");
94   actionButtonLayout.SetCellPadding( Vector2( 6.0f, 0.0f ) );
95
96   actionButtonLayout.SetAnchorPoint( AnchorPoint::CENTER );
97   actionButtonLayout.SetParentOrigin( ParentOrigin::CENTER );
98   actionButtonLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
99   actionButtonLayout.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
100   actionButtonLayout.SetCellPadding( Size( 10, 10 ) );
101   actionButtonLayout.SetFitHeight( 0 );
102
103   TextLabel label = TextLabel::New( "Action: ");
104   label.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
105   label.SetStyleName("ActionLabel");
106   label.SetAnchorPoint( AnchorPoint::TOP_CENTER );
107   label.SetParentOrigin( ParentOrigin::TOP_CENTER );
108   actionButtonLayout.AddChild( label, TableView::CellPosition( 0, 0 ) );
109   actionButtonLayout.SetCellAlignment( TableView::CellPosition( 0, 0 ), HorizontalAlignment::LEFT, VerticalAlignment::CENTER );
110
111   for( int i=0; i<3; ++i )
112   {
113     mActionButtons[i] = PushButton::New();
114     mActionButtons[i].SetName("ActionButton");
115     mActionButtons[i].SetStyleName("ActionButton");
116     mActionButtons[i].SetParentOrigin( ParentOrigin::CENTER );
117     mActionButtons[i].SetAnchorPoint( ParentOrigin::CENTER );
118     mActionButtons[i].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
119     mActionButtons[i].SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
120     mActionIndex = mActionButtons[i].RegisterProperty( "actionId", i, Property::READ_WRITE );
121     mActionButtons[i].ClickedSignal().Connect( this, &TransitionApplication::OnActionButtonClicked );
122     actionButtonLayout.AddChild( mActionButtons[i], TableView::CellPosition( 0, 1+i ) );
123   }
124   mActionButtons[0].SetLabelText( "Bounce" );
125   mActionButtons[1].SetLabelText( "X" );
126   mActionButtons[2].SetLabelText( "Y" );
127
128   contentLayout.Add( actionButtonLayout );
129   contentLayout.SetFitHeight(2);
130 }
131
132
133 bool TransitionApplication::OnActionButtonClicked( Button button )
134 {
135   int action = button.GetProperty<int>( mActionIndex );
136   switch( action )
137   {
138     case 0:
139     {
140       mBeatControl.StartBounceAnimation();
141       break;
142     }
143     case 1:
144     {
145       mBeatControl.StartXAnimation();
146       break;
147     }
148     case 2:
149     {
150       mBeatControl.StartYAnimation();
151       break;
152     }
153   }
154
155   return true;
156 }
157
158 void TransitionApplication::OnKeyEvent( const KeyEvent& keyEvent )
159 {
160   static int keyPressed = 0;
161
162   if( keyEvent.state == KeyEvent::Down)
163   {
164     if( keyPressed == 0 ) // Is this the first down event?
165     {
166       printf("Key pressed: %s %d\n", keyEvent.keyPressedName.c_str(), keyEvent.keyCode );
167
168       if( IsKey( keyEvent, DALI_KEY_ESCAPE) || IsKey( keyEvent, DALI_KEY_BACK ) )
169       {
170         mApplication.Quit();
171       }
172       else if( keyEvent.keyPressedName.compare("Return") == 0 )
173       {
174       }
175     }
176     keyPressed = 1;
177   }
178   else if( keyEvent.state == KeyEvent::Up )
179   {
180     keyPressed = 0;
181   }
182 }
183
184 } // namespace Demo