[dali_1.2.60] Merge branch 'devel/master'
[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/controls/control-devel.h>
28 #include "shadow-button.h"
29 #include <cstdio>
30 #include <sstream>
31
32 // Internal includes
33
34 using namespace Dali;
35 using namespace Dali::Toolkit;
36
37 namespace
38 {
39
40 void SetLabelText( Button button, const char* label )
41 {
42   button.SetProperty( Toolkit::Button::Property::LABEL, label );
43 }
44
45 }
46
47 namespace Demo
48 {
49
50 const char* TransitionApplication::DEMO_THEME_ONE_PATH( DEMO_STYLE_DIR "style-example-theme-one.json" );
51 const char* TransitionApplication::DEMO_THEME_TWO_PATH( DEMO_STYLE_DIR "style-example-theme-two.json" );
52
53
54 TransitionApplication::TransitionApplication( Application& application )
55 : mApplication( application ),
56   mTitle(),
57   mShadowButton(),
58   mActionButtons(),
59   mVisualIndex( Property::INVALID_INDEX ),
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.SetAnchorPoint( AnchorPoint::CENTER );
110   mShadowButton.SetParentOrigin( ParentOrigin::CENTER );
111   mShadowButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
112   mShadowButton.SetProperty( DevelControl::Property::STATE, DevelControl::DISABLED );
113   mShadowButton.SetProperty( DevelControl::Property::SUB_STATE, "UNCHECKED" );
114
115   buttonLayout.AddChild( mShadowButton, TableView::CellPosition(1, 1) );
116
117   TableView actionButtonLayout = TableView::New( 1, NUMBER_OF_ACTION_BUTTONS+1 );
118   actionButtonLayout.SetName("ThemeButtonsLayout");
119   actionButtonLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
120   actionButtonLayout.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
121   actionButtonLayout.SetFitHeight( 0 );
122
123   TextLabel label = TextLabel::New( "Action: ");
124   label.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
125   label.SetStyleName("ActionLabel");
126   actionButtonLayout.AddChild( label, TableView::CellPosition( 0, 0 ) );
127   actionButtonLayout.SetCellAlignment( TableView::CellPosition( 0, 0 ), HorizontalAlignment::LEFT, VerticalAlignment::CENTER );
128
129   for( int i=0; i<NUMBER_OF_ACTION_BUTTONS; ++i )
130   {
131     mActionButtons[i] = PushButton::New();
132     mActionButtons[i].SetName("ActionButton");
133     mActionButtons[i].SetStyleName("ActionButton");
134     mActionButtons[i].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
135     mActionButtons[i].SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
136     mActionIndex = mActionButtons[i].RegisterProperty( "actionId", i, Property::READ_WRITE );
137     mActionButtons[i].ClickedSignal().Connect( this, &TransitionApplication::OnActionButtonClicked );
138     actionButtonLayout.AddChild( mActionButtons[i], TableView::CellPosition( 0, 1+i ) );
139   }
140   SetLabelText( mActionButtons[0], "Enable" );
141   SetLabelText( mActionButtons[1], "Check" );
142   mActionButtons[1].SetProperty( Button::Property::DISABLED, true );
143
144   contentLayout.Add( actionButtonLayout );
145   contentLayout.SetFitHeight(2);
146 }
147
148 bool TransitionApplication::OnActionButtonClicked( Button button )
149 {
150   int action = button.GetProperty<int>( mActionIndex );
151   switch( action )
152   {
153     case 0:
154     {
155       bool activeState = mShadowButton.GetActiveState();
156       mShadowButton.SetActiveState( ! activeState );
157       if( activeState )
158       {
159         SetLabelText( button, "Enable" );
160         mShadowButton.SetProperty( DevelControl::Property::STATE, DevelControl::DISABLED );
161       }
162       else
163       {
164         SetLabelText( button, "Disable" );
165         mShadowButton.SetProperty( DevelControl::Property::STATE, DevelControl::NORMAL );
166       }
167       mActionButtons[1].SetProperty( Button::Property::DISABLED, activeState );
168       break;
169     }
170     case 1:
171     {
172       bool checkState = mShadowButton.GetCheckState();
173       mShadowButton.SetCheckState( ! checkState );
174       if( checkState )
175       {
176         SetLabelText( button, "Check" );
177         mShadowButton.SetProperty( DevelControl::Property::SUB_STATE, "UNCHECKED" );
178       }
179       else
180       {
181         SetLabelText( button, "Uncheck" );
182         mShadowButton.SetProperty( DevelControl::Property::SUB_STATE, "CHECKED" );
183       }
184       break;
185     }
186     case 2:
187     {
188       break;
189     }
190     case 3:
191     {
192       break;
193     }
194   }
195
196   return true;
197 }
198
199 void TransitionApplication::OnKeyEvent( const KeyEvent& keyEvent )
200 {
201   static int keyPressed = 0;
202
203   if( keyEvent.state == KeyEvent::Down)
204   {
205     if( keyPressed == 0 ) // Is this the first down event?
206     {
207       printf("Key pressed: %s %d\n", keyEvent.keyPressedName.c_str(), keyEvent.keyCode );
208
209       if( IsKey( keyEvent, DALI_KEY_ESCAPE) || IsKey( keyEvent, DALI_KEY_BACK ) )
210       {
211         mApplication.Quit();
212       }
213       else if( keyEvent.keyPressedName.compare("Return") == 0 )
214       {
215       }
216     }
217     keyPressed = 1;
218   }
219   else if( keyEvent.state == KeyEvent::Up )
220   {
221     keyPressed = 0;
222   }
223 }
224
225 } // namespace Demo