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