Merge "Remove non-touch related deprecated APIs" into devel/master
[platform/core/uifw/dali-demo.git] / examples / transitions / transition-application.cpp
1 /*
2  * Copyright (c) 2020 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 <dali-toolkit/devel-api/controls/table-view/table-view.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   mVisualIndex( Property::INVALID_INDEX ),
61   mActionIndex( Property::INVALID_INDEX )
62 {
63   application.InitSignal().Connect( this, &TransitionApplication::Create );
64 }
65
66 TransitionApplication::~TransitionApplication()
67 {
68 }
69
70 void TransitionApplication::Create( Application& application )
71 {
72   Stage stage = Stage::GetCurrent();
73   stage.KeyEventSignal().Connect(this, &TransitionApplication::OnKeyEvent);
74   stage.SetBackgroundColor( Vector4( 0.1f, 0.1f, 0.1f, 1.0f ) );
75
76   // Content panes:
77   TableView contentLayout = TableView::New( 3, 1 );
78   contentLayout.SetProperty( Dali::Actor::Property::NAME,"ContentLayout");
79   contentLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
80   contentLayout.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
81   contentLayout.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
82   contentLayout.SetCellPadding( Vector2( 0.0f, 5.0f ) );
83   contentLayout.SetBackgroundColor( Vector4(0.949, 0.949, 0.949, 1.0) );
84   // Assign all rows the size negotiation property of fitting to children
85
86   stage.Add( contentLayout );
87
88   mTitle = TextLabel::New( "Custom Control Transition Example" );
89   mTitle.SetProperty( Dali::Actor::Property::NAME, "Title" );
90   mTitle.SetStyleName("Title");
91   mTitle.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
92   mTitle.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
93   mTitle.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
94   contentLayout.Add( mTitle );
95   contentLayout.SetFitHeight(0); // Fill width
96
97   // Provide some padding around the center cell
98   TableView buttonLayout = TableView::New( 3, 3 );
99   buttonLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
100   buttonLayout.SetFixedHeight(1, 100 );
101   buttonLayout.SetFixedWidth(1, 350 );
102   contentLayout.Add( buttonLayout );
103
104   mShadowButton = ShadowButton::New();
105   mShadowButton.SetProperty( Dali::Actor::Property::NAME,"ShadowButton");
106   mShadowButton.SetActiveState( false );
107   mShadowButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
108   mShadowButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
109   mShadowButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
110   mShadowButton.SetProperty( DevelControl::Property::STATE, DevelControl::DISABLED );
111   mShadowButton.SetProperty( DevelControl::Property::SUB_STATE, "UNCHECKED" );
112
113   buttonLayout.AddChild( mShadowButton, TableView::CellPosition(1, 1) );
114
115   TableView actionButtonLayout = TableView::New( 1, NUMBER_OF_ACTION_BUTTONS+1 );
116   actionButtonLayout.SetProperty( Dali::Actor::Property::NAME,"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].SetProperty( Dali::Actor::Property::NAME,"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], "Enable" );
139   SetLabelText( mActionButtons[1], "Check" );
140   mActionButtons[1].SetProperty( Button::Property::DISABLED, true );
141
142   contentLayout.Add( actionButtonLayout );
143   contentLayout.SetFitHeight(2);
144 }
145
146 bool TransitionApplication::OnActionButtonClicked( Button button )
147 {
148   int action = button.GetProperty<int>( mActionIndex );
149   switch( action )
150   {
151     case 0:
152     {
153       bool activeState = mShadowButton.GetActiveState();
154       mShadowButton.SetActiveState( ! activeState );
155       if( activeState )
156       {
157         SetLabelText( button, "Enable" );
158         mShadowButton.SetProperty( DevelControl::Property::STATE, DevelControl::DISABLED );
159       }
160       else
161       {
162         SetLabelText( button, "Disable" );
163         mShadowButton.SetProperty( DevelControl::Property::STATE, DevelControl::NORMAL );
164       }
165       mActionButtons[1].SetProperty( Button::Property::DISABLED, activeState );
166       break;
167     }
168     case 1:
169     {
170       bool checkState = mShadowButton.GetCheckState();
171       mShadowButton.SetCheckState( ! checkState );
172       if( checkState )
173       {
174         SetLabelText( button, "Check" );
175         mShadowButton.SetProperty( DevelControl::Property::SUB_STATE, "UNCHECKED" );
176       }
177       else
178       {
179         SetLabelText( button, "Uncheck" );
180         mShadowButton.SetProperty( DevelControl::Property::SUB_STATE, "CHECKED" );
181       }
182       break;
183     }
184     case 2:
185     {
186       break;
187     }
188     case 3:
189     {
190       break;
191     }
192   }
193
194   return true;
195 }
196
197 void TransitionApplication::OnKeyEvent( const KeyEvent& keyEvent )
198 {
199   static int keyPressed = 0;
200
201   if( keyEvent.state == KeyEvent::Down)
202   {
203     if( keyPressed == 0 ) // Is this the first down event?
204     {
205       printf("Key pressed: %s %d\n", keyEvent.keyPressedName.c_str(), keyEvent.keyCode );
206
207       if( IsKey( keyEvent, DALI_KEY_ESCAPE) || IsKey( keyEvent, DALI_KEY_BACK ) )
208       {
209         mApplication.Quit();
210       }
211       else if( keyEvent.keyPressedName.compare("Return") == 0 )
212       {
213       }
214     }
215     keyPressed = 1;
216   }
217   else if( keyEvent.state == KeyEvent::Up )
218   {
219     keyPressed = 0;
220   }
221 }
222
223 } // namespace Demo