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